04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

522<br />

Returning to the original speed settings, speedX = 2, speedY = 2.5, what could you do if<br />

you wanted to double the speed of the object, but not alter its direction? You could multiply<br />

both values by 2. This would cause the object to move faster, but still along the same<br />

path. So what happened? I know this may seem pathetically simple, but the underlying<br />

concepts are very important for what we’ll be doing (<strong>and</strong> not as simple as they may initially<br />

seem).<br />

When you double the speed settings, the object’s speed obviously increases, but the ratio<br />

of the two speeds remains the same, keeping the object moving in the same direction. This<br />

ratio relates to the slope of the line defining the movement of the object. You may recall<br />

that slope is calculated as the change in y (∆y) over the change in x (∆x)—or the rise over<br />

the run.<br />

Thus, speed <strong>and</strong> direction can be thought of as two separate quantities; you can alter an<br />

object’s speed while still maintaining its current direction. (It would be pretty hard to drive<br />

a car safely if we couldn’t do this.) Mathematically, there is a convenient way of expressing<br />

all this, namely in a structure called a vector.<br />

I’ve mentioned vectors earlier in the book, <strong>and</strong> we’ll delve deeper into them in the final<br />

chapters (on 3D) as well. A vector, for our current purposes, is a quantity that describes<br />

both speed <strong>and</strong> direction, more commonly known as velocity.<br />

In the next example, the two parts of the vector (direction <strong>and</strong> speed) are coded as separate<br />

variables, to help illustrate their relationship. The sketch moves five rectangles in the<br />

same direction, but at differing speeds.<br />

float directionX = .56, directionY = .83;<br />

float[] speeds = {.75, 1.3, 2.2, .92, 1.5};<br />

float[] x = new float[5], y = new float[5];<br />

void setup(){<br />

size(400, 400);<br />

noStroke();<br />

fill(128);<br />

smooth();<br />

}<br />

void draw(){<br />

background(255);<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!