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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

642<br />

the rotateVertices() function; there are certainly other ways to implement this. Before<br />

you move on to the cube example, I strongly recommend you play/experiment with this<br />

one for a while.<br />

I’ll reuse the Point3D <strong>and</strong> Cube classes to build the next example. I’ll need to create<br />

another method to h<strong>and</strong>le the combined 3D rotations. I could redesign the Cube class <strong>and</strong><br />

add the new rotation method directly in there. But a much better a way to deal with<br />

adding the new method is to create a new class as a subclass of the Cube class. This<br />

approach gives me the benefit of using the original class without having to alter it, still<br />

allowing me to extend its capabilities. As discussed earlier in the book, the term inheritance<br />

in OOP is used to describe this extending approach. Although I could have gone into<br />

my original Cube class <strong>and</strong> added a rotation method, it’s best practice to not edit existing<br />

classes, as other dependent classes could be adversely affected. The following code gives<br />

the completed example (shown in Figure 13-12). I didn’t include the Point3D or Cube class<br />

code, just the new SpinnyCube class (Cube subclass); so don’t forget to add the Point3D<br />

<strong>and</strong> Cube classes before running the example. (Remember, you can either create a new tab<br />

for each class, or simply paste the classes beneath your current sketch code.)<br />

// Rotation of a Custom Cube Around the X-, Y-, <strong>and</strong> Z-Axes<br />

// custom Cube reference variable<br />

SpinnyCube c1;<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, 255);<br />

quadBG[1] = color(30, 175, 30, 255);<br />

quadBG[2] = color(30, 30, 175, 255);<br />

quadBG[3] = color(175, 175, 30, 255);<br />

quadBG[4] = color(175, 30, 175, 255);<br />

quadBG[5] = color(175, 87, 30, 255);<br />

//instantiate cube<br />

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

}<br />

void draw(){<br />

background(100);<br />

translate(width/2, height/2);<br />

if (mousePressed){<br />

//interactive rotation<br />

c1.spinnyRotateX(mouseY);<br />

c1.spinnyRotateY(mouseX);<br />

}<br />

else {

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

Saved successfully!

Ooh no, something went wrong!