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 />

626<br />

Point3D(float x, float y, float z){<br />

this.x = x;<br />

this.y = y;<br />

this.z = z;<br />

}<br />

}<br />

If you run this, you should see the following output: -50.0, -50.0, -50.0.<br />

Next, you’ll create your Cube class.<br />

I should point out that technically a cube would be composed only of equal-area square<br />

faces. However, my Cube class will allow you to specify different values for width, height,<br />

<strong>and</strong> depth; sorry if this offends any of you purists out there.<br />

The Cube class will rely on the Point3D class in a compostional relationship, in which I’ll<br />

embed variables of type Point3D directly within the Cube class. Remember, each cube will<br />

be created around the origin, so I won’t account for an x, y, or z property for the entire<br />

cube; only the cube’s overall width, height, <strong>and</strong> depth properties will be specified, similarly<br />

to how <strong>Processing</strong>’s box() function works. (The class code that follows will not execute on<br />

its own, but you probably realize that by now.)<br />

class Cube{<br />

Point3D[] vertices = new Point3D[24];<br />

float w, h, d;<br />

// constructors<br />

// default constructor<br />

Cube(){<br />

}<br />

Cube(float w, float h, float d){<br />

this.w = w;<br />

this.h = h;<br />

this.d = d;<br />

// cube composed of 6 quads<br />

//front<br />

vertices[0] = new Point3D(-w/2,-h/2,d/2);<br />

vertices[1] = new Point3D(w/2,-h/2,d/2);<br />

vertices[2] = new Point3D(w/2,h/2,d/2);<br />

vertices[3] = new Point3D(-w/2,h/2,d/2);<br />

//left<br />

vertices[4] = new Point3D(-w/2,-h/2,d/2);<br />

vertices[5] = new Point3D(-w/2,-h/2,-d/2);<br />

vertices[6] = new Point3D(-w/2,h/2,-d/2);<br />

vertices[7] = new Point3D(-w/2,h/2,d/2);<br />

//right

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

Saved successfully!

Ooh no, something went wrong!