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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

214<br />

This other way is less common:<br />

float[]x;<br />

x = new float []{val-1, val-2, val-3...};<br />

Getting back to the 2D array float[][]xy, this array just holds two other arrays. In this<br />

case, both of the internal arrays x[] <strong>and</strong> y[] are the same size, or length, but they don’t<br />

actually need to be. For example, the following is perfectly legal:<br />

int[]months = new int[12];<br />

int[]weeks = new int[52];<br />

int[]days = new int[365];<br />

int[][]year = {months, weeks, days};<br />

year is still a 2D array, even though it contains three arrays, each with a different length. A<br />

3D array, which I don’t recommend using, would be an array containing 2D arrays (yuck).<br />

To access the first value in the first array in xy[][], you use xy[0][0];. To access the second<br />

value in the first array, you use xy[0][1];. To access the first value in the second<br />

array, you use xy[1][0]; to access the second value in the second array, you use<br />

xy[1][1]. Do you see the pattern? Here’s one more: if you wanted to access the 17th<br />

value in the 26th array, you’d write arrayname[25][16]. One other metaphor that might<br />

help you visualize a 2D array is a multi-drawer filing cabinet. The first set of brackets of a<br />

2D array can be thought of as the drawers of the filing cabinet, <strong>and</strong> the second set of<br />

brackets represent the files in each drawer. You’ll be using arrays a whole lot throughout<br />

the book, so rest assured—if it didn’t stick this time, it will eventually.<br />

Anti-aliasing using the smooth function<br />

The other new function I used in the plotter example was smooth(), which anti-aliases<br />

the screen output. Anti-aliasing is essentially a blending trick, providing a smooth edge to<br />

screen output. Since the screen is composed of discrete rectangular pixels in a rigid grid,<br />

diagonal <strong>and</strong> curved edges won’t coincide precisely with the pixel grid, <strong>and</strong> will thus<br />

appear jagged. Anti-aliasing compensates for this by filling in edge pixels with varying<br />

colors/values, based on an anti-aliasing algorithm. For example, a relatively simple antialiasing<br />

algorithm could be to evaluate the percent a pixel is overlapped by a shape’s ideal<br />

edge (the edge if it weren’t converted to jagged pixels), <strong>and</strong> then fill those overlapped pixels<br />

with a color value based on the edge color multiplied by the percent of overlap. A popular,<br />

but far more processor-intensive anti-aliasing technique is called supersampling, in<br />

which an image in internally rendered to a pixel buffer (not to the screen) multiple times<br />

larger than it actually is. Thus, numerous pixels now represent each original image pixel.<br />

Color values can then be evaluated at different points on the original pixel (where each of<br />

the extra pixels now represent a part of the original pixel) <strong>and</strong> averaged. After the averaging<br />

is completed, the image is reduced to its normal size <strong>and</strong> rendered to the screen.

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

Saved successfully!

Ooh no, something went wrong!