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

224<br />

The new function calls I used, min() <strong>and</strong> max(), are stuck in the head of the for loop:<br />

for (int i=min(width, height); i>min(width, height)/2; i-=max(wStep,<br />

hStep)){...<br />

The min() <strong>and</strong> max() functions each take two arguments <strong>and</strong> spit back the lower or higher<br />

value, respectively. I used them to ensure that the maze was drawn correctly, regardless of<br />

what the display window or wStep <strong>and</strong> hStep values were set to. The problem they solve is<br />

correcting for potential asymmetrical values assigned to the aforementioned variables. For<br />

example, if the width <strong>and</strong> height arguments in the size() function were set to 600 <strong>and</strong><br />

400, my loop wouldn’t be able to adjust itself if I were using width to control the loop<br />

limit. I would always need the smaller dimension to be the loop limit for the autoadjustment<br />

to work. Since (theoretically) users may sometimes make the width argument<br />

bigger than height, or vice versa, there is no way to hard-code a value as the loop limit. By<br />

using the min() <strong>and</strong> max() functions, the loop automatically figures out the limiting variable<br />

(either the minimum value of either width or height, or the maximum value of wStep<br />

or hStep) <strong>and</strong> adjusts itself appropriately. This is a hard concept to explain in words, so I<br />

suggest playing with the values for width, height, wStep, <strong>and</strong> hStep. You can also try substituting<br />

the following for loop head into the sketch:<br />

for (int i=width; i>width/2; i-=wStep)<br />

while continuing to change all four values. You may notice that the substitute loop head<br />

creates less ordered but more interesting output.<br />

Next I created a tiled version of the maze (see Figure 6-37). The sketch puts together a<br />

bunch of concepts discussed thus far in this chapter. There’s a lot more that could be done<br />

with this sketch—for example, adding the scribble() function or additional r<strong>and</strong>om()<br />

functions. You could also add another maze pattern algorithm in the createMaze() function.<br />

I’ve included comments that show you what variables you can safely change. (Of<br />

course, safety aside, it‘s also helpful to sometimes break the code to get a better sense of<br />

how it works.)<br />

/*<br />

Maze Tile 1<br />

Ira Greenberg, November 25, 2005<br />

revised October 18, 2006<br />

*/<br />

// declare some vars<br />

float x, y, y2, x2, h, w, xShift, yShift;<br />

//these values can be changed<br />

int xSclFactor = 3;<br />

int ySclFactor = 3;<br />

float wStep = 4;<br />

float hStep = 4;

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

Saved successfully!

Ooh no, something went wrong!