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.

214 <strong>Learning</strong> <strong>Processing</strong><br />

13.9<br />

// Draw an ellipse at x,y<br />

noStroke();<br />

fill(0);<br />

ellipse(x + width/2, y + height/2, 16, 16); // Adjust for center of window<br />

// Increment the angle<br />

theta + = 0.01;<br />

}<br />

Exercise 13-5: Using Example 13-5, draw a spiral path. Start in the center and move<br />

outward. Note that this can be done by changing only one line of code and adding one line<br />

of code!<br />

Oscillation<br />

Trigonometric functions can be used for more than geometric calculations associated with right triangles.<br />

Let’s take a look at Figure 13.11, a graph of the sine function where y � sin( x ).<br />

fi g. 13.11<br />

4<br />

2<br />

0<br />

�1π 0 1π<br />

2π<br />

�2<br />

�4<br />

y<br />

You will notice that the output of sine is a smooth curve alternating between –1 and 1 . Th is type of<br />

behavior is known as oscillation , a periodic movement between two points. A swinging pendulum, for<br />

example, oscillates.<br />

We can simulate oscillation in a <strong>Processing</strong> sketch by assigning the output of the sine function <strong>to</strong> an<br />

object’s location. Th is is similar <strong>to</strong> how we used noise( ) <strong>to</strong> control the size of a circle (see Example 13-4),<br />

only with sin( ) controlling a location. Note that while noise( ) produces a number between 0 and 1.0, sin( )<br />

outputs a range between –1 and 1. Example 13-6 shows the code for an oscillating pendulum .<br />

x

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

Saved successfully!

Ooh no, something went wrong!