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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Conditionals<br />

Conditionals <strong>and</strong> the relational operators work together. The seven entries in the<br />

Conditionals section are the words/structures used in conjunction with the operators (=,<br />

etc.). The two most commonly used conditionals, especially by new coders, are the if()<br />

structure <strong>and</strong> the reserved keyword else. When used together, along with the operators,<br />

this simple if...else structure become a powerful data logic tool. There are also two<br />

other structures: case, used for switch statements, <strong>and</strong> ?:, a condensed version of the<br />

if...else structure—which are variations on this important programming construct. I<br />

cover these structures, including some examples, in Chapter 3.<br />

Logical Operators<br />

The Logical Operators section includes just three entries: !, &&, <strong>and</strong> ||, which are “not”,<br />

“<strong>and</strong>,” <strong>and</strong> “or,” respectively. These operators allow you to make compound conditional<br />

statements, in which more than one condition can be checked within the same structure.<br />

Using these logical operators in conjunction with the other structures in the Control section—relational<br />

operators, loops, <strong>and</strong> conditionals—you can control the world (or at least<br />

make some interesting code art).<br />

The next example, which depicts population explosion, includes some relational <strong>and</strong> logical<br />

operators (see Figure A-4). Breeding in this simple world is caused by the bots colliding<br />

with the edges of the display window. The detectCollision() function utilizes two compound<br />

conditional expressions. Notice how I divided the sketch up into a series of functions,<br />

with each function h<strong>and</strong>ling a specific task; this modular approach makes the<br />

program easier to underst<strong>and</strong>.<br />

/* Population Explosion<br />

Ira Greenberg, November 6, 2005<br />

revised October 10, 2006 */<br />

//declare some global variables<br />

int botCount = 0;<br />

int botLimit = 1000;<br />

color worldColor = color(0, 40);<br />

color[] botColor = new color[botCount];<br />

float[] x = new float[botCount];<br />

float[] y = new float[botCount];<br />

float[] speedX = new float[botCount];<br />

float[] speedY = new float[botCount];<br />

float botSize = 3;<br />

void setup(){<br />

size(400, 400);<br />

smooth();<br />

noStroke();<br />

breedBot();<br />

}<br />

PROCESSING LANGUAGE API<br />

689<br />

A

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

Saved successfully!

Ooh no, something went wrong!