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

262<br />

the loop incrementally moves through each index of the array, processing all the data.<br />

However, in any single iteration of the loop, only one value in each of the arrays is being<br />

processed/used at a time. So, for example, the second iteration of the for loop i is equal<br />

to 1, <strong>and</strong> therefore the second value in each of the arrays is the one being processed/used.<br />

You should also notice that I am using x[i] as both the variable receiving the assignment<br />

<strong>and</strong> the first argument in the makeWaves() function call. I remember being really confused<br />

when I first came across this sort of thing—it didn’t seem to make sense. To underst<strong>and</strong><br />

what’s happening, you need to consider the order in which things take place. Assignment<br />

operations happen from right to left. Thus, the function will do its thing before the assignment<br />

takes place. Since the makeWaves() function returns a float value, the returned value<br />

is assigned to x[i] after the function finishes. I am just using the makeWaves() function to<br />

increment the x[i] value by the trig function. The second argument, angle+=<br />

frequency[i], increments the angle variable each iteration of the loop by the current<br />

value of frequency[i]. The increasing angle value is needed by the makeWaves() function<br />

to keep its internal trig function moving. The last minor syntax issue that might be confusing<br />

to some readers is the following line:<br />

plot(new float[]{ x[i], y[i] });<br />

The plot() function requires a float array argument. The argument new float[]{ x[i],<br />

y[i] } is an anonymous float array that I’m passing to the plot() function. It’s anonymous<br />

because I didn’t assign the array to any variable; thus, its scope is limited to that moment<br />

in the function, which is all I needed. I could also have put the statement on two lines,<br />

like this:<br />

float[] myPts = { x[i], y[i] };<br />

plot(myPts);<br />

Creating curves using polynomials<br />

The last curve creation approach I’ll look at briefly before diving into the <strong>Processing</strong> API is<br />

the use of polynomial equations. Polynomials sound scarier than they really are. In fact,<br />

you’ve already used polynomials in this chapter. Even the simple equation x = 3 is a polynomial—albeit<br />

not a very inspiring one. y = 3 will generate a horizontal line at 3 on the<br />

y-axis. x = 3 will generate a vertical line at 3 on the x-axis. Equations that only use a constant<br />

(a number) are considered zero-degree polynomials. In contrast, the statement<br />

y = 3x is a first-degree polynomial, <strong>and</strong> will no longer only give a plain horizontal or vertical<br />

line. As x increases or decreases in value, the value of y will change. For example, the<br />

following sketch (shown in Figure 7-16) creates 100 points based on this equation, yielding<br />

a diagonal line:<br />

// First-degree polynomial<br />

// y = 3x<br />

size(200, 200);<br />

background(255);<br />

strokeWeight(3);<br />

float x = 0, y = 0;

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

Saved successfully!

Ooh no, something went wrong!