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.

Figure 7-30.<br />

Spline curve using <strong>Processing</strong>’s curve() function<br />

The curve() function is a spline curve implementation, or more precisely a Catmull-Rom<br />

spline implementation. The name “Catmull-Rom” comes from computer scientists Edwin<br />

Catmull <strong>and</strong> Raphael Rom. Catmull later went on to establish a small independent animation<br />

studio in 1986, called Pixar. Thus, you might assume that Catmull-Rom splines are relevant<br />

to 3D animation—<strong>and</strong> you’d be right. They’re important for efficiently generating<br />

complex, smooth geometry—creating motion paths, among other things. In addition to<br />

curve(), <strong>Processing</strong> has a related function, called vertexCurve(), that implements the<br />

same Catmull-Rom spline. I think vertexCurve() is a little simpler to use than curve() (I’ll<br />

look at this function after I slog through the curve() function a bit).<br />

In the last example, I created five initial points to hold the curves’ control points. Hopefully<br />

you noticed that there were no control points off the path, as in the earlier bezier()<br />

example. This is one of the main differences between a Bézier curve <strong>and</strong> a Catmull-Rom<br />

spline curve. After creating the five points, I call my curve() functions. Like the bezier()<br />

function, curve() uses eight parameters—the x <strong>and</strong> y components of four points.<br />

However, unlike bezier(), curve() doesn’t use a system of anchor <strong>and</strong> control points.<br />

Instead, all four of the points, in a sense, function as control points. The curve then follows<br />

the path of the control points. Look closely at the curve() calls in my last example. Here<br />

they are again:<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 />

In the first curve() call, points p0 <strong>and</strong> p1 are at the left <strong>and</strong> right edges of the first segment<br />

of the curve. p4 <strong>and</strong> p2 are the extra control points on either side. In the next<br />

curve() call, I copy p1 to the left point position of the second curve. This will allow the<br />

first curve’s right point <strong>and</strong> the second curve’s left point to align. I then use p2 for the right<br />

point of the second curve. Since each point also needs a control point on the left <strong>and</strong><br />

right, I put p0 to the left of p1, <strong>and</strong> p3 to the right of p2. This same pattern repeats itself in<br />

the next two calls. The only thing that may look odd is the last p0 in the final curve. Since<br />

CURVES<br />

281<br />

7

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

Saved successfully!

Ooh no, something went wrong!