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.

This is a dense little sketch, so it may not be apparent at first glance what’s happening. The<br />

nested for loop generates a table structure of quadrilaterals. In the outer for loop head, I<br />

initialized two variables (i <strong>and</strong> k) since I needed one variable to be incremented by quadH<br />

(i) <strong>and</strong> one to be incremented by 1 (k). The first line in the loop, after the comment, is the<br />

function call resetMatrix(). This simple step clears the transformation matrix at the<br />

beginning of each iteration of the loop. Since the matrix was getting reset each time, I<br />

couldn’t use it to step down the rows of the table. I solved this by multiplying the second<br />

argument (which controls the y position) in the outer loop translate() call with k, as in<br />

translate(0, quadH*k). However, by resetting the matrix, I was able to repeatedly call<br />

translate(quadW, 0) within the inner loop, which took care of stepping the quadrangles<br />

across the display window. Finally, I set up a convenient little function, r(val), to add a<br />

r<strong>and</strong>om offset to the quadrangle points. Since I used the outer loop variable k to specify<br />

the r<strong>and</strong>om range, r<strong>and</strong>omization increased as the loop ran. Try changing the r<strong>and</strong>Shift<br />

variable at the top of the sketch to see how it affects the output.<br />

Before moving on to some other drawing functions, I want to provide one more example<br />

that will hopefully help clarify the matrix transformations. The next sketch uses a convenient<br />

<strong>Processing</strong> function, printMatrix(), which actually prints to the screen the current<br />

contents of the transformation matrix. As I mentioned earlier, transformations affect the<br />

virtual drawing, or graphics context, so in this next example, all I include are the actual<br />

matrix transformations—which is all I need to see how the individual transformation function<br />

calls affect the overall matrix. You don’t need to spend too much time on this, but just<br />

look at which part of the matrix is affected by each call (see Figure 9-15):<br />

// printMatrix()<br />

//initial state<br />

println(" before transformations");<br />

printMatrix();<br />

//translate<br />

translate(150, 225);<br />

println(" after translate()");<br />

printMatrix();<br />

//scale<br />

scale(.75, .95);<br />

println(" after translate() <strong>and</strong> scale()");<br />

printMatrix();<br />

//rotate<br />

rotate(PI*.3);<br />

println(" after translate(), scale() <strong>and</strong> rotate()");<br />

printMatrix();<br />

//reset<br />

resetMatrix();<br />

println(" after resetMatrix()");<br />

printMatrix();<br />

SHAPES<br />

357<br />

9

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

Saved successfully!

Ooh no, something went wrong!