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.

I call my custom function makeRect(), passing the arrays to the function. makeRect() plots<br />

points based on the coordinate data in the arrays, <strong>and</strong> then draws lines between the<br />

points. I needed to use the two conditional if statements to ensure that the connecting<br />

lines were drawn to the right points, based on the value of i in the for loop. Remember<br />

that arrays are zero-indexed, meaning that the first position is at [0], not [1]. If you<br />

request an index position in an array that doesn’t exist, you’ll get an error. The first conditional<br />

statement ensures that this doesn’t happen. The second conditional statement<br />

checks when the loop is in its last cycle, <strong>and</strong> then simply connects the last point back to<br />

the initial point, closing the path. Another way to have done this would be to have eliminated<br />

the last conditional statement <strong>and</strong> just put a line after the for loop, connecting<br />

the last point back to the first, such as line(pts[0][pts[0].length-1], pts[1][<br />

pts[0].length-1], pts[0][0], pts[1][0]);.<br />

This last code line may look pretty confusing, mostly because of the 2D arrays. These<br />

structures are really not that confusing to conceptually grasp, but they are visually <strong>and</strong><br />

procedurally complex to work with. If you really don’t like them, you don’t need to use<br />

them, as you can always substitute two regular one-dimensional arrays in their place.<br />

However, many people use them, so it’s good to be able to recognize <strong>and</strong> underst<strong>and</strong> how<br />

they work. In truth, there is really no such thing as a multidimensional array structure in<br />

<strong>Processing</strong> or Java. Since arrays can hold any data type in <strong>Processing</strong> <strong>and</strong> Java, they can also<br />

hold other arrays. Multidimensional arrays are just arrays of arrays. In the code example, I<br />

created two coordinate arrays, float[]x <strong>and</strong> float[]y, <strong>and</strong> then to be a pest, stuck these<br />

into another 2D array, float[][]xy. I covered arrays in detail in Chapter 3, but a little<br />

review on how to declare/initialize them might be helpful. There are two ways to<br />

declare/initialize arrays in <strong>Processing</strong>. The first way is as follows:<br />

float[]x = new float[array length];<br />

This creates a new array object <strong>and</strong> reserves a number of indices in memory. After you<br />

declare <strong>and</strong> instantiate an array this way, each index will have a default value assigned to<br />

it, based on the data type you’re using; for float arrays, that would be 0.0. You still need<br />

to assign specific values to each index position in the array. For example:<br />

x[0] = .2;<br />

x[1] = .3;<br />

x[2] = .7;<br />

x[3] = .1;<br />

The second way to declare <strong>and</strong> initialize an array is somewhat of a shortcut:<br />

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

This approach works great when you already know the values to put in the array.<br />

Both of these approaches can also be broken into two steps, which you might use if you<br />

needed your array variable to be declared at the top of your program (giving it global<br />

scope), but also wanted to instantiate the array in setup().<br />

float[]x;<br />

x = new float[array length];<br />

LINES<br />

213<br />

6

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

Saved successfully!

Ooh no, something went wrong!