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

192<br />

There is one new concept in this example <strong>and</strong> a little mathematical expression that may<br />

need some clarification. The expression that might be problematic is the following:<br />

float val = cellWidth*(255.0/width);<br />

I need the decrementation of the color value to coincide with the number of steps in the<br />

for loop. To ensure that this happens, I need to find the ratio between the width of<br />

the display window <strong>and</strong> the maximum value of the color (255). I simply multiply this ratio<br />

by the cellWidth interval to calculate how much to decrement the color value each step<br />

of the loop. This gives me a smooth transition from 255 to 0 (or white to black). The new<br />

concept I mentioned refers to using multiple statements within the head of the for loop,<br />

separated by commas. This is a convenient thing to do. I could also have initialized a variable<br />

outside of the loop <strong>and</strong> explicitly incremented/decremented it within the loop block,<br />

but that’s more work. In truth, all three parts in the head of the for loop are optional (you<br />

do need the semicolons, though). For example, this for loop will compile <strong>and</strong> run fine. I<br />

used the break; comm<strong>and</strong> so that the loop doesn’t run infinitely.<br />

for(;;){<br />

print("i'm in this weird for loop");<br />

break;<br />

}<br />

In the next modification, I applied the value shifting to both the vertical <strong>and</strong> horizontal<br />

lines (see Figure 6-17). I also broke out of the square format to illustrate how the grid can<br />

automatically size itself to the display window size/format. This is a good example of<br />

why you want to try to base program measurements (if it’s at all practical) on the dimensions<br />

of the display window, which allows your program to scale <strong>and</strong> adapt to different<br />

environments.<br />

size(500, 200);<br />

background(0);<br />

float cellWidth = width/50.0;<br />

// find ratio of value range(255) to width<br />

float valw = cellWidth*(255.0/width);<br />

//vertical lines<br />

for (float i=cellWidth, v=255; i

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

Saved successfully!

Ooh no, something went wrong!