Reputation: 35
I implemented a Bezier curve drawing function like this:
Vector Bezier(float t)
{
Vector rt(0,0);
int n = length-1;
for(int i=0;i<length;i++)
{
float Bi = 1;
for(int j = 1;j<=i;j++)
{
Bi *= (float) (n-j+1)/j;
}
Bi *= pow(t,i) * pow(1-t, n-i);
rt = rt + (Cpoints[i] * Bi);
}
return rt;
}
void drawBezier()
{
int segments = 100;
glBegin( GL_LINE_STRIP );
for(int i=0;i<segments;i++)
{
float t = (float) i / segments;
Vector p = Bezier(t);
glVertex2f(p.x, p.y);
}
glEnd( );
}
CPoints is an array containing the coordinates of the Control Points, length is the number of Control Points. The question is, how do I make it a closed Bezier curve, like this:
Upvotes: 2
Views: 2656
Reputation:
Simply use an additional segment that connects the last endpoint to the first (ex: duplicating the first control point).
A single bezier spline segment, whether it's cubic or quadratic or quartic, can't represent that kind of closed shape. Yet multiple segments can.
So you typically don't want to modify your multi-segment curve drawing function, per se, but rather the control points you feed into it. Although you could modify the drawing function to accept a flag to draw a closing segment, it's probably easier is to just think of it as a problem associated with the control points/curve segments you provide as input.
Upvotes: 5