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.

Figure 7-18. Generating a curve with a second-degree<br />

polynomial, mapped to the display window<br />

The scary-looking line float ratio = height/(2*pow(loopLimit-1, 2)-3*loopLimit-1<br />

+ 19); is your quadratic equation converted into code. The pow(loopLimit-1, 2) part<br />

uses <strong>Processing</strong>’s built-in pow() function. The function takes two arguments: the first is the<br />

value to act upon (the base), <strong>and</strong> the second is the power to raise the value to (the exponent),<br />

so pow(loopLimit-1, 2) is equal to (loopLimit – 1) 2 . In this case, the base is the<br />

expression loopLimit – 1.<br />

There is another important property of second-degree polynomials, also known as<br />

quadratic curves, that you’re not seeing in the plot. Quadratics don’t just form any old<br />

curves—they form parabolas, or bell-shaped symmetrical curves. However, to see the bell<br />

shape, you need to shift the curve to the right by width/2, <strong>and</strong> also begin your for loop at<br />

-width/2. Here’s the modified sketch code (shown in Figure 7-19):<br />

/* parabola<br />

y = 2x 2 -3x + 19;<br />

parabola fits within display window*/<br />

size(400, 400);<br />

background(255);<br />

strokeWeight(3);<br />

float x = 0, y = 0;<br />

int loopLimit = 200;<br />

//shifts curve to the right<br />

int xShift = width/2;<br />

/*Instead of using the magic number 19324.0 in the ratio, I used the<br />

polynomial <strong>and</strong> plugged in the loop limit to get the maximum. This<br />

way if the window size changes, the program should still work.*/<br />

float ratio = height/(2*pow(loopLimit-1, 2)-3*loopLimit-1 + 19);<br />

CURVES<br />

265<br />

7

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

Saved successfully!

Ooh no, something went wrong!