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

90<br />

print(street[i]);<br />

println(city[i]);<br />

print(state[i]);<br />

println(country[i]);<br />

println();<br />

}<br />

The sketch outputs the following:<br />

Ira Jed<br />

4 Happy Ln, Oxford<br />

Ohio, USA<br />

Sophie Rose<br />

19 Hunan Pl, Changsha<br />

Hunan, China<br />

Ian Isaac<br />

104 Hevi Hevi St, Easton<br />

Pennsylvania, USA<br />

Robin McLennan<br />

1000 Donkey Dr, Edinboro<br />

Scotl<strong>and</strong>, UK<br />

The for loop does all the heavy lifting by moving through the arrays, using the counter as<br />

the index to the position in the arrays. Also notice in this example that I alternated<br />

between print() <strong>and</strong> println() to format the address <strong>and</strong> line spacing correctly. Using a<br />

for loop to process four elements in each array may not seem too impressive. But imagine<br />

if each of the arrays included 10,000 elements. This simple sketch would have just as<br />

easily processed <strong>and</strong> output all that address data as well (of course, it would have taken a<br />

few more seconds). To better illustrate this, let’s look at a more visually interesting example.<br />

I’m going to take the previous bouncing ball animation <strong>and</strong> beef it up some, using<br />

arrays <strong>and</strong> a loop or two. I’ll also be incorporating one of <strong>Processing</strong>’s h<strong>and</strong>iest functions:<br />

r<strong>and</strong>om(value), which generates a r<strong>and</strong>om value between 0 <strong>and</strong> the value argument.<br />

This example is a little long—try not to panic. You’ll be looking at a lot of code like this<br />

throughout the book. I recommend running the sketch before reading through the code.<br />

You may even want to mess around with some of the values to see how it changes the<br />

output.<br />

Here’s the Many Bouncing Balls program (see Figure 3-5):<br />

/*<br />

title: Many Bouncing Balls<br />

description: balls deflect off sketch window edges<br />

created: August 9, 2005<br />

by: Ira Greenberg<br />

*/<br />

// global variables

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

Saved successfully!

Ooh no, something went wrong!