资源简介
利用传入参数给定的控制顶点构成的顶点序列画出bezier曲线
代码片段和文件信息
#include
#include
#include
#define N 4 //控制顶点个数
double t;//参数t
typedef struct{
GLfloat x y;
}point;
int M = 0;
point a[N];
int multiply_n(int m int n)//求出二项式系数
{
int i j a;
if (m != 0)
{
a = 1;
for (i = m + 1; i <= n; i++)
{
a = a*i;
}
for (j = 1; j <= n - m; j++)
{
a = a / j;
}
return a;
}
else{
return 1;
}
}
double bernstein(int m int n double t1) //基函数bernstein调用函数multiply_n算出Bi n(t)
{
int i j;
double sum;
sum = multiply_n(m n);
for (i = 1; i <= m; i++)
{
sum = sum*t1;
}
for (j = 1; j <= n - m; j++)
{
sum = sum*(1 - t1);
}
return sum;
}
void myinit()
{
glClearColor(0.0 0.0 0.0 0.0);
glColor3f(1.0f 0.0 0.0);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0 640 0.0 480.0);
}
void drawDot(point x)//画出点
{
glBegin(GL_POINTS);
glVertex2f(x.x x.y);
glEnd();
glFlush();
}
void drawLine(point v point g)//连接两点画直线
{
glBegin(GL_LINES);
glVertex2f(v.x v.y);
glVertex2f(g.x g.y);
glEnd();
glFlush();
}
point bezier(int n double t1)//求出bezier曲线上参数t1对应点
{
point p1;
p1.x = 0.0;
p1.y = 0.0;
for (int i = 0; i <= n; i++)
{
p1.x += bernstein(i n t1)*
评论
共有 条评论