I know using De Casteljau's algorithm is not the best way to draw a Bezier curve, but I need to implement it for an assignment I am defining my algorithm based on the following equations (from Drexel).
Where:
defines the control points.
I am trying to define the function to do the algorithm, but am struggling with where/how to incorporate the control points. The control points are defined by the user; as they interact with the program, a left click adds a new control point. My function currently looks as follows:
2Dpoint deCast(float t)
{
2Dpoint tempDC // Temporary value of point passed back to OpenGL draw function
tempDC.x = 0; tempDC.y = 0 // Initialize temporary value
int r,i;
int n = C->B.size(); // C is pointer to B vector, which is where the control points are stored in a 2D vector
for (r = 1; r<n, r++)
{
for (i = 0; i<n-r; i++)
{
// Calculation of deCast points goes here
}
}
}
Where 2Dpoint
is just a structure defined by a header file, C
is a pointer to the location of the control points, which are stored in a 2Dpoint
struct called B
(i.e the i
value of the control point vector is accessed by C -> B[i].x
and C -> B[i].y
). t
is provided to the function when it is implemented in my draw
function, as shown below.
void draw()
{
glColor3f(0.0f, 1.0f, 0.0f);
glLineWidth(2.0f);
glBegin(GL_LINE_STRIP);
float DCiter = 0;
while (DCiter <= 1.0)
{
2Dpoint DC = decast(DCiter);
glVertex2f(DC.x, DC.y);
DCiter = DCiter + 0.01;
}
}