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.

it provides an easy way for users to instantiate objects with various initial configurations<br />

<strong>and</strong> values, as I did in this last example. One other minor point is that I created a couple<br />

local variables in the drawWindow() method:<br />

//local variables<br />

int margin = 0;<br />

int winHt = 0;<br />

int winWdth = 0;<br />

Like parameters, these variables are only scoped within the method that they are defined<br />

within. In general, it’s a good rule to only give variables the scope they require. These variables<br />

are only used within the drawWindow() method, so it made sense to make them<br />

local. Take a few minutes to look at the switch statement in the drawWindow() method, as<br />

well as the procedure I used to draw the different windows. Again, I relied on <strong>Processing</strong>’s<br />

rect() function to take care of all the drawing.<br />

Roof class<br />

The last piece of our house I need is the roof. Next is a sketch that draws a roof using the<br />

new Roof class (see Figure 9-33):<br />

// Drawing Some Roofs<br />

void setup(){<br />

size(400, 150);<br />

background(200);<br />

smooth();<br />

Roof roof1 = new Roof();<br />

roof1.drawRoof(25, 100, 100, 150);<br />

Roof roof2 = new Roof(Roof.GAMBREL);<br />

roof2.drawRoof(150, 100, 100, 150);<br />

Roof roof3 = new Roof(Roof.DOME);<br />

roof3.drawRoof(275, 100, 100, 100);<br />

}<br />

class Roof{<br />

//roof properties<br />

int x;<br />

int y;<br />

int w;<br />

int h;<br />

// roof style<br />

int style = 0;<br />

//constants<br />

final static int CATHEDRAL = 0;<br />

final static int GAMBREL = 1;<br />

final static int DOME = 2;<br />

SHAPES<br />

389<br />

9

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

Saved successfully!

Ooh no, something went wrong!