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 10-19. Wave Gradient sketch<br />

Hopefully this last sketch didn’t cook your computer. Between the two nested loops,<br />

there’s a fair amount of processing. The gradient is determined by a simple sine wave with<br />

increasing frequency; this is what gives the gradient its asymmetry. The color is determined,<br />

somewhat arbitrarily, by some simple expressions. The red component is generated<br />

by the sine function, the green component is based on 255 (minus the value of the sine<br />

function), <strong>and</strong> the blue component is determined by the counter j. In all three cases, the<br />

component color values calculated by the expressions are remapped to a range of 0 to<br />

255. Definitely mess around with some of the values, especially the color expressions <strong>and</strong><br />

the frequency. At the bottom of the function, the third for loop fills gaps in the gradient,<br />

similarly to what was done in the radial gradient sketch. You can also try changing the<br />

fillGap variable to see what happens. Using the set() function is relatively straightforward,<br />

but not the fastest way to change pixel data in <strong>Processing</strong>.<br />

Faster pixel functions<br />

There are three other language structures (two functions <strong>and</strong> an array) that can speed<br />

things up. The three structures are loadPixels(), updatePixels(), <strong>and</strong> pixels[]. The<br />

first structure, loadPixels(), creates an array of pixels (assigned to the built-in global variable<br />

pixels[]) of the entire display window. The number of pixels in the display window<br />

can be calculated by width * height. So, for example, a 400 ✕ 400 display would equal<br />

160,000 pixels. Here’s some evidence:<br />

size(400, 400);<br />

loadPixels();<br />

print(pixels.length);<br />

COLOR AND IMAGING<br />

429<br />

10

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

Saved successfully!

Ooh no, something went wrong!