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.

curve() <strong>and</strong> bezier()<br />

The next two <strong>Processing</strong> curve functions I’ll look at are curve() <strong>and</strong> bezier(). I’ll consider<br />

these functions together, as they share some common implementation issues. As you’ve<br />

probably gathered by now, the implementation of curves is a little complex. Fortunately,<br />

<strong>Processing</strong>’s curve() <strong>and</strong> bezier() functions simplify curve generation quite a bit. These<br />

functions encapsulate pretty sophisticated mathematical ideas, including cubic polynomials—which<br />

you looked at a little—as well as some calculus. I’m not going to deal with the<br />

calculus here, nor mess directly with polynomials. The functions each require eight arguments,<br />

representing the x <strong>and</strong> y components of four points. The bezier() function uses<br />

two points (the first two parameters <strong>and</strong> the last two parameters) to describe the ends of<br />

the curves (anchor points), <strong>and</strong> two additional points (the middle four parameters) to control<br />

how the curve moves between the endpoints (referred to as control points). Here’s an<br />

example (shown in Figure 7-26). Please note that I took the liberty of connecting the control<br />

<strong>and</strong> anchor points with lines to help illustrate how the structure works.<br />

//Bézier sketch<br />

size(400, 400);<br />

background(255);<br />

smooth();<br />

/*I used Java's Point class, a convenient<br />

data type for holding x,y coords, with public<br />

access to x <strong>and</strong> y properties (pt.x or pt.y)*/<br />

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

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

Point pt3 = new Point(300, 100);<br />

Point pt4 = new Point(250, 300);<br />

//plot curve<br />

stroke(0);<br />

bezier(pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y, pt4.x, pt4.y);<br />

//draw control points connected to anchor points<br />

stroke(150);<br />

line(pt1.x, pt1.y, pt2.x, pt2.y);<br />

line(pt3.x, pt3.y, pt4.x, pt4.y);<br />

//control points<br />

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

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

//anchor points<br />

rectMode(CENTER);<br />

rect(pt1.x, pt1.y, 10, 10);<br />

rect(pt4.x, pt4.y, 10, 10);<br />

CURVES<br />

273<br />

7

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

Saved successfully!

Ooh no, something went wrong!