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

534<br />

newX[0] = cos(theta)*x[0] - sin(theta)*y[0];<br />

newY[0] = cos(theta)*y[0] + sin(theta)*x[0];<br />

newX[1] = cos(theta)*x[1] - sin(theta)*y[1];<br />

newY[1] = cos(theta)*y[1] + sin(theta)*x[1];<br />

line(newX[0], newY[0], newX[1], newY[1]);<br />

// to rotate<br />

if (mousePressed){<br />

theta += radians(1);<br />

}<br />

}<br />

If you’re curious how the two forms of the trig expressions relate, I’ll clue you<br />

in. Starting with the long form of the expressions<br />

new x = cos(theta)*x - sin(theta)*y<br />

new y = cos(theta)*y + sin(theta)*x<br />

assume the angle of rotation starts at 0, which will make x equal to the radius<br />

(on the unit circle) <strong>and</strong> y equal to 0. Next, plug these values for x <strong>and</strong> y into the<br />

longer form, giving you the following:<br />

new x = cos(theta)*radius - sin(theta)*0<br />

new y = cos(theta)*0 + sin(theta)*radius<br />

If you then factor out the 0 values, you’re left with the familiar short form:<br />

new x = cos(theta)*radius<br />

new y = sin(theta)*radius<br />

To learn more, check out Kirupa Chinnathambi’s fabulous site at www.kirupa.<br />

com/developer/actionscript/trig_multiple_axis.htm.<br />

Notice in the last example that I used the short form of the trig expressions in the setup()<br />

function to calculate the initial coordinate values for the line, which was rotated with a<br />

r<strong>and</strong>om angle between 0 <strong>and</strong> 2pi. I then used the longer form of the expressions in the<br />

draw() function to further rotate the line based on the line’s current coordinate locations;<br />

using the longer form allowed for a smooth transition between the rotations. One final<br />

note about these expressions: By changing the value of the signs in the expression, you can<br />

reverse the direction of the rotation. Thus, the expressions<br />

new x = cos(theta)*x - sin(theta)*y<br />

new y = cos(theta)*y + sin(theta)*x<br />

rotate clockwise, but the expressions<br />

new x = cos(theta)*x + sin(theta)*y<br />

new y = cos(theta)*y - sin(theta)*x<br />

rotate counterclockwise.

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

Saved successfully!

Ooh no, something went wrong!