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 />

280<br />

Another way you can generate more complex curves, without depending solely on stringing<br />

so many Bézier segments together, is to use higher-degree polynomials. Higher-degree<br />

curves provide more inflection points (the places where curves change direction), <strong>and</strong> also<br />

require more control points. The number of control points is determined by the polynomial<br />

degree minus 1—that’s why the st<strong>and</strong>ard Bézier, third-degree curve has two control<br />

h<strong>and</strong>les. The ability for a curve to change direction a number of times should make it useful<br />

as a design or animation tool. However, the complexity <strong>and</strong> computational cost of<br />

using these higher-degree curves offsets most of the benefits. Thus, you’re pretty much<br />

stuck with the cubic (third-degree) <strong>and</strong> quadratic (second-degree) curves you’ve already<br />

looked at.<br />

However, there is one other important approach to creating smooth, continuous curves<br />

that simplifies some of the challenges of simply stringing together Bézier curves. This<br />

approach is encapsulated (within <strong>Processing</strong>) in the curve() function. I’ll show you how<br />

the curve() function actually works in a minute, but first I’ll jump in with a curve() example<br />

(see Figure 7-30) that plots the same curve generated in the preceding Bézier example<br />

to help highlight the difference between the two functions:<br />

// Catmull-Rom spline curve<br />

size(500, 500);<br />

background(255);<br />

Point p0 = new Point(150, 100);<br />

Point p1 = new Point(350, 100);<br />

Point p2 = new Point(350, 300);<br />

Point p3 = new Point(150, 300);<br />

Point p4 = new Point(100, 400);<br />

//curve segments<br />

curve(p4.x, p4.y, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y);<br />

curve(p0.x, p0.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);<br />

curve(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);<br />

curve(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, p0.x, p0.y );<br />

//control points<br />

ellipse(p0.x, p0.y, 10, 10);<br />

ellipse(p1.x, p1.y, 10, 10);<br />

ellipse(p2.x, p2.y, 10, 10);<br />

ellipse(p3.x, p3.y, 10, 10);<br />

ellipse(p4.x, p4.y, 10, 10);

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

Saved successfully!

Ooh no, something went wrong!