06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

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.

Example 14-3: A rectangle moving along the z-axis<br />

float z = 0; // a variable for the Z (depth) coordinate<br />

void setup() {<br />

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

}<br />

void draw() {<br />

background(0);<br />

stroke(255);<br />

fill(100);<br />

// Translate <strong>to</strong> a point before displaying a shape there<br />

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

rectMode(CENTER);<br />

rect(0,0,8,8);<br />

z + + ; // Increment Z (i.e. move the shape <strong>to</strong>ward the viewer)<br />

}<br />

Translation and Rotation (in 3D!) 231<br />

Although the result does not look diff erent from Example 14-1, it is quite diff erent conceptually as we have<br />

opened the door <strong>to</strong> creating a variety of three-dimensional eff ects on the screen with <strong>Processing</strong>’s 3D engine.<br />

Exercise 14-1: Fill in the appropriate translate( ) functions <strong>to</strong> create this pattern. Once you are<br />

fi nished, try adding a third argument <strong>to</strong> translate( ) <strong>to</strong> move the pattern in<strong>to</strong> three dimensions.<br />

size(200,200);<br />

background(0);<br />

stroke(255);<br />

fill(255,100);<br />

translate(_______,_______);<br />

rect(0,0,100,100);<br />

translate(_______,_______);<br />

rect(0,0,100,100);<br />

translate(_______,_______);<br />

line(0,0,-50,50);<br />

When using (x,y,z) coordinates, we must tell<br />

<strong>Processing</strong> we want a 3D sketch. This is done<br />

by adding a third argument “P3D” <strong>to</strong> the size()<br />

function. See Section 14.2 for more details.<br />

Th e translate( ) function is particularly useful when you are drawing a collection of shapes relative <strong>to</strong> a<br />

given centerpoint. Harking back <strong>to</strong> a Zoog from the fi rst 10 chapters of this book, we saw code like this:<br />

void display() {<br />

// Draw Zoog's body<br />

fill(150);<br />

rect( x,y ,w/6,h*2);<br />

}<br />

// Draw Zoog's head<br />

fill(255);<br />

ellipse( x,y-h/2 ,w,h);

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

Saved successfully!

Ooh no, something went wrong!