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.

Beyond box() <strong>and</strong> sphere()<br />

While there are many common 3D primitives (including cylinders, pyramids, cones, <strong>and</strong><br />

toroids), <strong>Processing</strong>’s built-in primitive-creation functions are limited to the measly box()<br />

<strong>and</strong> sphere(). I’ll show you how to construct a few of these—which is mostly a matter of<br />

creating algorithms. What I mean by this is that you need a set of logical instructions to<br />

create these forms (unless you’re a math genius, which I’m not). Sometimes forms seem<br />

complex until you see the steps to generate them. Before switching shapes, I’ll develop an<br />

algorithm to construct a brick tower (shown in Figure 13-13) that includes a crenellated<br />

roof, using the existing Cube class.<br />

The tower will be constructed of rings of bricks with a rotational offset between the brick<br />

rows, allowing the vertically adjacent brick seams to alternate, like in a regular brick wall.<br />

The first part of the algorithm generates a single ring of bricks; next, the graphics context<br />

(the object that controls the actual drawing) is translated the distance equal to the height<br />

of a brick <strong>and</strong> rotated one-half the ring rotation angle so that the vertical seams alternate.<br />

Then the first step is repeated until some specified height is reached. Finally, the last step<br />

involves creating the crenellation, which uses the same basic routine as step 1 (generating<br />

a brick ring), but with every other brick removed (actually never added).<br />

To generate the initial brick ring, I used the following trig expressions:<br />

z = cos(radians(angle))*radius;<br />

x = sin(radians(angle))*radius;<br />

These expressions, which were discussed earlier in the chapter, can be used to generate a<br />

regular polygon that is flipped to face the top <strong>and</strong> bottom of the display window. In addition<br />

to placing the bricks in a ring in the xz plane, I also need to rotate the bricks so that<br />

they are aligned with the ring path. I accomplished this by rotating each brick the same<br />

number of degrees I displaced it to form the ring. To help underst<strong>and</strong> this, imagine that<br />

you have a brick floating in front of your eyes. The brick’s wide side is facing you, as if you<br />

were looking straight at a building. Now displace the brick 90 degrees around your head<br />

(but don’t rotate it), so that it’s next to your left ear. The brick’s end is now facing your ear.<br />

To make the wide side of the brick again face your head (actually, your ear), you need to<br />

rotate the brick around its center point the same number of degrees it was displaced (i.e.,<br />

90 degrees).<br />

Following is the code for the example, which requires the Cube <strong>and</strong> Point3D classes:<br />

// Brick Tower<br />

// Point3D <strong>and</strong> Cube classes required.<br />

float bricksPerLayer = 16.0; // value must be even<br />

float brickLayers = 25.0;<br />

Cube[]bricks = new Cube[int(bricksPerLayer*brickLayers)];<br />

float brickWidth = 60, brickHeight = 25, brickDepth = 25;<br />

float radius = 150.0;<br />

float angle = 0;<br />

3D<br />

647<br />

13

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

Saved successfully!

Ooh no, something went wrong!