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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

342<br />

Figure 9-2. rectMode() example<br />

The default mode is rectMode(CORNER), which draws the rectangle from the top-left<br />

corner. Since this is the default, I didn’t need to explicitly call it in the sketch. The CENTER<br />

<strong>and</strong> CORNER modes treat the four rect() arguments as x, y, width, <strong>and</strong> height. However,<br />

the CORNERS mode treats the four arguments as x1, y1, x2, y2, specifying the top-left <strong>and</strong><br />

bottom-right corner points of the rectangle. If I add one more rect() call to the bottom<br />

of the program, without specifying another drawing mode call, what mode do you think<br />

will be used? This question is a little tricky, as many people will assume the default state<br />

(CORNER) will be used, but the answer is actually (CORNERS). Every time you issue one of<br />

these drawing mode comm<strong>and</strong>s in a program, you change the current state of the drawing<br />

mode. This type of programming change is referred to as a state change, as you are changing<br />

the drawing state of the program, at least in terms of drawing rectangles.<br />

<strong>Processing</strong>’s ellipse() function, which has the same four parameters as rect(int x,<br />

int y, int w, int h), works similarly. However, as mentioned earlier, the default drawing<br />

mode for ellipse() is ellipseMode(CENTER). ellipse() also has access to one<br />

additional mode, ellipseMode(CENTER_RADIUS). This mode does the same thing as<br />

CENTER, only it treats the width <strong>and</strong> height arguments as radii rather than diameters. One<br />

somewhat useful thing to do with these two modes together is to quickly generate an<br />

ellipse within an ellipse (see Figure 9-3):<br />

size(400, 400);<br />

background(255);<br />

strokeWeight(10);<br />

fill(127);<br />

ellipseMode(CENTER_RADIUS);<br />

ellipse(200, 200, 170, 170);<br />

fill(255);<br />

ellipseMode(CENTER);<br />

ellipse(200, 200, 170, 170);

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

Saved successfully!

Ooh no, something went wrong!