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

72<br />

The fill(interval) lines are calls to <strong>Processing</strong>’s built-in fill() function. The calls pass<br />

an argument to the function that controls the fill color of the shapes. In this case, I’m<br />

working with grayscale values between 0 <strong>and</strong> 255. The ellipse(x, y, w, h) function call<br />

has four arguments for the x <strong>and</strong> y position <strong>and</strong> width <strong>and</strong> height of the ellipse. You’ll also<br />

notice that the arguments I pass to the ellipse functions are both other variables <strong>and</strong><br />

expressions that include variables. Try running the program, if you haven’t already done<br />

so, <strong>and</strong> changing some of the values <strong>and</strong>/or expressions to see what happens. See also if<br />

you can increase the number of steps in the gradient, making it a little smoother. When we<br />

get to loops, I’ll recode this for you, <strong>and</strong> you’ll really see the power of code for doing<br />

repetitive tasks or iteration.<br />

Operators<br />

Some of you may remember the word operator from math class. Operators are the<br />

symbols you use when you write mathematical expressions. Technically speaking, operators<br />

perform a function on oper<strong>and</strong>s. In the expression x + 3, x <strong>and</strong> 3 are the oper<strong>and</strong>s <strong>and</strong><br />

+ is the operator. In terms of coding, you’ll primarily use the same four operators you<br />

learned about in grade school: +, –, *, <strong>and</strong> /. Just using these operators, you’re able to<br />

perform almost all the math you need to create amazing visuals. <strong>Processing</strong> <strong>and</strong> Java<br />

have additional operators besides these four, some of which I’ll go over as well. Beginning<br />

to use operators is pretty simple. <strong>Coding</strong> can get complex, however, when you’re figuring<br />

out the right combinations of operators needed in forming more complex expressions.<br />

You’ve already seen a bunch of operators in action in the last section on variables.<br />

For example, in the function call ellipse(xpos, ypos, gradientWidth-interval*4,<br />

gradientHeight-interval*4), the third <strong>and</strong> fourth arguments passed to the ellipse<br />

function, gradientWidth-interval*4 <strong>and</strong> gradientHeight-interval*4, each used two<br />

operators (– <strong>and</strong> *). Because operators return a value, you are able to use them as arguments,<br />

as shown previously.<br />

Here are some assignment operations utilizing the four basic operators. I’ll also go over<br />

some basic rules that control the order in which operators do their thing, which is referred<br />

to as operator precedence.<br />

int x = 4+3-6;<br />

float y = 14*3/5-2.34;<br />

float z = 14*3/(5-2.34);<br />

The order of calculation is as follows:<br />

Parentheses<br />

Multiplication <strong>and</strong> division<br />

Addition <strong>and</strong> subtraction<br />

When operators have the same precedence level, the expression is evaluated from left to<br />

right.

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

Saved successfully!

Ooh no, something went wrong!