#include<graphics.h>/*加入c图形库*/
#include<conio.h>/*键盘控制*/
#include<stdlib.h>
#include<stdio.h>
float px[10]={50,90,150,120,220,300,380,320,450,500};
float py[10]={100,60,50,150,240,100,100,200,250,130};/*控制点*/
void B_spline()
{
float a0,a1,a2,a3,b0,b1,b2,b3;
int k,x,y;
float i,t,dt,n=10;
setcolor(WHITE);
dt=1/n;
for(k=0;k<10;k++)
{
if(k==0)
moveto(px[k],py[k]);
lineto(px[k],py[k]);
}/*特征多边形*/
setcolor(RED);
for(k=0;k<7;k++)
{
if(getch()==17)exit(1);/*读入键盘按键*/
a0=(px[k]+4*px[k+1]+px[k+2])/6;
a1=(px[k+2]-px[k])/2;
a2=(px[k]-2*px[k+1]+px[k+2])/2;
a3=-(px[k]-3*px[k+1]+3*px[k+2]-px[k+3])/6;
b0=(py[k]+4*py[k+1]+py[k+2])/6;
b1=(py[k+2]-py[k])/2;
b2=(py[k]-2*py[k+1]+py[k+2])/2;
b3=-(py[k]-3*py[k+1]+3*py[k+2]-py[k+3])/6;
for(i=0;i<n;i+=0.1)
{
t=i*dt;
x=a0+a1*t+a2*t*t+a3*t*t*t;
y=b0+b1*t+b2*t*t+b3*t*t*t;
if(i==0)
moveto(x,y);
lineto(x,y);
}
}
}
void main()
{
int driver,mode;
int gdriver=DETECT,gmode;
driver=DETECT;/*初始化显示模式参数*/
initgraph(&gdriver,&gmode,"d:\\tc");/*初始化显示为默认的640*480、16色模式*/
printf("Press any key to continue except 'Ctrl+Q' to quit.\n");
B_spline();
if(getch()==17)exit(1);/*读入键盘按键*/
closegraph();/*关闭图形模式*/
}