<< Chapter < Page | Chapter >> Page > |
As opposed to the
curve()
, in
the
bezier()
function call the two control points
specified by the four middle parameters are not points touchedby the curve. They only serve to define the shape of the
approximating
Bézier curve , which has the following interesting
properties:
stroke(255, 0, 0);
line(93, 40, 10, 10);line(90, 90, 15, 80);
stroke(0, 0, 0);noFill();
bezier(93, 40, 10, 10, 90, 90, 15, 80);
the control points lay on the tangent passing by the
extremal points. In order to have an arbitrary number ofcontrol points one must use the
bezierVertex()
to specify each point within a block delimited by
beginShape()
and
endShape()
. In this way, an arbitrarily
involute curve can be traced in the 3D space. In 2D, thefunction
bezierVertex()
has six parameters that
correspond to the coordinates of two control points and oneanchor point. The first invocation of
bezierVertex()
has to be preceded by a call to
vertex()
which fixes the first anchor point of
the curve.
There are other methods that allow to read the coordinates or the slope of the tangent to an arbitrary point of aBézier or spline curve. Such point can be specified by a parameter that can go from (first extreme) to (second extreme). It is also possible to set the precision of approximating orinterpolating curves in 3D. For details see the Processing reference manual .
The Processing sketch in table shows the difference between the spline interpolating curve and the Bézier curve.
applet that compares the Bézier curve (red) and theinterpolating spline (black) |
void setup() {
c1x = 120;c1y = 110;
c2x = 50;c2y = 70;
background(200);stroke(0,0,0);
size(200, 200);}
int D1, D2;int X, Y;
int c1x, c1y, c2x, c2y;void draw() {
if (mousePressed == true) {X = mouseX; Y = mouseY;
// selection of the point that is modifiedD1 = (X - c1x)*(X - c1x) + (Y - c1y)*(Y - c1y);
D2 = (X - c2x)*(X - c2x) + (Y - c2y)*(Y - c2y);if (D1<D2) {
c1x = X; c1y = Y;}
else {c2x = X; c2y = Y;
}}
background(200);stroke(0,0,0);
strokeWeight(1);noFill();beginShape();
curveVertex(10, 10);curveVertex(10, 10);
curveVertex(c2x, c2y);curveVertex(c1x, c1y);
curveVertex(190, 190);curveVertex(190, 190);
endShape();stroke(255,30,0);bezier(10,10,c2x,c2y,c1x,c1y,190,190);
strokeWeight(4);point(c1x,c1y);
point(c2x,c2y);} |
fill()
, which also gives the
possibility to set the degree of transparency.
triangle()
,
whose six parameters correspond to the coordinates of thevertices in the image window. Even though each triangle is
defined in 2D, it can be rotated and translated in the 3Dspace, as it happens in the Processing sketch
void setup(){ size(200, 200, P3D); fill(210, 20,
20); }float angle = 0;
void draw(){background(200); // clear image
stroke(0,0,0);angle += 0.005;
rotateX(angle);triangle(10,10,30,70,80,80);
}
Notification Switch
Would you like to follow the 'Media processing in processing' conversation and receive update notifications?