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

430<br />

The pixels[] array stores each pixel as <strong>Processing</strong>’s color data type, structured in consecutive<br />

rows. For example, if the sketch size is 100 ✕ 100, then the 5,050th pixel<br />

(pixels[5049]) will be close to the center point in the display window. Here’s an example<br />

that directly changes the color of that pixel:<br />

size(100, 100);<br />

background(0);<br />

loadPixels();<br />

pixels[5050] = color(255);<br />

updatePixels();<br />

The <strong>Processing</strong> reference includes a very simple mathematical expression to help you target<br />

a pixel in the pixels[] array of the display window: pixels[y*width+x]. Based on this<br />

expression, the last little sketch (shown in Figure 10-20) could be rewritten as follows:<br />

size(100, 100);<br />

background(0);<br />

loadPixels();<br />

pixels[height/2*width+width/2] = color(255);<br />

updatePixels();<br />

Figure 10-20. Pixels array (single white pixel) sketch<br />

To change more than 1 pixel, a loop will help. The next example divides the display window<br />

in horizontal b<strong>and</strong>s, assigning a different color to each b<strong>and</strong> (see Figure 10-21):<br />

// Color B<strong>and</strong>ing with pixels[]<br />

size(400, 400);<br />

loadPixels();<br />

int px = pixels.length;<br />

int b<strong>and</strong>s = 20; // should be a factor of px<br />

color c;

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

Saved successfully!

Ooh no, something went wrong!