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.

vertex(width/2, 3);<br />

vertex(width-margin, margin);<br />

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

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

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

vertex(margin, height-margin);<br />

endShape(CLOSE);<br />

Figure 9-16. Octagon sketch<br />

The default beginShape() mode is a filled but open shape. By adding the CLOSE argument<br />

to the endShape() call, the shape is closed (by connecting the last vertex to the initial vertex),<br />

as illustrated in the Octagon example. Plotting the octagon wasn’t too difficult<br />

because an 8-sided regular shape on a 4-sided window boils down to plotting vertices at<br />

the corners <strong>and</strong> midpoints of the display window—but what about switching to a pentagon<br />

or heptagon (7 sides), or even an enneakaidecagon (a 19-sided polygon)? Using a little<br />

trig, you can very easily create a general-purpose polygon creator. In the following<br />

example, I’ve parameterized lots of details to make the makePoly() function useful. I’ve<br />

also called this function a bunch of times in the sketch to showcase the range of images it<br />

can create (see Figure 9-17).<br />

/*<br />

Polygon Creator<br />

Ira Greenberg, December 26, 2005<br />

*/<br />

void setup(){<br />

size(600, 600);<br />

background(127);<br />

smooth();<br />

/*<br />

SHAPES<br />

359<br />

9

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

Saved successfully!

Ooh no, something went wrong!