COMPUTER GRAPHICS - Implement Bezier Curve
A.1 Aim:
Implement Bezier Curve using n Control Points.
A.2 Prerequisite:
1. C Language.
2. Geometric Concepts.
3. Curve Generation.
A.3 Outcome:
After successful completion of this experiment students will be able to,
Implementation of curve and fractal generation.
A.4 Theory:
Bezier curve is discovered by the French engineer Pierre Bézier. These curves can be
generated under the control of other points. Approximate tangents by using control
points are used to generate curve. The Bezier curve can be represented mathematically
as −
∑k=0nPiBni(t)∑k=0nPiBin(t)
Where pipi is the set of points and Bni(t)Bin(t) represents the Bernstein polynomials
which are given by −
Bni(t)=(ni)(1−t)n−itiBin(t)=(ni)(1−t)n−iti
Where n is the polynomial degree, i is the index, and t is the variable.
The simplest Bézier curve is the straight line from the point P0P0 to P1P1. A quadratic
Bezier curve is determined by three control points. A cubic Bezier curve is determined
by four control points.
A.5 Procedure:
Basic Algorithm
The objective here is to find points in the middle of two nearby points and iterate this
until we have no more iterations. The new values of points will give us the curve. The
famous Bezier equation is the exact formulation of this idea. Here is the algorithm:
Step 1: Select a value t Î [0,1]. This value remains constant for the rest of the steps.
Step 2: Set Pi[0] (t) = Pi, for i = 0,...,n.
Step 3: For j= 0,...,n, set for i = j,...,n.
Step 4: g (t) = Pn[n] (t)
CODE :
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
int fact(int a)
{
int i,f=1;
for(i=1;i<=a;i++)
f=f*i;
return f;
}
void main()
{
int x[100],y[100],i,n;
int tempx,tempy,ncr;
double put_x=0,put_y=0,t;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter number of control points ");
scanf("%d",&n);
printf("\nPlease enter x and y coordinates\n");
for(i=0;i<n;i++)
{
scanf("%d %d",&x[i],&y[i]);
putpixel(x[i],y[i],3); // Control Points
}
for(t=0.0;t<=1.0;t=t+0.001) // t always lies between 0 and 1
{
tempx=n-1;
tempy=n-1;
put_x=0;
put_y=0;
for(i=0;i<n;i++)
{
ncr=fact(n-1)/(fact(i)*fact(n-1-i));
if(i==n-1)
{
put_x=put_x+(pow(t,n-1)*x[i]);
put_y=put_y+(pow(t,n-1)*y[i]);
}
else
{
put_x=(pow(t,i)*ncr*pow(1-t,tempx)*x[i])+put_x;
put_y=(pow(t,i)*ncr*pow(1-t,tempy)*y[i])+put_y;
tempx=tempx-1;
tempy=tempy-1;
}
}
putpixel(put_x,put_y, WHITE); // putting pixel
}
getch();
closegraph();
}
Output:
Question of Curiosity
Q.1] Explain the properties of Bezier Curve.
Ans : They generally follow the shape of the control polygon, which consists of the segments joining the control points.
They always pass through the first and last control points.
They are contained in the convex hull of their defining control points.
Q.2] Write the advantages and disadvantages of Bezier Curve.
Ans:
Advantages of Bezier Curve:
The advantage of Bezier curves is the efficiency of computation, determination at the lower degrees of handles.
The Bezier curves can be specified with boundary conditions, with a characterizing matrix or with blending function.
The curve generally follows the shape of a defining polygon.
These are found in painting and drawing packages as well as in CAD applications.
Disadvantages of Bezier Curve:
It although unlimited but contains a finite number of control points.
There are many curves which cannot be completely expressed by Bezier curves.
Most cubic rational Bezier curves may be included in that class, and most curves of higher degree also.
It is liable to approximate in nature.
Q.3] Name the types of Bezier Curve.
Ans : There are three types of a bezier curve, they are as follows :
Simple Bezier Curve.
Quadric Bezier Curve. And the last one is
Cubic Bezier Curve.
Comments
Post a Comment