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.

}<br />

void setLocation(float tempX, float tempY) {<br />

x = tempX;<br />

y = tempY;<br />

}<br />

void display() {<br />

stroke(0);<br />

fill(col);<br />

ellipse(x,y,r*2,r*2);<br />

}<br />

Algorithms 187<br />

// A function that returns true or false based if the catcher intersects a raindrop<br />

boolean intersect(Drop d) {<br />

float distance = dist(x,y,d.x,d.y); // Calculate distance<br />

if (distance < r + d.r) { // Compare distance <strong>to</strong> sum of radii<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

class Drop {<br />

float x,y; // Variables for location of raindrop<br />

float speed; // Speed of raindrop<br />

color c;<br />

float r; // Radius of raindrop<br />

Drop() {<br />

r = 8; // All raindrops are the same size<br />

x = random(width); // Start with a random x location<br />

y = -r*4; // Start a little above the window<br />

speed = random(1,5); // Pick a random speed<br />

c = color(50,100,150); // Color<br />

}<br />

// Move the raindrop down<br />

void move() {<br />

y + = speed; // Increment by speed<br />

}<br />

// Check if it hits the bot<strong>to</strong>m<br />

boolean reachedBot<strong>to</strong>m() {<br />

if (y > height + r*4) { // If we go a little beyond the bot<strong>to</strong>m<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

// Display the raindrop<br />

void display() {<br />

// Display the drop<br />

fill(c);

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

Saved successfully!

Ooh no, something went wrong!