I managed to draw a bezier curve like:
glColor3f(0,1,0);
glBegin(GL_LINE_STRIP);
for (int i = 3; i < nPt; i+=3) {
glColor3f(0,0,0);
for (float k = 0; k < NLINESEGMENT+1; k++) {
float x = pow(1.0-k/NLINESEGMENT,3)*ptList[i-3].x +
3*(k/NLINESEGMENT)*pow(1.0-k/NLINESEGMENT, 2) * ptList[i-2].x +
3*(1.0-k/NLINESEGMENT)*pow(k/NLINESEGMENT, 2) * ptList[i-1].x +
pow(k/NLINESEGMENT, 3)*ptList[i].x;
float y = pow(1.0-k/NLINESEGMENT,3)*ptList[i-3].y +
3*(k/NLINESEGMENT)*pow(1.0-k/NLINESEGMENT, 2) * ptList[i-2].y +
3*(1.0-k/NLINESEGMENT)*pow(k/NLINESEGMENT, 2) * ptList[i-1].y +
pow(k/NLINESEGMENT, 3)*ptList[i].y;
glVertex2d(x,y);
}
}
glEnd();
Now I want to add tangent arrows for each point, how can I do that? I am given a function that draws an arrow. So I believe I need to just rotate the reference frame and draw that arrow. But how do I compute the rotation? I think I need to differenciate the equations, but the question still remains, how do I use that?
UPDATE
As every 4th point is put, a curve is drawn.
I am supposed to achieve something like below
UPDATE 2
Ok I made an attempt at drawing the tangents like:
glColor3f(0,1,0);
for (int i = 3; i < nPt; i+=3) {
for (int n = 0; n < NOBJECTONCURVE; n++) {
float t = (float)n/NOBJECTONCURVE;
float x0 = points[i-3].x,
x1 = points[i-2].x,
x2 = points[i-1].x,
x3 = points[i].x;
float y0 = points[i-3].y,
y1 = points[i-2].y,
y2 = points[i-1].y,
y3 = points[i].y;
float x = pow(1.0-t, 3) * points[i-3].x +
3 * t * pow(1.0 - t, 2) * points[i-2].x +
3 * (1.0 - t) * pow(t, 2) * points[i-1].x +
pow(t, 3)*points[i].x;
float y = pow(1.0-t, 3) * points[i-3].y +
3 * t * pow(1.0 - t, 2) * points[i-2].y +
3 * (1.0 - t) * pow(t, 2) * points[i-1].y +
pow(t, 3)*points[i].y;
float dx = -3*(1-t)*x0 + 3*x1*((2*t)*(t-1)+pow((1-t),2)) + 3*x2*(2*t*(1-t)-pow(t,2)) + 3*pow(t,2)*x3;
float dy = -3*(1-t)*y0 + 3*y1*((2*t)*(t-1)+pow((1-t),2)) + 3*y2*(2*t*(1-t)-pow(t,2)) + 3*pow(t,2)*y3;
float angle = atan(dy/dx);
glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(angle * 180 / 3.14159265, 0, 0, 1);
drawRightArrow();
glPopMatrix();
}
}
But as you can see the tangents appear to be incorrect especially in the middle of a bezier curve?