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.

“Operators” will hopefully sound pretty familiar by now. Operators are the basic math<br />

symbols used to do mathematical operations. I’ve been using the most common four (+, -,<br />

+, <strong>and</strong> /) throughout the book, so I’ll assume you know what these basic operators do. If<br />

not, you can always refer to the “Operators” section of Chapter 3. These symbols can be<br />

used in conjunction with = for assignment operations, as in speed+=.5. I also cover assignment<br />

operations in detail in Chapter 3. % is a strange operator to many people, so it may<br />

need a little more clarification. You might remember that % is called the modulus operator,<br />

<strong>and</strong> it used to find the remainder of a division operation. For example, 5 % 3 = 2 <strong>and</strong> 6 %<br />

2 = 0. In the first example, 2 is the remainder of the division; in the second expression,<br />

there is no remainder, so it equals 0. Two other operators that may need a little further<br />

clarification are the increment <strong>and</strong> decrement operators (++ <strong>and</strong> --). I’ve used these<br />

throughout the book as well, so you are probably familiar with how they basically work.<br />

What I haven’t covered are the pre <strong>and</strong> post options when using them. I’ve mostly been<br />

using them on the right side of the oper<strong>and</strong>, as in speed++, but it is perfectly valid to write<br />

the statement as ++speed. However, there is a subtle difference in how these two forms<br />

work. For example, in the following code snippets, the pre increment expression outputs a<br />

7 <strong>and</strong> the post increment a 6. Why?<br />

Here’s the pre increment code:<br />

int val = 6;<br />

print(++val); //outputs 7<br />

And here’s the post increment version:<br />

int val = 6;<br />

print(val++); //outputs 6<br />

The pre increment performs the incrementation before the print function returns a value,<br />

<strong>and</strong> the post version does it afterward. If you run a second print statement on the variable,<br />

as follows, you’ll see that it now says 7:<br />

int val = 6;<br />

println(val++); //outputs 6<br />

print(val); //outputs 7<br />

Bitwise Operators<br />

Bitwise operators act at the bit level (down at the zeros <strong>and</strong> ones), <strong>and</strong> are especially useful<br />

for efficiently manipulating the individual RGBA color components of pixels. The four<br />

bitwise operators included in <strong>Processing</strong> are: >> (right shift),

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

Saved successfully!

Ooh no, something went wrong!