我设法绘制了一条贝塞尔曲线,例如:
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();
现在我想为每个点添加切线箭头,我该怎么做?我得到了一个绘制箭头的函数。所以我相信我只需要旋转参考框架并绘制那个箭头。但是我如何计算旋转?我想我需要区分方程,但问题仍然存在,我该如何使用它?
更新
每放置第 4 个点,就会绘制一条曲线。
我应该实现以下目标
更新 2
好的,我尝试绘制切线,例如:
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();
}
}
但正如您所见,切线似乎不正确,尤其是在贝塞尔曲线的中间?