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.

don’t realize you’re doing it—sort of like the cardiovascular heath benefits you receive<br />

from certain bedroom activities. Algebra relies on variables, numbers, <strong>and</strong> operators to<br />

form expressions—which is identical to programming. In programming, though, you need<br />

to use slightly different syntax based on the language you are using. For example, Table 4-1<br />

shows some algebraic expressions <strong>and</strong> the corresponding <strong>Processing</strong> implementations.<br />

Table 4-1. Algebraic expressions <strong>and</strong> their corresponding <strong>Processing</strong> implementations<br />

Algebraic expression <strong>Processing</strong> implementation<br />

2x + 2 2 * x + 2<br />

x 2 – 3x + 4 x * x – 3 * x + 4<br />

(5 * pow(x, 3)) / 4<br />

Elementary algebra also has some very basic rules or laws that help us solve equations.<br />

Some of these laws are also central to programming. For your purposes, though, you just<br />

need to apply them—you don’t have to worry about formal definitions or proofs.<br />

Following is a list—sort of a cheat sheet—of the elementary algebraic laws that most apply<br />

to beginning graphics programming.<br />

Operation order (a.k.a. operator precedence)<br />

Operations happen in a certain order, which can have a direct effect on results. For most<br />

operations, simply remember that what’s inside the parentheses is evaluated first, then multiplication<br />

<strong>and</strong> division, <strong>and</strong> then addition <strong>and</strong> subtraction. If the operators have the same<br />

precedence in a compound expression, <strong>and</strong> there are no parentheses (e.g., 4 + 20 + 5), then<br />

the expression is evaluated from left to right. The following list provides some examples:<br />

6 * 3 – 4 = 14: The multiplication is done first, <strong>and</strong> then the subtraction.<br />

7 – 9 / 3 = 4: The division is done first, <strong>and</strong> then the subtraction.<br />

(7 – 9) / 3 = –2/3: The operation inside the parentheses is done first, <strong>and</strong> then the<br />

division.<br />

4 * 5 / 2 * 3 = 30: The precedence of each operation is equal, so the equation is<br />

solved from left to right.<br />

4 * 15 / 2 – 13 * 9 + 5 = –82: The multiplication <strong>and</strong> division are done first, <strong>and</strong> then<br />

the subtraction <strong>and</strong> addition, from left to right.<br />

For a complete list of operator precedence in Java <strong>and</strong> <strong>Processing</strong>, check out http://<br />

java.sun.com/docs/books/tutorial/java/nuts<strong>and</strong>bolts/expressions.html.<br />

Associative property<br />

When performing addition, grouping doesn’t matter, so you don’t need parentheses, but if<br />

you include them, it won’t do any damage:<br />

(a + b) + c = a + (b + c) = a + b + c<br />

COMPUTER GRAPHICS, THE FUN, EASY WAY<br />

121<br />

4

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

Saved successfully!

Ooh no, something went wrong!