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 />

350<br />

Transforming shapes<br />

Figure 9-9. Triangle Spin sketch<br />

In the last example, I wanted to create a nice, even rotation around the center point of the<br />

triangles. Instead, what I got was the triangles spinning out of the display window. What<br />

happened? The computer didn’t actually do anything wrong (although I suspect you realized<br />

that). I asked it to rotate 3.6 degrees each iteration of the loop, <strong>and</strong> it did just that.<br />

When you call the rotate() function, <strong>Processing</strong> rotates around the origin, which is (0, 0),<br />

or the top-left corner of the display window. The triangles, however, are centered in the<br />

middle of window. So <strong>Processing</strong> did exactly what I asked of it—it rotated around the origin,<br />

not the center point of the triangles or the display window.<br />

To fix this issue, I either need to write my own custom triangle <strong>and</strong> rotate functions using<br />

some trig functions, or (the much simpler way) I can draw my triangles centered around<br />

the origin (0,0), <strong>and</strong> then use <strong>Processing</strong>’s translate() function to move them to the center<br />

of the display window. In reality, this shifts the graphics context of the entire display<br />

window, not just the triangles. The graphic context is the virtual drawing space of the display<br />

window. Here’s the last example corrected using <strong>Processing</strong>’s translate() function<br />

(see Figures 9-10 <strong>and</strong> 9-11):<br />

//Triangle Flower<br />

Point[]p = new Point[3];<br />

float shift = 1.0;<br />

float fade = 0;<br />

float fillCol = 0;<br />

float rot = 0;

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

Saved successfully!

Ooh no, something went wrong!