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.

else {<br />

}<br />

// extra line needed to attach line back to point- ➥<br />

not necessary in HATCHING style<br />

line(x1, y1, x2, y2); //try commenting this line out<br />

The HATCHING style doesn’t require this fix. Finally, you’ll notice that I use the same variable<br />

name, steps, for the local variable in the makeRect() function <strong>and</strong> the parameter in<br />

the scribble() function. I can do this because the steps variable is declared within the<br />

makeRect() function, not at the top of the sketch. Therefore, it is local in scope to<br />

the makeRect() function <strong>and</strong> not visible anywhere else. Thus, I can use the same-named<br />

variable in both functions without any conflict. Of course, you can use unique names if<br />

that seems simpler to you. Personally, I find it hard to keep coming up with new related<br />

names, so I tend to reuse local variable names.<br />

Well, enough of this low-level point recording business—let’s luxuriate through the rest of<br />

this chapter using <strong>Processing</strong>’s user-friendly vertex() function.<br />

Applying the vertex function<br />

The vertex() function is called between the beginShape() <strong>and</strong> endShape() functions,<br />

which internally take care of the point recording <strong>and</strong> rendering just discussed. You’ve<br />

looked at the POINTS mode already. The LINES mode works similarly, except instead of<br />

rendering points, vertices are connected by lines in groups of two. For example, the<br />

following code generates a horizontal line:<br />

size(300, 300);<br />

background(255);<br />

beginShape(LINES);<br />

vertex(20, height/2);<br />

vertex(width-20, height/2);<br />

endShape();<br />

Since the lines are laid down in groups of two vertices, adding a third vertex() function<br />

call will have no effect on the output:<br />

size(300, 300);<br />

background(255);<br />

beginShape(LINES);<br />

vertex(20, height/2);<br />

vertex(width-20, height/2);<br />

vertex(width/2, height-20); //will have no effect<br />

endShape();<br />

LINES<br />

219<br />

6

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

Saved successfully!

Ooh no, something went wrong!