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

504<br />

Figure 11-11. Ravenous Triangle I sketch<br />

I used the basic unit circle relationships cos(theta) = x <strong>and</strong> sin(theta) = y to plot the triangle<br />

in the createPredatoryTriangle() function. The variables predCntrX <strong>and</strong> predCntrY<br />

give the center point of the triangle, <strong>and</strong> the variable predLen is the length of each of the<br />

triangles’ three vertices from the center point. I’ve used these unit circle relationships to<br />

plot polygons throughout the book, so hopefully this is a review.<br />

To get the triangle to rotate <strong>and</strong> stay oriented, I used <strong>Processing</strong>’s atan2(y, x) function.<br />

The <strong>Processing</strong> documentation says this about the function: “Calculates the angle (in radians)<br />

from a specified point to the coordinate origin as measured from the positive x-axis.”<br />

(See http://processing.org/reference/atan2_.html.)<br />

Don’t worry if this doesn’t help much. The “atan” part of the function name refers to arctangent.<br />

Normally, using trigonometry (e.g., sine, cosine, <strong>and</strong> tangent), we begin with an<br />

angle <strong>and</strong> we can calculate the ratio of two sides of a right triangle. The arc versions of the<br />

trig functions are used when we know the ratio of two of the sides <strong>and</strong> we want to find<br />

the angle between them. In the sketch, deltaY <strong>and</strong> deltaX (representing the change in y<br />

<strong>and</strong> the change in x) provide the ratio of two sides of a right angle, which I plugged into<br />

the atan2(deltaY, deltaX) function to get the angle I need, like so:<br />

predRot = atan2(deltaY, deltaX);<br />

I then added predRot (which is already in radians) directly into the two trig equations that<br />

plotted the triangle:<br />

predX[i] = predCntrX+cos(radians(predAng)+predRot)*predLen;<br />

predY[i] = predCntrY+sin(radians(predAng)+predRot)*predLen;

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

Saved successfully!

Ooh no, something went wrong!