Bezier Curve
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define address "d:\\tc\\bgi"
struct Point{
double x,y;
}c[]={{100,100},{105,140},{140,105},{150,115}};
int n=3;
void draw_Bezier(struct Point[]);
Point getPU(double);
double com(int,int);
double b(int,int,double);
void main()
{
int sel=0;
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,address);
ABOVE:
cleardevice();
draw_Bezier(c);
char ch=getch();
if(ch==27) return;
sel=ch-'0';
if(sel<0||sel>3) sel=3;
c[sel].x=c[sel].x+1,c[sel].y=c[sel].y+1;
goto ABOVE;
}
void draw_Bezier(struct Point c[]){
Point Pu,oPu;
oPu.x=c[0].x,oPu.y=c[0].y;
setcolor(WHITE);
for(double u=0.1;u<=1.0;u+=0.1)
{
Pu=getPU(u);
line(oPu.x,oPu.y,Pu.x,Pu.y);
oPu=Pu;
}
setcolor(RED);
for(int i=0;i<=n;i++)
{
circle(c[i].x,c[i].y,2);
if(i>0) line(c[i-1].x,c[i-1].y,c[i].x,c[i].y);
}
}
Point getPU(double u){
Point p={0,0};
for(int i=0;i<=n;i++)
{
p.x=p.x+c[i].x*b(n,i,u);
p.y=p.y+c[i].y*b(n,i,u);
}
return p;
}
double b(int n,int k,double u)
{
return com(n,k)*pow(u,k)*pow(1-u,n-k);
}
double com(int n,int k)
{
double result=1;
for(int i=n;i>n-k;i--)
{
result=(result*i)/(n-i+1);
}
return result;
}