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.

Figure 13-11. Custom Rotation of a Rectangle Around<br />

the X-, Y-, <strong>and</strong> Z-Axes example<br />

The 2D array float[][]originalVertices is used to store the original rectangle vertices<br />

data, <strong>and</strong> float[][]transformedVertices is used eventually to receive the transformed<br />

vertices data, which is then passed to <strong>Processing</strong>’s vertex() function for drawing. In the<br />

setup() function, I create the original rectangle with the st<strong>and</strong>ard sin() <strong>and</strong> cos() function<br />

approach described in numerous sections of the book. I don’t draw the rectangle<br />

here, but rather construct it in data <strong>and</strong> assign that data to the originalVertices[][]<br />

array. Next, in draw(), I call some custom rotate functions that update the values of the<br />

angleX, angleY, <strong>and</strong> angleZ variables used in the rotation calculations. Finally, I call the<br />

createRect() function, which internally calls the rotateVertices() function <strong>and</strong> draws<br />

the rectangle on the screen. It is in the rotateVertices() function that most of the<br />

sketch’s real heavy lifting occurs.<br />

At the top of the rotateVertices() function I declare three 2D arrays. These arrays are<br />

used to temporarily store the transformed vertices data. To make things simpler (honestly),<br />

I set up the sketch to call the rotateVertices() function every time draw()<br />

executes. This isn’t the most efficient approach, but it avoids things like having to check<br />

which rotation function comm<strong>and</strong>s were invoked; I just assume they’re all being called.<br />

However, if you comment out any/all of the following lines:<br />

myRotateX(2);<br />

myRotateY(6);<br />

myRotateZ(3);<br />

the program will still work properly. The rotateVertices() function will still be called<br />

from within createRect(), but some/all of the rotation angle values won’t change, so<br />

you’ll get less or no rotation. Reading though the rotateVertices() function should be<br />

straightforward, even though it’s kind of dense. The important thing to notice is how the<br />

previous array’s values are input into the next set of expressions, <strong>and</strong> how these expressions<br />

are assigned to one of the temporary arrays. I chose to have the function return the<br />

final array, which is why I used the expression transformedVertices = rotateVertices();<br />

in the createRect() function <strong>and</strong> created the three temporary arrays (rotatedVertices_<br />

XAxis[][], rotatedVertices_YAxis[][], <strong>and</strong> rotatedVertices_ZAxis[][]) locally within<br />

3D<br />

641<br />

13

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

Saved successfully!

Ooh no, something went wrong!