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

180<br />

size(300, 300);<br />

background(0);<br />

int totalPts = 300;<br />

float steps = totalPts+1;<br />

stroke(255);<br />

for (int i=1; i< steps; i++){<br />

point((width/steps)*i, (height/2)+r<strong>and</strong>om(-2, 2));<br />

}<br />

Figure 6-6. R<strong>and</strong>omized point line<br />

The function r<strong>and</strong>om(-2, 2) generates a r<strong>and</strong>om number between the range specified by<br />

the min <strong>and</strong> max arguments passed to the function. Try passing larger <strong>and</strong> smaller <strong>and</strong> also<br />

asymmetrical arguments to the function to see what happens. Also try changing the number<br />

of points. This flexibility <strong>and</strong> ease with which you can experiment <strong>and</strong> iterate through<br />

different possibilities is what creative coding is all about. It’s also the reason it’s worth taking<br />

the time to code your projects efficiently, using well-structured loops <strong>and</strong> a minimal<br />

amount of magic numbers. Perhaps you’d like the r<strong>and</strong>om effect to change over time as<br />

well so that the line is not so consistent. Since the r<strong>and</strong>om() function is just looking for two<br />

float arguments, you can pass those as variables. For example, the following generates a<br />

particle spray (shown in Figure 6-7). Notice I added another variable called r<strong>and</strong>.<br />

size(300, 300);<br />

background(0);<br />

int totalPts = 300;<br />

float steps = totalPts+1;<br />

stroke(255);<br />

float r<strong>and</strong> = 0;<br />

for (int i=1; i< steps; i++){<br />

point((width/steps)*i, (height/2)+r<strong>and</strong>om(-r<strong>and</strong>, r<strong>and</strong>));<br />

r<strong>and</strong>+=.2;<br />

}

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

Saved successfully!

Ooh no, something went wrong!