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.

Figure 7-35. Bézier Ellipse sketch, example 2<br />

The program plots a closed ellipse using the bezier() function with a little help from<br />

sin() <strong>and</strong> cos(). When plotting a circle using a Bézier curve, which you might remember<br />

is a cubic curve (third-degree polynomial), the two control points get placed between the<br />

two anchor points, dividing the curve into equal thirds, from anchor 1 to control 1 to control<br />

2 to anchor 2. If you were using a quadratic curve (second-degree polynomial), as is<br />

used by ActionScript’s curveTo() method, for example, you’d put your single control<br />

point halfway between the two adjacent anchor points. When using cubic Bézier curves,<br />

you can approximate a circle using only four anchor points; a quadratic Bézier requires<br />

eight—however, neither of these generates a perfect circle. In the example, I used two different<br />

radius variables (radius <strong>and</strong> controlRadius), one for the anchor points <strong>and</strong> one for<br />

the control points. I did this so that you can eventually morph the ellipse into more interesting<br />

forms by changing the spatial relationships between the control <strong>and</strong> anchor points.<br />

However, I also wanted to maintain some of the original symmetry, so the variations aren’t<br />

purely r<strong>and</strong>om.<br />

The two functions that do most of the work are drawEllipse() <strong>and</strong> setEllipse().<br />

setEllipse() fills the anchor <strong>and</strong> control point arrays (px, py, cx, cy, cx2, <strong>and</strong> cy2) based<br />

on the trig equations. Notice that I instantiated the arrays <strong>and</strong> initialized their sizes at the<br />

top of the setEllipse() function, based on the number of points passed into the function.<br />

Since this needs to happen at both the start of the sketch <strong>and</strong> whenever anyone<br />

clicks the screen, it seemed most efficient to create a function, rather than include the<br />

CURVES<br />

293<br />

7

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

Saved successfully!

Ooh no, something went wrong!