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.

Figure 13-13. Brick Tower sketch<br />

This is a pretty long example, but I assure you you’ve seen it all before. I reused the<br />

Point3D <strong>and</strong> Cube classes, which really simplified things. If you build your classes well, you<br />

can continue to reuse or extend them, which will increase your efficiency over time. This is<br />

one of the great benefits of OOP. The only somewhat complicated parts of the sketch are<br />

the code that controls the alternating brick layer rotation <strong>and</strong> the crenellation code. Since<br />

I knew what angle of rotation I needed to generate the brick ring, I wrote the expression<br />

angle = 360.0/bricksPerLayer*i/2;<br />

to control the alternating seams per each brick layer. Remember the order of operations<br />

(which does make a difference): when the operators are of the same precedence level, the<br />

expression is solved from left to right. This was the case with this expression, as multiplication<br />

<strong>and</strong> division have the same precedence level. The expression evaluates alternating<br />

values per loop cycle between an even multiple of the angle <strong>and</strong> one-half of an even multiple,<br />

which gives me the alternating brick seams. (The actual angle value doesn’t really<br />

matter.) Try removing the /2 from the expression to see what happens when the seams<br />

don’t alternate.<br />

The crenellation code uses the modulus operator, which I’ve discussed in numerous chapters<br />

throughout the book; remember that it evaluates to the remainder of a division. I<br />

used the operator so that every other brick would be drawn.<br />

// add crenellation<br />

if (i==brickLayers-1){<br />

if (j%2 == 0){<br />

bricks[j].create();<br />

}<br />

}<br />

I guess with crenellation like this (one brick high), you’d need very tall bricks (or very short<br />

soldiers).<br />

3D<br />

649<br />

13

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

Saved successfully!

Ooh no, something went wrong!