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

264<br />

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

polynomial<br />

You can see a bit of a curve, but the problem is that the value of y grows so fast that most<br />

of the curve is out of the window. In fact, after 100 iterations, y is equal to 19324.0<br />

(pixels)—but the display window is only 200 pixels tall. To better see the curve, you can<br />

calculate the percentage of the display window to the y maximum value <strong>and</strong> multiply y by<br />

this ratio: y*(height/19324.0). This will, in a sense, remap the curve to the viewing space.<br />

Here’s the modified sketch (shown in Figure 7-18):<br />

/* Second-degree polynomial<br />

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

curve fits within display window*/<br />

size(200, 200);<br />

background(255);<br />

strokeWeight(3);<br />

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

int loopLimit = 100;<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 />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!