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.

}<br />

pts[counter2].y-yg);<br />

xg*=-1;<br />

counter++;<br />

}<br />

counter2++;<br />

branch(pts);<br />

}<br />

I don’t want to spend too much time on the code here, as lots of coding is coming up in<br />

subsequent chapters. I’ll just give a quick overview of how the function works. If you feel<br />

overwhelmed at any point, just skip the code parts. In Chapter 3, I’ll begin with a very<br />

gentle introduction to the fundamentals of coding, so don’t panic. What I’m really interested<br />

in illustrating is a creative process using code.<br />

The branch function expects an array, of type Point2D.Float, as an argument (the stuff<br />

between the parentheses at the top of the function). An array just holds a bunch of values,<br />

as opposed to a variable, which only holds a single value. I chose to use Java’s Point2D.Float<br />

class because it has built-in x <strong>and</strong> y properties <strong>and</strong> can h<strong>and</strong>le real numbers (as opposed<br />

to only integers). The stemCount variable controls how many branches each subsequent<br />

branch gets. I hard-coded it to 2 just for now. The for loop draws the diagonal lines <strong>and</strong><br />

updates the pts array with the new branch locations. The variable xg controls whether the<br />

branches lean left or right. By continuously multiplying xg by –1, it keeps toggling<br />

the branches from right to left—not terribly organic, but efficient. The counters keep<br />

track of stuff, <strong>and</strong> then I recursively call the same function, passing in the updated pts<br />

array. At this point, you are probably either like “Cool, that makes sense,” or “Is it too late<br />

to return this book? This guy is nuts!” Well, I may be nuts, but I don’t expect most of<br />

you to be able to follow this code. However, hearing some of these terms over <strong>and</strong> over<br />

will help with the “stick in your brain” part. What is really much more important <strong>and</strong> interesting<br />

is how to turn this cold schematic diagram into a tree (see Figure 2-3).<br />

Figure 2-3. Initial code implementation of the branching algorithm<br />

CREATIVE CODING<br />

47<br />

2

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

Saved successfully!

Ooh no, something went wrong!