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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

628<br />

The create() method includes a nested for loop that is a bit dense to untangle. I needed<br />

an algorithm that would run through the vertices array <strong>and</strong> treat each individual face as a<br />

self-contained quadrangle (composed of four points). Notice that the beginShape() <strong>and</strong><br />

endShape() functions are nested within the outer for loop as well, allowing each face to<br />

be drawn as a separate unit. This structure will also allow you to easily customize the color<br />

of each face, which we’ll look at shortly. However, before getting into that, I’ll show you<br />

how to use the two classes just created to generate a cube.<br />

Here’s some runnable code. (Please note that I didn’t repeat the code from the Point3D<br />

<strong>and</strong> Cube classes shown previously. Just make sure you add the two classes beneath the following<br />

code, or put each class in its own tab.)<br />

Cube c1;<br />

void setup(){<br />

size(400, 400, P3D);<br />

c1 = new Cube(200, 200, 200);<br />

c1.create();<br />

}<br />

// Don't forget to add the Point3D <strong>and</strong> Cube classes code<br />

I also want to translate the cube to the middle of the drawing space, or at least have<br />

it move around some. I’ll use <strong>Processing</strong>’s translate() <strong>and</strong> rotate() functions in<br />

conjunction with the classes to achieve this. In addition, I’ll create an overloaded<br />

create(color[]quadBG) method for customizing the color of each quad face.<br />

You may remember that an overloaded method is an object-oriented concept<br />

meaning that more than one method with the same name exists in a class. What<br />

differentiates the same named methods is the number <strong>and</strong> type of parameters<br />

between the parentheses in the head of the method. You can read more about<br />

overloaded methods <strong>and</strong> other object-oriented concepts in Chapter 8.<br />

The next example, shown in Figure 13-8, creates two cubes, using the two overloaded create()<br />

methods. Since I added an additional create() method to the Cube class, I’ve<br />

included the updated class code in the following example. I didn’t repeat the Point3D<br />

class, which you’ll need as well to run the example; simply paste it below the Cube class or<br />

put it into its own tab.<br />

// Two Rotating Custom Cubes<br />

// custom Cube reference variables<br />

Cube c1, c2;<br />

// array to hold different face colors<br />

color[]quadBG = new color[6];<br />

void setup(){<br />

size(400, 400, P3D);<br />

quadBG[0] = color(175, 30, 30, 150);<br />

quadBG[1] = color(30, 175, 30, 150);

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

Saved successfully!

Ooh no, something went wrong!