04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A quick review of creating transformations<br />

I think the preceding sketch illustrates nicely how numbers, code, <strong>and</strong> even rigid geometry<br />

can begin to approximate organic forms. You may have also noticed that I snuck in two<br />

new comm<strong>and</strong>s, pushMatrix() <strong>and</strong> popMatrix(), <strong>and</strong> also used resetMatrix(), which you<br />

looked at a little in Chapter 9. In that chapter, I also discussed <strong>Processing</strong>’s translate()<br />

function <strong>and</strong> talked a little bit about the underlying affine transformation matrix that controls<br />

how the coordinate geometry you create is ultimately mapped to the display window.<br />

This is important (albeit a little confusing) information, so I want to review some of it<br />

again. <strong>Processing</strong>’s display window is technically a Java component. Components include<br />

such things as buttons, panels, frames, <strong>and</strong> labels. Components utilize a graphics object to<br />

actually paint stuff in the component. The graphics object is just a Java class that encapsulates<br />

lower-level rendering stuff, which you don’t have to worry about. If, however, you<br />

would like to learn some more about these under-the-hood issues, check out http://<br />

java.sun.com/products/jfc/tsc/articles/painting/.<br />

When the display window’s graphics object does its thing, coordinate data is mapped from<br />

a kind of a virtual object coordinate space to the actual display window coordinate space.<br />

By default, these two spaces start out in sync. However, when I call a function like<br />

translate(), I can shift the virtual object space in reference to the display window—<br />

making the two coordinate spaces no longer in sync. For example, prior to calling<br />

translate(), the call rect(50, 50, 100, 100) draws a rectangle 50 pixels down <strong>and</strong> to<br />

the right from the top-left corner of the display window. However, if I call translate(25,<br />

25) prior to the rect(50, 50, 100, 100) call, the rectangle will be drawn 75 pixels down<br />

<strong>and</strong> to the right of the top-left corner of the display window. In addition, the transformed<br />

virtual coordinate space will remain in this changed state. So, with any additional drawing<br />

calls I make, the new geometry will be shifted 25 pixels down <strong>and</strong> to the right. If I call<br />

translate(25, 25) again, all new drawing calls will now be shifted 50 pixels down <strong>and</strong> to<br />

the right from the literal values passed to the respective drawing calls.<br />

When resetMatrix() is called, <strong>Processing</strong> in a sense resets the transformation state—<br />

putting the object coordinate space back in sync with the display window space. However,<br />

there are times when you don’t want to reset the entire transformation, <strong>and</strong> that’s when<br />

the functions pushMatrix() <strong>and</strong> popMatrix() come in.<br />

Pushing <strong>and</strong> popping the matrix<br />

When you surround a transform operation (such as translate(), rotate(), or scale())<br />

with the function calls pushMatrix() <strong>and</strong> popMatrix(), the original state of the matrix<br />

before the pushMatrix() call is restored after the popMatrix() call, regardless of what<br />

happens between the pushMatrix() <strong>and</strong> popMatrix() calls. Here’s an example that prints<br />

the matrix values before, during, <strong>and</strong> after to illustrate this point (see Figure 10-6):<br />

COLOR AND IMAGING<br />

409<br />

10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!