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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

176<br />

Figure 6-4. Emerging line sketch<br />

This is beginning to look like a real program, <strong>and</strong> it also has an insistent line. There is<br />

something simple <strong>and</strong> beautiful about the sharpness <strong>and</strong> delicateness of the white points<br />

against the black. You’ll notice that I called stroke(255), which sets the stroke state of<br />

the sketch to white. I use the term “stroke state” because any drawing executed after the<br />

stroke(255) call that involves rendering a stroke will be white, until I explicitly make<br />

another call to change the rendering state of the stroke color—for example,<br />

stroke(127);. I discuss the idea of a rendering state in the color section of the language<br />

reference, found in Appendix A. The sketch is now 12 lines (of code) long <strong>and</strong> beginning<br />

to get a little unwieldy, especially for making global changes to the program. For example,<br />

if you want to now add a tenth point <strong>and</strong> still keep the line centered in the display window,<br />

it’s time-consuming (<strong>and</strong> annoying) to have to go <strong>and</strong> change the value for each<br />

point. There are two ways to simplify this process. The first is to base your point positions<br />

on the size <strong>and</strong> shape of the display window, <strong>and</strong> the second is to add a looping structure<br />

that will h<strong>and</strong>le the iteration for you.<br />

Here’s a solution for using ten points, implementing the first improvement:<br />

size(300, 300);<br />

background(0);<br />

stroke(255);<br />

point((width/11)*1, height/2);<br />

point((width/11)*2, height/2);<br />

point((width/11)*3, height/2);<br />

point((width/11)*4, height/2);<br />

point((width/11)*5, height/2);<br />

point((width/11)*6, height/2);<br />

point((width/11)*7, height/2);<br />

point((width/11)*8, height/2);<br />

point((width/11)*9, height/2);<br />

point((width/11)*10, height/2);<br />

This solution is procedurally an improvement—it uses the computer to h<strong>and</strong>le the calculation<br />

of the division by 11. Next, I’ll show you how to implement a while loop.

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

Saved successfully!

Ooh no, something went wrong!