我必須使用貝塞爾曲線創建運動路徑。在運行下面的程序時,我得到了應該是我的運動路徑曲線的控制多邊形的值,我可以通過「printf」語句來檢查它們,但是我實際上並沒有得到曲線。使用貝塞爾曲線的運動路徑
我做了什麼錯了,我可怎麼解決呢?
#include "stdafx.h"
#include <gl/glut.h>
#include <cstdio>
struct point
{
float x;
float y;
};
// simple linear interpolation between two points
void lirp(point& dest, const point& a, const point& b, const float t)
{
dest.x = a.x + (b.x - a.x)*t;
dest.y = a.y + (b.y - a.y)*t;
}
// evaluate a point on a bezier-curve. t goes from 0 to 1.0
void bezier(point &dest, const point& a, const point& b, const point& c, const point& d, const float t)
{
point ab, bc, cd, abbc, bccd;
lirp(ab, a, b, t); // point between a and b
lirp(bc, b, c, t); // point between b and c
lirp(cd, c, d, t); // point between c and d
lirp(abbc, ab, bc, t); // point between ab and bc
lirp(bccd, bc, cd, t); // point between bc and cd
lirp(dest, abbc, bccd, t); // point on the bezier-curve
}
void Draw() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(20);
point a = { 700, 100 };
point b = { 650, 20 };
point c = { 600, 180 };
point d = { 550, 100 };
for (int i = 0; i<1000; ++i) //should plot 1000 points within the control polygons.
{
point p;
float t = static_cast<float>(i)/999.0;
bezier(p, a, b, c, d, t);
float p_x = p.x;
float p_y = p.y;
printf("%f %f\n", p_x, p_y);
glBegin(GL_POINTS);
glVertex3f(p_x, p_y, 0.0);
glEnd();
glutSwapBuffers();
}
}
void Timer(int Unused)
{
glutPostRedisplay();
glutTimerFunc(20, Timer, 0);
}
void Init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("Animation Test");
Init();
glutDisplayFunc(Draw);
Timer(0);
glutMainLoop();
return 0;
}
*笑*這貝塞爾代碼來自我的舊網站:http://cubic.org/docs/bezier.htm什麼從過去的爆炸。 –
哦!感謝您讓我瞭解曲線近似值。這是非常好的解釋。 – 2bit