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

66<br />

You might be curious as to why you can get 256 unique values from a single byte (8<br />

bits) of memory. If you get scared by this explanation, don’t worry. It’s not essential to<br />

know! It’s because in base 2 (as opposed to the base 10 system our minds are accustomed<br />

to), a byte is an 8-digit binary number, made up only of a combination of zeros<br />

<strong>and</strong> ones. The number of distinct values of a binary number can be calculated by taking<br />

2—only two possible values for each digit, 0 or 1—<strong>and</strong> raising it to the power of<br />

the number of places or digits in the number. So 2 to the 8th power equals 256. ints<br />

can hold up to 4 bytes (or 2 to the 32nd power), <strong>and</strong> thus can hold numbers between<br />

–2,147,483,648 <strong>and</strong> 2,147,483,647. If you’ve been involved in digital design for some<br />

time, you’ve probably converted images from 24-bit color to 8-bit color, going from 2<br />

to the 24th power, or 16,777,216 possible colors, to 2 to the 8th power, or 256 possible<br />

colors. The old web-safe palette of 216 colors was just a subset of the 8-bit palette<br />

that looked somewhat consistent on different platforms/browsers.<br />

When you write the variable data type <strong>and</strong> identifier (name)—for example, int<br />

ballCount—you refer to this as declaring the variable. When you give a variable a specific<br />

value—for example, ballCount = 3—then you say you are initializing the variable. These<br />

two activities can be done together in one step (e.g., int ballCount = 3), or separately,<br />

depending upon your program needs. Because variables are by default mutable, once you<br />

initialize a variable you can still change its value whenever you need to (as long as<br />

you assign it a value consistent with its originally declared data type). For example, try running<br />

the following code:<br />

int ballCount = 15;<br />

println(ballCount);<br />

ballCount = 10;<br />

println(ballCount);<br />

ballCount = 100;<br />

println(ballCount);<br />

When you run this sketch, you’ll get the following output:<br />

15<br />

10<br />

100<br />

Strict typing<br />

I remember first learning Java <strong>and</strong> being confused that I had to keep writing the data types<br />

as I declared my variables, not to mention the whole case-sensitivity issue. I was used to<br />

old-school ActionScript, where variables could hold anything at any time. However, I’ve<br />

come to underst<strong>and</strong> <strong>and</strong> appreciate the benefits of what’s referred to as “strict typing” of<br />

variables. Interestingly, ActionScript, which some of you may know, now uses explicit typing<br />

of variables as well—so much for the good old days. So int ballCount can only hold<br />

integer values <strong>and</strong> float ballSpeed can only hold float values. But why is this helpful?

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

Saved successfully!

Ooh no, something went wrong!