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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

354<br />

Figure 9-12. Toy Truck sketch<br />

When looking at the code in the last example, I hope some of you protested, exclaiming<br />

“Hey, you can’t use all those magic numbers!” It’s true, it isn’t good practice to use magic<br />

numbers. And this really is a pretty lousy implementation of a truck (although it’s kind of<br />

cute, don’t you think?). The problem I faced was dealing with all the accumulating matrix<br />

transformations <strong>and</strong> having to manually plug in <strong>and</strong> try different values until I got my<br />

truck. Because of the scaling effect, some of the translation values got pretty wacky, with<br />

a few values far exceeding the display window dimensions. Fortunately, there is a much<br />

better way to h<strong>and</strong>le resetting the matrix values, which I’ll discuss shortly.<br />

The last basic shape function I’ll cover is quad(). This function works just like the<br />

triangle() function, but uses four vertices instead of three. The function requires eight<br />

arguments, defining the x <strong>and</strong> y components of each of the four vertices. Unlike the<br />

rect() function, in which each corner is 90 degrees, quad() can create a four-point polygon<br />

with different angles for each corner. In the following simple quad() example, I illustrate<br />

this using the r<strong>and</strong>om() function for each vertex (see Figure 9-13):<br />

// Simple Quad<br />

size(300, 300);<br />

background(0);<br />

noStroke();<br />

smooth();<br />

// quad(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y)<br />

quad(50+r<strong>and</strong>om(-25, 25), 50+r<strong>and</strong>om(-25, 25), ➥<br />

250+r<strong>and</strong>om(-25, 25), 50+r<strong>and</strong>om(-25, 25), ➥<br />

250+r<strong>and</strong>om(-25, 25), 250+r<strong>and</strong>om(-25, 25), ➥<br />

50+r<strong>and</strong>om(-25, 25), 250+r<strong>and</strong>om(-25, 25));

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

Saved successfully!

Ooh no, something went wrong!