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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

216 <strong>Learning</strong> <strong>Processing</strong><br />

Exercise 13-7: Use the sine function <strong>to</strong> create a “ breathing ” shape, that is, one whose size oscillates.<br />

We can also produce some interesting results by drawing a sequence of shapes along the path of the sine<br />

function. See Example 13–7 .<br />

13.10<br />

Example 13-7: Wave<br />

// Starting angle<br />

float theta = 0.0;<br />

void setup() {<br />

size(200,200);<br />

smooth();<br />

}<br />

void draw() {<br />

background(255);<br />

// Increment theta (try different values for " angular velocity " here)<br />

theta + = 0.02;<br />

noStroke();<br />

fill(0);<br />

float x = theta;<br />

// A simple way <strong>to</strong> draw the wave with an ellipse at each location<br />

for (int i = 0; i < = 20; i + + ) {<br />

// Calculate y value based off of sine function<br />

float y = sin(x)*height/2;<br />

// Draw an ellipse<br />

ellipse(i*10,y + height/2,16,16);<br />

// Move along x-axis<br />

x + = 0.2;<br />

}<br />

}<br />

Exercise 13-8: Rewrite the above example <strong>to</strong> use the noise( ) function instead of sin( ) .<br />

Recursion<br />

fi g. 13.14 The Mandelbrot set:<br />

http://processing.org/learning/<strong>to</strong>pics/mandelbrot.html<br />

fi g. 13.13<br />

A for loop is used <strong>to</strong> draw all the points along a sine<br />

wave (scaled <strong>to</strong> the pixel dimension of the window).

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

Saved successfully!

Ooh no, something went wrong!