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.

By using functions to organize a program, you get the benefit of modularity, but also the<br />

burden of added abstraction. It doesn’t help that so many of the parameters are index<br />

positions in arrays. However, with a sketch this complex, you need ways of cutting down<br />

on the variable clutter. Another, <strong>and</strong> better, way to code this sketch would be using an<br />

object-oriented approach. This would allow for even greater modularity <strong>and</strong> potential<br />

code reuse. For example, I could create a class for each of the main elements in the<br />

sketch: Particle, Emitter, Dynamics, Wave, World, <strong>and</strong> so forth. This would allow me to<br />

really encapsulate the functionality of each of these constructs. I would also design the<br />

relationships between these different classes. Of course, this approach also brings lots of<br />

added abstraction to the process, so it’s not the best approach for new coders. Later in the<br />

book, when you’re an old, salty hackster, I’ll approach some problems using objectoriented<br />

programming (OOP). Before moving on, though, I want go over a few elements in<br />

the last particle wave example.<br />

I avoided global variables <strong>and</strong> declared local variables within the functions. This encourages<br />

code reuse, as each function is a self-contained processing unit. To use the function,<br />

you just need to know the parameter list, or the interface to the function. The parameter<br />

list shows the input that the function requires to do its thing. The return type of the function<br />

shows what value, if any, the function returns when it is called. For example, the float<br />

makeWaves() function returns a float value every time it’s called. Returning a value should<br />

not be confused with outputting something to the display window. The plot() function,<br />

for example, outputs points to the screen, but the function doesn’t return any value when<br />

it is called. Another benefit of using all local variables defined within functions is that there<br />

is no danger of name collisions. I can use the same named variables in each function without<br />

any ambiguity because each variable has local scope. If I need the value of a variable<br />

outside the function, I need to pass it as an argument, as I do throughout the program.<br />

I used a bunch of convenience arrays in the setup() function. I use the term “convenience”<br />

because I could have just passed the variables individually as arguments to the<br />

setParticles() function. However, the parameter list, defined in the head of the<br />

setParticles() function, would have needed to account for all these variables, including<br />

their type. This list would have included 17 parameters. Instead, by packaging the variables<br />

into logical arrays, I only needed to pass five arguments to the setParticles() function<br />

(which of course needed five parameters of the same type declared in the head of<br />

the function). The burden of this organizational abstraction is that once in the<br />

setParticles() function, I needed to, as the function designer, carefully decode what<br />

value each array index contained. However, since all the values required by the function<br />

are passed in as arguments, once completed, I theoretically never have to mess with the<br />

insides of this function again. If I want to change the output of the function, I simply<br />

change the values I pass to it—it’s a black box construction.<br />

One issue in this example that may be confusing to new coders is my use of function calls<br />

within other functions, especially with regard to using a returned value in an assignment.<br />

For example, from within the setParticles() function, I call the makeWaves() function:<br />

// called from within the setParticles() function<br />

x[i]=makeWaves(x[i], angle+=frequency[i], amplitude[i]);<br />

Try not to get confused by all the array notation (all the [i]s) in the function. Everything<br />

is efficiently getting processed in a for loop, <strong>and</strong> each of the variables is a primitive array;<br />

CURVES<br />

261<br />

7

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

Saved successfully!

Ooh no, something went wrong!