GET KTU B.TECH RESULTS ON MAIL

Subscribe to our mailing list and get KTU results in your mail box. CLICK HERE

BEZIER CURVE - Computer Graphics Lab Programs in C Programming Language

Computer Graphics Lab Programs in C Programming Language- BEZIER CURVE

AIM
Write a C program to implement Bezier Curves.

ALGORITHM
1. Start.
2. Read the number of control points(no) and set the number n=no-1
3. for I as 0 to n do step 5 and 6
4. Read the end point to xk[i] and yk[i]
5. Set i=i+1
6. Set cpx=xk[0] and cpy=yk[0]
7. for u is 0.1 to 0.3 repeat as follows
8. setxu as 0 and yu as 0
9. fori =n to 0 repeat as follows
10. If i is not equal 0 then set xu=xu+xk[i]*Bezier(n,i) * u^i*(1-u)^n-i
Else set xu=xu+xk[i]*bezier(n,i)*(1-u)^n-i
11. fori=n to 0 do
12. If i is not equal to 0 then set yu=yu+yk[i]*Bezier(n,i)*u^i(1-u)^i-1
Else set yu=yu+yk[i]*Bezier(n,i) (1-u)^n-i
13. Draw the curve based on the values calculated
14. Input the control points and interval d. set n=d-1
15. Inside the Bezier function calculate n!/(i!)*(n-1)! By calling the factorial function for each variable.
16. In the factorial function, initialize fact=1 and get the number into i.
17. Calculate fact=fact*i recursively and decrement i till 1 is reached.
18. End.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main(){
intgd=DETECT,gm;
int x[4],y[4],px,py,i,n;
double t;
initgraph(&gd,&gm,"D:\\TC\\BGI");
printf("Enter the no of control points");
scanf("%d",&n);
printf("Enter the control points of bezier curve: ");
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],GREEN);
}
for(t=0.0;t<=1.0;t+=0.001){
px=(1-t)*(1-t)*(1-t)*x[0]+3*t*(1-t)*(1-t)*x[1]+3*t*t*(1-t)*x[2]+t*t*t*x[3];
py=(1-t)*(1-t)*(1-t)*y[0]+3*t*(1-t)*(1-t)*y[1]+3*t*t*(1-t)*y[2]+t*t*t*y[3];
putpixel(px,py,WHITE);
delay(2);
}
getch();
closegraph();

}
Previous
Next Post »