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

184<br />

variable i in all your non-nested for loops. However, when you nest a loop, the inner loop<br />

sees all of the variables you created in the outer loop; that’s why you’re forced to use a<br />

new name for the loop variable j in the inner loop. If you’ve been following this scope<br />

logic, do you think the outer loop can see the variable declared in the inner loop? The<br />

answer is no because the rules of local scope still apply.<br />

Getting back to the code example, within the inner loop are a few lines that might be<br />

confusing:<br />

rowHop-=rowNudge;<br />

if (j % (1+ (int)(r<strong>and</strong>om(r<strong>and</strong>Nudge))) == 0){<br />

rowNudge*=-1;<br />

}<br />

The first line is a st<strong>and</strong>ard shortcut assignment operation. The variable rowHop moves the<br />

points up <strong>and</strong> down on the y-axis, <strong>and</strong> rowNudge is the value that it’s either incremented or<br />

decremented by. I use the term “shortcut” because this operation does both an arithmetic<br />

operation (subtraction) <strong>and</strong> an assignment operation. The next line uses the modulus<br />

operator, which is usually foreign to non-coders. Modulus returns the remainder of division<br />

between two oper<strong>and</strong>s. The reason I used it was to generate a somewhat r<strong>and</strong>om<br />

result in the loop. I tested for the modulus expression to evaluate to 0, meaning that the<br />

division would yield no remainder. As i increases, there is a greater possibility that division<br />

will have no remainder. For example, 4 has three factors (1, 2, <strong>and</strong> 4) that go into it evenly,<br />

but 12 has six (1, 2, 3, 4, 6, <strong>and</strong> 12). In addition, I used a r<strong>and</strong>om number generator for the<br />

right oper<strong>and</strong> to make the process more chaotic. The reason I added 1 to the value of the<br />

r<strong>and</strong>om() function before doing the modulus operation was to avoid division by 0, which<br />

is illegal <strong>and</strong> will cause a compiler error. Since the r<strong>and</strong>om() function will generate a r<strong>and</strong>om<br />

value between 0 <strong>and</strong> the argument value, periodically 0 will come up. In addition, I<br />

am converting the float value, evaluated by the r<strong>and</strong>om() function, to an integer, so the<br />

modulus expression has a chance of evaluating to 0. When you convert a float to an int<br />

by using the int() function, values are rounded down, so .999 still evaluates to 0. Thus,<br />

the statement (int)(r<strong>and</strong>om(r<strong>and</strong>Nudge)) can generate a lot of zeros. I used parentheses<br />

around the entire expression (1 + (int)(r<strong>and</strong>om(r<strong>and</strong>Nudge))) to make sure that the<br />

addition happens before the division. Normally multiplication <strong>and</strong> division, including modulus,<br />

are evaluated before addition <strong>and</strong> subtraction, but you can use parentheses to<br />

change the order of precedence, which is discussed at more length in Chapter 3. Finally,<br />

the shortcut assignment operation rowNudge*=-1; switches the direction in which the<br />

statement rowHop-=rowNudge; is pushing the points on the y-axis, giving a razor-tooth pattern<br />

to the line output. Hopefully this description wasn’t too rough on the head. Make<br />

sure you play, tweak, experiment, <strong>and</strong> bust apart the code to get a better sense of what’s<br />

going on.<br />

There are two more modifications I’ll make to the current sketch, <strong>and</strong> then I’ll (finally)<br />

move on to using <strong>Processing</strong>’s “real” line functions. The first modification involves laying a<br />

more regular grid pattern on top of the razor-tooth pattern, <strong>and</strong> the second modification<br />

involves shifting value.

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

Saved successfully!

Ooh no, something went wrong!