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 />

578<br />

isShapeClosed = true;<br />

}<br />

}<br />

The mousePressed() function begins with a conditional block that initializes the sketch<br />

arrays when the shape is closed; this allows new shapes to be plotted when the mouse is<br />

pressed (only after the shape has been closed <strong>and</strong> has begun animating). I used<br />

<strong>Processing</strong>’s subset() function to reset the primitive arrays. The function returns a subset<br />

of an array. The first argument is the array to subset, the second argument is the place in<br />

the array to begin the subset, <strong>and</strong> the third argument is the number of positions in the<br />

array to include in the subset. Since I used 0, 0 for the starting point <strong>and</strong> number of positions<br />

in the subset, an empty array was returned. Notice that I assigned the empty array<br />

back to the same array I used in the first argument; this may look odd, but it is perfectly<br />

legal. At the end of the conditional block, I reset the variable isShapeClosed to false to<br />

allow new nodes to be plotted.<br />

The next conditional block, if(!isInitialNode()), uses the ! operator with the function<br />

call isInitialNode(). The function call returns a Boolean value (true or false). Prefacing<br />

the function call with the exclamation mark means that the conditional test will evaluate<br />

to true when the function call returns false (i.e., when the user is not pressing on the initial<br />

node). This kind of logic can hurt your head. Instead of using !isInitialNode(), you<br />

could also use isInitialNode() == false or isInitialNode() != true to h<strong>and</strong>le the<br />

test.<br />

Within the conditional block, I used <strong>Processing</strong>’s append() function. This function<br />

increases the length of arrays. The function takes two arguments: the first argument is the<br />

array to append to, <strong>and</strong> the second is the value to add to the array. Like subset(),<br />

the append() function returns the changed array, which I reassigned to the same array<br />

variable. On each mouse press, all six of the arrays are appended to. Finally, if the user<br />

clicks the initial node, the arrays are not exp<strong>and</strong>ed, but the Boolean variable<br />

isShapeClosed is set to true.<br />

void moveShape(){<br />

//animate after shape is closed<br />

if (isShapeClosed){<br />

for (int i=0; iwidth-nodeRadius){<br />

nodeXPos[i] = width-nodeRadius;<br />

xSpeed[i]*=-1;<br />

}<br />

// left display window edge<br />

if (nodeXPos[i]

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

Saved successfully!

Ooh no, something went wrong!