Processing : Why transform coordinate system, not individual elements?

Because transformations make everything so much simpler and your code so much cleaner.

Everything we draw on computers we describe with Cartesian coordinates (x, y, and maybe z). By a perfectly reasonable default, Processing uses one unit per pixel. When I’m drawing something, though, I don’t want to think in terms of pixels, but rather I would like to use whatever units are appropriate for the object I’m creating. If I’m drawing a house, I might use feet or meters. If it’s a face, I might use inches or millimeters. But then, if I’m drawing them together, I need to scale them into consistent units and scale those units to pixels. So I could include a scale factor in every single drawing call I make, but what a mess that makes of the code. Alternatively, I can just slap a scale( inches to feet conversion ); before my draw call and suddenly the face and house are in the consistent scaling.

Likewise, for convenience, I coded the face sitting about the origin so that I could easily use symmetry for the left and right, but now I want to position the face or even move it around. Again, I could litter my code with adding offsets to each individual draw call, OR I can just say pushMatrix(); translate( posx, posy ); drawFace(); popMatrix(); and leave the graphics calls clean and simple.

Nearly all of my Processing sketches start with

translate( width/2, height/2 );
scale( height/2.2 );

because that moves the origin to the center of the window and lets me draw a unit circle with a slight margin around it. Now I can draw stuff either in that circle or using sin/cos polar coordinates and I never have to worry about the screen resolution. If I want to crank the resolution high to print out a poster or make it small for an online GIF animation, my drawing commands needn’t worry about the difference.

3 Likes