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

622<br />

as the block is offset across the stage. Returning to the rotating block wall, how would you<br />

solve this problem?<br />

<strong>Processing</strong> comes equipped with two h<strong>and</strong>y functions to solve the problem: pushMatrix()<br />

<strong>and</strong> popMatrix(). These functions work in pairs <strong>and</strong> allow you to temporarily offset the<br />

drawing context of the display window, draw something, <strong>and</strong> then put the drawing context<br />

back to where it was before you transformed it. Pretty cool, right? Here’s the fixed rotating<br />

wall utilizing the two functions (see Figure 13-6):<br />

// Multiple Translations with pushMatrix() <strong>and</strong> popMatrix()<br />

void setup(){<br />

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

}<br />

void draw(){<br />

background(0);<br />

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

rotateY(frameCount*PI/60);<br />

pushMatrix();<br />

translate(-50, -50);<br />

fill(255, 0, 0);<br />

box(100, 100, 100);<br />

popMatrix();<br />

pushMatrix();<br />

translate(50, -50);<br />

fill(255, 255, 0);<br />

box(100, 100, 100);<br />

popMatrix();<br />

pushMatrix();<br />

translate(50, 50);<br />

fill(0, 0, 255);<br />

box(100, 100, 100);<br />

popMatrix();<br />

pushMatrix();<br />

translate(-50, 50);<br />

fill(0, 255, 0);<br />

box(100, 100, 100);<br />

popMatrix();<br />

}

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

Saved successfully!

Ooh no, something went wrong!