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.

You’ll notice that by default, the rectangle is drawn from its top-left corner while the<br />

ellipse is drawn from its center. If this doesn’t seem to make any sense, look at the different<br />

x <strong>and</strong> y positions for these shapes (the first two arguments). You can also explicitly<br />

change how these shapes are drawn, if the default doesn’t work for you, with calls to<br />

either rectMode() or ellipseMode(). These two functions can take a couple different<br />

arguments that control how the shape is drawn. For example, to reverse the default modes<br />

for both the rect() <strong>and</strong> ellipse() functions, you would write rectMode(CENTER); <strong>and</strong><br />

ellipseMode(CORNER);, respectively. Now rectangles would be drawn from the center <strong>and</strong><br />

ellipses would be drawn from the top-left corner.<br />

Curves<br />

The Curves section includes eight entries, dealing with—you guessed it—creating curves.<br />

Curves are a lot more difficult to draw than rectilinear shapes. If you don’t believe me, just<br />

try drawing a circle freeh<strong>and</strong>. Our brains <strong>and</strong> physical capabilities seem more equipped to<br />

deal with simple linear ratios than curvy math. Unfortunately, most really interesting forms<br />

<strong>and</strong> animations tend to deal with more organic-looking structures <strong>and</strong> dynamics. The two<br />

main curve drawing functions in <strong>Processing</strong> are bezier() <strong>and</strong> curve().<br />

Bézier curves require two anchor points <strong>and</strong> two control points. The control points, in a<br />

sense, direct the curve between the anchor points. The common pen tools in programs<br />

like Illustrator <strong>and</strong> Photoshop employ Bézier-like curves, allowing you to interactively control<br />

both the anchor points <strong>and</strong> their control h<strong>and</strong>les.<br />

The curve() function utilizes a spline curve <strong>and</strong> also requires two extra control points.<br />

However, spline curves react quite differently from Bézier curves when these control<br />

points are manipulated. In the next code example, you can drag the control <strong>and</strong> anchor<br />

points on two different curves simultaneously to see this difference (see Figure A-6). The<br />

green path is a spline curve <strong>and</strong> the blue is a Bézier curve. Notice how the control points<br />

for the Bézier curve are off the curve, while with the spline the control points are directly<br />

on the curve. This is a pretty advanced example, which I included to illustrate how the<br />

curves work. I cover the curve functions in detail in Chapter 7.<br />

/*<br />

Curves<br />

Ira Greenberg, November 7, 2005<br />

revised October 9, 2006<br />

--------------------------------------<br />

Interact with the curves by dragging<br />

the anchors/control points<br />

green curve = spline<br />

blue curve = bezier<br />

*/<br />

int pts = 4;<br />

int ptSize = 6;<br />

float[]x = new float[4];<br />

float[]y = new float[4];<br />

boolean[]isDragSafe = new boolean[4];<br />

PROCESSING LANGUAGE API<br />

693<br />

A

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

Saved successfully!

Ooh no, something went wrong!