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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

714<br />

This example isn’t too exciting, but it does illustrate the power <strong>and</strong> simplicity of doing<br />

matrix transformations in <strong>Processing</strong>. In the example, I used the rect() comm<strong>and</strong> three<br />

times, using the same four arguments; yet I got three rectangles with different sizes <strong>and</strong><br />

positions. If you run this example, you should notice four tables of numbers output in the<br />

<strong>Processing</strong> console. Each table is composed of two rows <strong>and</strong> three columns. These mysterious<br />

numbers are the actual matrices at different states in the program. You’ll notice that<br />

the first <strong>and</strong> last table of numbers are identical:<br />

1.0000 0.0000 0.0000<br />

0.0000 1.0000 0.0000<br />

These tables represent the base, or identity, matrix, without any transformation applied.<br />

The first rectangle I created used this matrix, before I called the first translate() function,<br />

<strong>and</strong> that’s why what I input with regard to the arguments is what I got out. The other<br />

two rectangles have different transforms, <strong>and</strong> thus look different. In going over this<br />

sketch, you should take the time to see what parts of the matrix get changed after each<br />

transformation.<br />

Another strange concept to deal with is the matrix stack. A stack is a temporary data structure<br />

that operates following the last in, first out (LIFO) principal. Stacks are utilized in<br />

many areas of computing, including managing transformation matrices. The pushMatrix()<br />

<strong>and</strong> popMatrix() functions alter the matrix stack, allowing different transformations to<br />

happen in a seemingly independent fashion. When you want to create a localized transformation<br />

(not affecting everything following the transformation call), you first call<br />

pushMatrix(), which adds any transformations that follow it onto the matrix stack. Then,<br />

when you’re done, you call popMatrix(), which restores the matrix to its previous form.<br />

This is the reason that the first <strong>and</strong> last tables of values shown previously were identical—<br />

the first table represented the untransformed matrix <strong>and</strong> the last table represented the<br />

matrix after popMatrix() was called—which removed the last transformation pushed onto<br />

the stack.<br />

This next example (shown in Figure A-13) is much more interesting than the last, <strong>and</strong> really<br />

illustrates the power of this whole matrix business. But it’s also pretty complex <strong>and</strong> long;<br />

so watch it, play with it, <strong>and</strong> don’t worry about underst<strong>and</strong>ing all the code yet. After you<br />

get a sense of what the sketch does (visually), try messing with some of the values; it’s a<br />

great way to see what’s going on programatically (<strong>and</strong> don’t worry about breaking the<br />

sketch, either). After you launch the example, let it run for a while, as there are some<br />

really interesting patterns that form <strong>and</strong> change in interesting <strong>and</strong> unpredictable ways<br />

over time.<br />

//Example 2:<br />

/*<br />

PushPop<br />

Ira Greenberg, November 8, 2005<br />

*/<br />

/*Need to import opengl library to use OPENGL<br />

rendering mode. You can also try running<br />

in P3D mode*/<br />

import processing.opengl.*;

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

Saved successfully!

Ooh no, something went wrong!