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.

type into the sketch. Although the 1 is a constant, it also expresses a state that is a constant.<br />

There will always be one more column than the number of points when the points<br />

are spaced out evenly using outer columns, <strong>and</strong> one less column than points if outer<br />

columns aren’t used (see Figure 6-5).<br />

Figure 6-5. Calculating columns based on number of objects<br />

The float type keeps rounding errors from shifting the points during division in the for<br />

loop. This is a subtle but very important point. For example, the simple expression 5 / 2<br />

will not give the answer you might suspect; the answer, in <strong>Processing</strong>, is 2. This is because<br />

two integers are divided together, which returns another integer. To get the right mathematical<br />

answer (5.0 / 2 = 2.5), one of the oper<strong>and</strong>s needs to be a float.<br />

In the sketch, change the float type to int, set totalPts to 30 or 60 points, <strong>and</strong> run the<br />

sketch. Then, set totalPts to 29 or 59 points <strong>and</strong> run it again; the problem will go away,<br />

as 29 + 1 <strong>and</strong> 59 + 1 yields an even multiple of 300. You can use other values to demonstrate<br />

this as well. When steps is declared as type int, <strong>and</strong> you use it in the for loop (i.e.,<br />

(width/steps)*i,), the answer gets rounded to an int. If you want to see the calculations,<br />

try putting the statement println((width/steps)*i), within the for loop. You’ll<br />

see that all the values are integers. Now switch steps type from int back to float, <strong>and</strong> the<br />

output should be real numbers (numbers with fractional parts after the decimal point).<br />

Creating organic form through r<strong>and</strong>omization<br />

OK, so you can draw a line with some points—no big deal, right? Well, there are some<br />

interesting things you can do now that you have your line being described procedurally.<br />

For example, to create a more organic or fuzzy line, you can use <strong>Processing</strong>’s r<strong>and</strong>om()<br />

function within the loop (see Figure 6-6):<br />

LINES<br />

179<br />

6

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

Saved successfully!

Ooh no, something went wrong!