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

742<br />

rounds down, <strong>and</strong> round() rounds the normal way—to whichever value is closer. There is<br />

a function to calculate distance (dist()), square roots (sqrt()), maximums (max()), <strong>and</strong><br />

minimums (min()). Many of these functions can be considered convenience functions, in<br />

the sense that it’s possible to use other existing code structures in <strong>Processing</strong> to derive<br />

your own routines for these functions. That said, it really doesn’t make a whole lot of<br />

sense to re-create perfectly fine (<strong>and</strong> very likely more efficient) existing code.<br />

Trigonometry<br />

Trigonometry rocks! I know this might sound like a shocking <strong>and</strong> absurd statement to<br />

many of you (or you’ve already written me off as a major geek <strong>and</strong> have come to expect<br />

it). Trig allows you to do very cool things with code that would be a major pain without it.<br />

Some of these things include wave generation, firing projectiles/aiming, 2D <strong>and</strong> 3D rotations,<br />

<strong>and</strong> any type of organic motion. I cover trig in Chapter 4, in the section entitled “The<br />

Joy of Math,” as well as in Appendix B; <strong>and</strong> (in case you haven’t noticed), I’ve also been<br />

using it in many of the code examples throughout the book.<br />

The Trigonometry section includes nine functions: the basic three <strong>and</strong> their inverses (sin(),<br />

cos(), tan(), asin(), acos(), <strong>and</strong> atan()); two useful utility functions (degrees() <strong>and</strong><br />

radians(), which convert between these different units of measure); <strong>and</strong> atan2(), a variation<br />

on atan(). Remember that trig functions expect angles in radians, not degrees.<br />

Without the radians() function, you’d have to take all your angles, multiply them by pi,<br />

<strong>and</strong> then divide by 180 to convert to radians; it’s much simpler to write radians(angle in<br />

degrees). The atan2() function is really h<strong>and</strong>y for aiming <strong>and</strong> shooting stuff on the<br />

computer—two things my seven-year-old son can’t seem to get enough of (in spite of his<br />

peacenik, granola-eating parents’ best intentions).<br />

R<strong>and</strong>om<br />

The R<strong>and</strong>om section includes five functions involved in r<strong>and</strong>om number generation. In<br />

practically every piece of software art I create, I include some r<strong>and</strong>om processes.<br />

R<strong>and</strong>omization brings an organic quality to the coding process, as well as a sense of continuous<br />

discovery. <strong>Processing</strong> has two r<strong>and</strong>om generators: r<strong>and</strong>om() <strong>and</strong> noise().<br />

r<strong>and</strong>om() is the far simpler function, <strong>and</strong> the one I use most often. It works by receiving<br />

either one or two arguments, as in<br />

or<br />

r<strong>and</strong>om(1)<br />

r<strong>and</strong>om(1, 10);<br />

The first case generates a r<strong>and</strong>om float value between 0 <strong>and</strong> the argument (in this case,<br />

1). The second version generates a r<strong>and</strong>om float value between the two arguments (1<br />

<strong>and</strong> 10). If you need the returned value to be an integer, you’ll need to convert the result<br />

using a statement such as the following:<br />

round(r<strong>and</strong>om(1, 10));

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

Saved successfully!

Ooh no, something went wrong!