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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

346<br />

stroke(0);<br />

strokeWeight(2);<br />

fill(225);<br />

triangle(p[0].x, p[0].y, p[1].x, p[1].y, p[2].x, p[2].y);<br />

}<br />

Figure 9-6. Triangle sketch<br />

The examples thus far in the chapter have been pretty dull, so we’ll do something a little<br />

more interesting with the triangle() function. Also note that I used Java’s Point class<br />

again, as I find it very convenient when plotting stuff. This class is not included in<br />

<strong>Processing</strong>, so you won’t find it in the <strong>Processing</strong> reference. However, it’s in the Java API (see<br />

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Point.html). Arrays in <strong>Processing</strong><br />

<strong>and</strong> Java can be declared of any data type. (Remember from Chapter 8 that a class is also<br />

a data type.) Point is a class in Java <strong>and</strong> thus a valid data type. This allowed me to create a<br />

Point array <strong>and</strong> to refer to each Point object in the array by a single index value (e.g.,<br />

p[0] or p[1]). Since each Point object has an x <strong>and</strong> y property, this is a convenient data<br />

type for keeping track of both components (x <strong>and</strong> y) at the same time. If I had just used<br />

arrays of type float instead of the Point class, I’d need to use either two separate parallel<br />

arrays—one for x <strong>and</strong> one for y—or I’d need to use a multidimensional array (p[][]).<br />

I think using an array of Points is the simplest solution. Here’s a little variation on the<br />

Triangle sketch (see Figure 9-7):<br />

//Triangle Zoom<br />

Point[]p = new Point[3];<br />

float shift = 10;<br />

void setup(){<br />

size(400, 400);<br />

background(190);<br />

smooth();<br />

p[0] = new Point(1, height-1);

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

Saved successfully!

Ooh no, something went wrong!