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.

242 <strong>Learning</strong> <strong>Processing</strong><br />

14.7<br />

Scale<br />

Exercise 14-7: Rotate the 3D cube you made in Exercise 14-5. Can you rotate it around the<br />

corner or center? You can also use the <strong>Processing</strong> function box( ) <strong>to</strong> make the cube.<br />

Exercise 14-8: Make a Pyramid class.<br />

In addition <strong>to</strong> translate( ) and rotate( ) , there is one more function, scale( ) , that aff ects the way shapes<br />

are oriented and drawn onscreen. scale( ) increases or decreases the size of objects onscreen. Just as with<br />

rotate( ) , the scaling eff ect is performed relative <strong>to</strong> the origin’s location.<br />

scale( ) takes a fl oating point value, a percentage at which <strong>to</strong> scale: 1.0 is 100%. For example, scale(0.5)<br />

draws an object at 50% of its size and scale(3.0) increases the object’s size <strong>to</strong> 300%.<br />

Following is a re-creation of Example 14-1 (the growing square) using scale( ) .<br />

Example 14-11: A growing rectangle, using scale()<br />

float r = 0.0;<br />

void setup() {<br />

size(200,200);<br />

}<br />

void draw() {<br />

background(0);<br />

// Translate <strong>to</strong> center of window<br />

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

// Scale any shapes according <strong>to</strong> value of r<br />

scale(r);<br />

// Display a rectangle in the middle of the screen<br />

stroke(255);<br />

fill(100);<br />

rectMode(CENTER);<br />

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

// Increase the scale variable<br />

r + = 0.02;<br />

}<br />

scale( ) can also take two arguments (for scaling along the x and y -axes with diff erent values) or three<br />

arguments (for the x -, y -, and z -axes).<br />

14.8<br />

The Matrix: Pushing and Popping<br />

What is the matrix?<br />

fi g. 14.22<br />

scale() increases the dimensions<br />

of an object relative <strong>to</strong> the origin<br />

by a percentage (1.0 � 100%).<br />

Notice how in this example the<br />

scaling effect causes the outline<br />

of the shape <strong>to</strong> become thicker.<br />

In order <strong>to</strong> keep track of rotations and translations and how <strong>to</strong> display the shapes according <strong>to</strong> diff erent<br />

transformations, <strong>Processing</strong> (and just about any computer graphics software) uses a matrix.

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

Saved successfully!

Ooh no, something went wrong!