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.

Next, I assign values to some primitive variables:<br />

// controls b<strong>and</strong>ing of gradient<br />

interval = 255/5;<br />

Because the gradient will go from black to white, I made the interval variable a factor<br />

of 255 (255 equals white). This way, I can step from black to white an equal number of<br />

steps—in this case, five. The next line may look a little strange:<br />

// radial width <strong>and</strong> height<br />

gradientWidth = gradientHeight = width;<br />

If I write it out in English, its meaning will be clearer: the value of width is assigned to the<br />

variable gradientHeight, which is then assigned to the variable gradientWidth. When you<br />

use the single-equal sign (=), it means assignment, not equals. Assignment statements are<br />

evaluated right to left, which is why this compound assignment statement works. Also,<br />

remember that width is a built-in <strong>Processing</strong> property that holds the width I made the<br />

sketch window with the function call size(200, 200). gradientWidth <strong>and</strong> gradientHeight<br />

are variables I declared that will hold the width <strong>and</strong> height of the gradient. I could have<br />

written the statement in two lines, as well:<br />

gradientHeight = width;<br />

gradientWidth = gradientHeight;<br />

Setting the center of the gradient, I defined the variables xpos <strong>and</strong> ypos with the expressions<br />

width/2 <strong>and</strong> height/2, respectively. If you’ve never coded before, it may look odd to<br />

use a mathematical expression as something to be assigned, but it’s perfectly legal <strong>and</strong><br />

common in programming. Remember, computers are really just super-powerful (<strong>and</strong><br />

expensive) calculators.<br />

xpos = width/2;<br />

ypos = height/2;<br />

The line noStroke(); turns off any stroke rendering, which is on by default in <strong>Processing</strong><br />

when you render an ellipse or rectangle. noStroke() is another built-in <strong>Processing</strong> function.<br />

The rest of the program paints the gradient:<br />

fill(interval);<br />

ellipse(xpos, ypos, gradientWidth, gradientHeight);<br />

fill(interval*2);<br />

ellipse(xpos, ypos, gradientWidth-interval, gradientHeight-interval);<br />

fill(interval*3);<br />

ellipse(xpos, ypos, gradientWidth-interval*2, ➥<br />

gradientHeight-interval*2);<br />

fill(interval*4);<br />

ellipse(xpos, ypos, gradientWidth-interval*3, ➥<br />

gradientHeight-interval*3);<br />

fill(interval*5);<br />

ellipse(xpos, ypos, gradientWidth-interval*4, ➥<br />

gradientHeight-interval*4);<br />

CODE GRAMMAR 101<br />

71<br />

3

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

Saved successfully!

Ooh no, something went wrong!