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

426<br />

The Simple Linear Gradient example uses a custom setGradient() function that allows<br />

you to specify the size, position, colors, <strong>and</strong> axis of the gradient within the display window.<br />

The nested for loops in the function may look a little confusing. The math stuff is simpler<br />

than it looks. Initially in the function, the three local variables deltaR, deltaG, <strong>and</strong> deltaB<br />

are assigned the differences between the R, G, <strong>and</strong> B components, respectively, within the<br />

two colors. The rest of the problem involves iterating each component from color c1 to<br />

color c2 evenly in the for loops. By dividing the delta values by the axis dimension, I get<br />

the ratio of the delta to the height, which I then multiply by the counter j. This way, even<br />

though j is incremented by 1 in the loop, the ratio will translate to the appropriate value<br />

for the color component, creating a smooth, continuous gradient. Also, the reason I subtract<br />

the y <strong>and</strong> x from j in both loop options is to ensure that the beginning of the gradient<br />

begins with c1, not c1 plus the value of x or y. As always, play with this sketch <strong>and</strong><br />

change things. I also recommend calling some println() comm<strong>and</strong>s <strong>and</strong> checking on the<br />

values of variables during the for loops. When I debug my programs (which takes up too<br />

much of my time), I am constantly calling println() with a variable name as an argument<br />

to see how the program is working.<br />

Next, I’ll create the radial gradient shown in Figure 10-18. (Please be patient—this sketch<br />

can take some time to render.)<br />

// Simple Radial Gradient<br />

// January 12, 2006<br />

void setup(){<br />

size(400, 400);<br />

background(0);<br />

smooth();<br />

// create a simple table of gradients<br />

int columns = 4;<br />

int radius = (width/columns)/2;<br />

// create some gradients<br />

for (int i=radius; i

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

Saved successfully!

Ooh no, something went wrong!