Saturday, May 9, 2009

C program to implement Bezier Curve Drawing Algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int x,y,z;

void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];


for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }



printf("\n\n Enter four points : \n\n");

for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);

}

c[4][0]=c[0][0];
c[4][1]=c[0][1];

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");

ymax = 480;

setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}

setcolor(3);
n=3;

for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);

if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}

float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}

C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}

4 comments:

Anonymous said...

Hey Angad,this is very important program that you have uploaded and is very usefull for me since i am doing BCA from IP university myself, but is it anyhow possible for you to upload the outputs of these programs and do you also have the programs for Transformation in 2D,all 5 of them,those are the ones i am not able to get through.
Thanks

Anonymous said...

THANK YOU VERY MUCH

Anonymous said...

hi there is a little change in this prog pls change it as "theta" in rotation function scanf statement

Anonymous said...

Cubic bezier curve

#include
#include
int X()
{
int x;
printf("\nEnter the value of x: ");
scanf("%d",&x);
return x;
}
int Y()
{
int y;
printf("\nEnter the value of y: ");
scanf("%d",&y);
return y;
}
float Square(float u)
{
return (u*u);
}
float Cube(float u)
{
return (u*u*u);
}
int main()
{
int x1,x2,x3,x4,y1,y2,y3,y4;
float u=0.0,x,y,v1,v2,v3,v4;
int gd=DETECT,gm;
x1=X();
y1=Y();
x2=X();
y2=Y();
x3=X();
y3=Y();
x4=X();
y4=Y();
initgraph(&gd,&gm,NULL);
for(u=0.0;u<1.0;++u)
{
v1=Cube(1-u);
v2=3*Square(1-u);
v3=(1-u)*Square(1-u);
v4=Cube(u);
x=(x1*v1)+(x2*v2)+(x3*v3)+(x4*v4);
y=(y1*v1)+(y2*v2)+(y3*v3)+(y4*v4);
putpixel(x,y,7);
delay(500);
}
//closegraph();
getch();
return 0;
}