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

Create successful ePaper yourself

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

are filled with black. Using the intialNode variable allows the first plotted node’s color to<br />

be changed when the mouse is over it, providing some interactive direction to the user.<br />

// function returns true/false if mouse is over initial node<br />

boolean isInitialNode(){<br />

boolean isOnNode = false;<br />

if (nodeXPos.length>2){<br />

if (dist(mouseX, mouseY, nodeXPos[0], nodeYPos[0]) < nodeRadius){<br />

isOnNode = true;<br />

}<br />

}<br />

return isOnNode;<br />

}<br />

The isInitialNode() function returns true if there are more than two nodes plotted <strong>and</strong><br />

the mouse is over the initially plotted node. If neither of these conditions is true, the function<br />

returns false. Notice how I declared the function using the boolean keyword in front<br />

of the function name; this enforces that the function must return a Boolean value. Note<br />

also that the Boolean variable isOnNode, which I declared within the function, only exists<br />

locally (has local scope) within the function.<br />

void mousePressed(){<br />

if(isShapeClosed){<br />

// after shape is closed empty arrays<br />

nodeXPos = subset(nodeXPos, 0, 0);<br />

nodeYPos = subset(nodeYPos, 0, 0);<br />

xSpeed = subset(xSpeed, 0, 0);<br />

ySpeed = subset(ySpeed, 0, 0);<br />

jitter = subset(jitter, 0, 0);<br />

elasticity = subset(elasticity, 0, 0);<br />

//reset variable to false<br />

isShapeClosed = false;<br />

}<br />

if(!isInitialNode()){<br />

// if not on initial node plot at mouse position<br />

// increase size of node coordinate arrays<br />

nodeXPos = append(nodeXPos, mouseX);<br />

nodeYPos = append(nodeYPos, mouseY);<br />

// increase size of dynamics arrays<br />

xSpeed = append(xSpeed, 0);<br />

jitter = append(jitter, r<strong>and</strong>om(-jitterRange, jitterRange));<br />

elasticity = append(elasticity, r<strong>and</strong>om(elasticRangeMin, ➥<br />

elasticRangeMax));<br />

ySpeed = append(ySpeed, 0);<br />

}<br />

else {<br />

// set to true to avoid additional nodes on shape<br />

INTERACTIVITY<br />

577<br />

12

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

Saved successfully!

Ooh no, something went wrong!