06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

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.

fi g. 13.9<br />

y-axis<br />

sine(theta) � y/r → y � r * sine(theta)<br />

cosine(theta) � x/r → x � r * cosine(theta)<br />

r<br />

θ<br />

x<br />

Polar coordinate (r,θ)<br />

Cartesian coordinate (x,y)<br />

y sin(θ) = y/r<br />

cos(θ) = x/r<br />

x-axis<br />

Mathematics 213<br />

For example, if r is 75 and theta is 45 ° (or PI/4 radians), we can calculate x and y as follows. Th e functions<br />

for sine and cosine in <strong>Processing</strong> are sin( ) and cos( ) , respectively. Th ey each take one argument, a fl oating<br />

point angle measured in radians.<br />

float r = 75;<br />

float theta = PI / 4; // We could also say: float theta = radians(45);<br />

float x = r * cos(theta);<br />

float y = r * sin(theta);<br />

Th is type of conversion can be useful in certain applications. For example, how would you move a shape<br />

along a circular path using Cartesian coordinates? It would be <strong>to</strong>ugh. Using polar coordinates, however,<br />

this task is easy. Simply increment the angle!<br />

Here is how it is done with global variables r and theta .<br />

Example 13-5: Polar <strong>to</strong> Cartesian<br />

// A Polar coordinate<br />

float r = 75;<br />

float theta = 0;<br />

void setup() {<br />

size(200,200);<br />

background(255);<br />

smooth();<br />

}<br />

Polar coordinates<br />

(r, theta) are<br />

converted <strong>to</strong><br />

Cartesian (x,y)<br />

for use in the<br />

ellipse() function.<br />

void draw() {<br />

// Polar <strong>to</strong> Cartesian conversion<br />

float x = r * cos(theta);<br />

float y = r * sin(theta);<br />

The Greek letter θ (theta) is often<br />

used as a symbol for an angle.<br />

fi g. 13.10

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

Saved successfully!

Ooh no, something went wrong!