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.

14.6<br />

Example 14-5: Rectangle rotating around center<br />

void setup() {<br />

size(200,200);<br />

}<br />

void draw() {<br />

background(255);<br />

stroke(0);<br />

fill(175);<br />

// Translate origin <strong>to</strong> center<br />

translate(width/2,height/2);<br />

// theta is a common name of a variable <strong>to</strong> s<strong>to</strong>re an angle<br />

float theta = PI*mouseX / width;<br />

// Rotate by the angle theta<br />

rotate(theta);<br />

// Display rectangle with CENTER mode<br />

rectMode(CENTER);<br />

rect(0,0,100,100);<br />

}<br />

Translation and Rotation (in 3D!) 239<br />

fi g. 14.15<br />

The angle ranges from 0 <strong>to</strong> PI, based on the ratio<br />

of mouseX location <strong>to</strong> the sketch’s width.<br />

Exercise 14-6: Create a line that spins around its center (like twirling a ba<strong>to</strong>n). Draw a<br />

circle at both endpoints.<br />

Rotation Around Different Axes<br />

Now that we have basic rotation out of the way, we can begin<br />

<strong>to</strong> ask the next important rotation question:<br />

Around what axis do we want <strong>to</strong> rotate?<br />

In the previous section, our square rotated around the Z -axis.<br />

Th is is the default axis for two-dimensional rotation. See<br />

Figure 14.16 .<br />

fi g. 14.16<br />

Z-axis<br />

<strong>Processing</strong> window

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

Saved successfully!

Ooh no, something went wrong!