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.

Figure 6-27. Table Layout II sketch<br />

This was a pretty brute-force <strong>and</strong> inefficient way to add r<strong>and</strong>omization to the algorithm,<br />

<strong>and</strong> you should notice all the magic numbers lurking in the line() function calls (yuck).<br />

But running the sketch, you’ll see that things are beginning to get more interesting.<br />

Wouldn’t it be cool if you could do this to HTML table structures (on purpose)? I also<br />

added some more formatting to the code to make it more modular, such as the<br />

drawTable() function.<br />

When you add your own custom functions to <strong>Processing</strong>, you also need to include<br />

<strong>Processing</strong>’s setup() function. setup() is executed only once when a sketch launches, <strong>and</strong><br />

is a good place to initialize program variables. Since setup(), like all functions, is structured<br />

with external curly braces surrounding a main block, it follows the same rules of<br />

scope as I discussed with for loops—variables declared within the block are only visible<br />

from within the block (they have local scope). If you want variables to be accessible<br />

throughout the entire program, you need to declare them outside of the setup() function,<br />

at the top of the program.<br />

Although functions add another layer of abstraction to your code, they provide tremendous<br />

processing power, as they modularize a process, allowing it to be used like an independent<br />

machine or engine. By utilizing user-defined inputs (parameters) with a function,<br />

the same function can generate a wide range of output possibilities. In the next example, I<br />

added a number of parameters to the drawTable() function, greatly increasing the sketch’s<br />

expressive potential. Three examples of the sketch’s output are shown in Figures 6-28,<br />

6-29, <strong>and</strong> 6-30.<br />

LINES<br />

205<br />

6

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

Saved successfully!

Ooh no, something went wrong!