实验四 曲线的生成算法实现
一、实验目的
1. 掌握B样条曲线、Bezier曲线的定义;
2. 能编程实现N 次B样条曲线、Bezier曲线的绘制与显示。
二、实验内容
1. 编程实现二次Bezier曲线的绘制
三、算法描述
1. 贝塞尔曲线
贝塞尔曲线的参数向量表达式
通常,n+1个顶点定义一个n次多项式。
其中
称为伯恩斯坦(Bernstain)基函数。
四、实验步骤
1. 编程实现二次Bezier曲线的绘制;
2. 上机调试程序,显示最终绘图结果。
五、实验要求
1. 上交源程序;
2. 上交实验报告,实验报告内容如下:
(1)实验名称
(2)实验目的
(3)说明图形设计思想或程序流程图
(4)程序结果分析
二次Bezier曲线:
#include<stdio.h>
#include<graphics.h>
float decas(int degree,float coeff[],float t){
int r,i;
float t1;
float coeffa[10];
t1=1.0-t;
for(i=0;i<=degree;i++){
coeffa[i]=coeff[i];
}
for(r=1;r<=degree;r++){
for(i=0;i<=degree-r;i++){
coeffa[i]=t1*coeffa[i]+t*coeffa[i+1];
}
}
return (coeffa[0]);
}
void main(){
int i,n,k;
int gd = DETECT, gm = 0;
float t,x,y;
static float px[4];
static float py[4];
n=2;
k=3000;
px[0]=50; px[1]=140;px[2]=400;
py[0]=400; py[1]=20;py[2]=40;
initgraph(&gd, &gm, "e:\\TC\\BGI");
cleardevice();
setcolor(BLUE);
for (i=0;i<n;i++)
line(px[i],py[i],px[i+1],py[i+1]);
setcolor(YELLOW);
for(i=0;i<=k;i++){
t=(float)i/k;
x=decas(n,px,t);
y=decas(n,py,t);
if(i==0)
moveto(x,y);
else
lineto(x,y);
}
getch();
}
三次Bezier曲线:
#include <graphics.h>
void bezier_3(int color, double p[4][2])
{
double t,t1,t2,xt,yt;
int rate=200,x,y;
setcolor(color);
moveto(p[0][0],p[0][1]);
for (t=0;t<=1;t+=1.0/rate)
{
yt=1-t;
t1=yt*yt;
t2=3*yt*t;
xt=p[0][0]*t1*yt+p[1][0]*t2*yt+p[2][0]*t2*t+p[3][0]*t*t*t;
yt=p[0][1]*yt*t1+p[1][1]*t2*yt+p[2][1]*t2*t+p[3][1]*t*t*t;
x=(int)(xt);
y=(int)(yt);
lineto(x,y);
}
}
void main()
{
static double p[4][2]={50,400,140,20,400,40,635,420};
const NO=3; /*特征顶点数*/
int i;
int driver=DETECT,mode;
initgraph(&driver,&mode,"e:\\TC\\BGI");
cleardevice();
setcolor(BLUE);
moveto(p[0][0],p[0][1]);
for (i=1;i<NO;i++)
lineto(p[i][0],p[i][1]);
bezier_3(LIGHTRED,p);
getch(); /*按ESC键退出*/
closegraph();
}