draw_a_square_in_center
should only draw a square in center.
But it also change the global state of origin.
Now poor circle is at right-bottom of scren.
void setup() {
size(800, 600);
background(236, 226, 225);
}
void draw() {
draw_a_rectagle_in_center();
circle(width/2, height/2, 50);
}
void draw_a_square_in_center() {
translate(width/2, height/2);
rect(0, 0, 100,100);
}
- Functions don't depend on another
Use pushMatrix
everywhre.
void setup() {
size(800, 600);
background(236, 226, 225);
}
void draw() {
draw_a_rectagle_in_center();
circle(width/2, height/2, 50);
}
void draw_a_square_in_center() {
pushMatrix();
translate(width/2, height/2);
rect(0, 0, 100,100);
popMatrix();
}
f(x) = y means y is only depend on x, and global variable in our case :(. A function can operate with global variables, but it shouldn't need caller function's state. Beacuse it's hard for extracting specific part later from that chapter.
Wait what?
Why bar
didn't centered the circle. When I call from foo
it did.
void setup() {
size(800, 600);
background(236, 226, 225);
}
void draw() {
foo();
bar();
}
void foo() {
translate(width/2, height/2);
circle(0, 0, 100);
bar();
}
void bar(){
circle(0, 0, 50);
}
TODO
void foo(){
pushMatrix();
// Matrix must made sandwich
popMatrix();
}
Use auto-format from processing's ide. It's bound to C-t (Control-t)
Processing doesn't follow Cartesian coordinate system.
It uses translate(x,y)
to temporarirly changing orign.
Each changing lives untill
- Another
translate(x,y)
- Popped Matrix