Computer Graphics Lab Manual
Computer Graphics Lab Manual
EXPERIMENT NO. 1
AIM : - To Draw a Line Using Digital Differential Analyzer(DDA) Line Drawing Algorithm
THEORY :- In any 2-Dimensional plane if we connect two points (x0, y0) and (x1, y1), we get a
line segment. But in the case of computer graphics we cannot directly join any two coordinate
points, for that we should calculate intermediate point’s coordinate and put a pixel for each
intermediate point, of the desired color with help of functions like putpixel(x, y, K) in C, where
(x,y) is our co-ordinate and K denotes some color. For using graphics functions, our system
output screen is treated as a coordinate system where the coordinate of the top-left corner is (0, 0)
and as we move down our x-ordinate increases and as we move right our y-ordinate increases for
any point (x,y). Now, for generating any line segment we need intermediate points and for
calculating them we have can use a basic algorithm called DDA (Digital differential analyzer) line
generating algorithm.
DDA can draw a) Vertical & Horizontal Lines
b) +450 or -450
c) Lines with other orientation
ALGORITHM :-
1. Read the end points of a line (x1, y1) & (x2, y2).
2. If the points are same, plot the points & exit.
3. △x = | x2-x1 | & △y = | y2-y1 |
4. if abs(△ x) ≥ abs(△y) then
Steps =△x
Else
Steps=△y
5. x inc = △x / steps.
6. y inc = △y /steps
7. initialize (x, y) with (x1,y1)
x = x1
y = y1
8. plot the coordinate (x, y)
9. initialize counter i=1
10. start the loop
x = x + x inc
y = y+ y inc
Plot the coordinate(x, y)
11. Continue the loop till the counter i<= steps
12. Stop.
PROGRAM :-
OUTPUT: -
AIM : - To Plot a line Using Bresenham’s Line Drawing Algorithm with +ve Slope
THEORY :- The idea of Bresenham's algorithm is to avoid floating point multiplication and
addition to compute mx + c, and then computing round value of (mx + c) in every step. In
Bresenham's algorithm, we move across the x-axis in unit intervals. We always increase x by 1,
and we choose about next y, whether we need to go to y+1 or remain on y. In other words, from
any position (Xk, Yk) we need to choose between (Xk + 1, Yk) and (Xk + 1, Yk + 1). We would like
to pick the y value (among Yk + 1 and Yk) corresponding to a point that is closer to the original
line.
We need a decision parameter to decide whether to pick Yk + 1 or Yk as next point. The idea is to
keep track of slope error from previous increment to y. If the slope error becomes greater than 0.5,
we know that the line has moved upwards one pixel, and that we must increment our y coordinate
and readjust the error to represent the distance from the top of the new pixel – which is done by
subtracting one from error. The above algorithm still includes floating point arithmetic. To avoid
floating point arithmetic, consider the value below value m.
We also change slope_error to slope_error * (x2 - x1). To avoid comparison with 0.5, we further
change it to slope_error * (x2 - x1) * 2.
1. Read the line end points (x1,y1) & (x2,y2) such that they are not equal.
2. If the points are same, plot the points & exit (Else continue with the
calculation)
3. Calculate
△x = |x2-x1|
△y = |y2-y1|
4. [initialize the starting point]
x=x1
y=y1
5. e = 2 * △y - △x
[initialize value of decision variable or error to compensate for non zero
intercepts]
6. i = 1[initialize counter]
7. plot(x , y)
8. while (e ≥ 0)
{
y = y+1
e = e – 2 * △x
}
x = x+1
e = e+2 * △y
9. i = i+1
10. if ( i ≤ △x ) then go to step 6.
11. Stop.
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
return 0;
}
OUTPUT:-
Aim : Program to draw a thick line using Bresenhams Thick Line Drawing Method
THEORY :-
Bresenham's Thick line algorithm is an algorithm that determines the points of an n-dimensional
raster that should be selected in order to form a close approximation to a straight line between two
points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen),
as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations
in standard computer architectures. It is an incremental error algorithm. It is one of the earliest
algorithms developed in the field of computer graphics. An extension to the original algorithm
may be used for drawing circles .
ALGORITHM
while(i<wx);
{
wx=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1));
line(x1-i,y1,x2-i,y2);
line( x1+i,y1,x2+i,y2);
i= i+1;
}
}
N Y
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float wy,wx,x1,y1,x2,y2;
int i,thickness;
initgraph(&gd,&gm,"..\\bgi");
printf("Enter the coordinates for the line:\n");
printf("X1:");
scanf("%f",&x1);
printf("Y1:");
scanf("%f",&y1);
printf("X2:");
scanf("%f",&x2);
printf("Y2:");
scanf("%f",&y2);
printf("Enter the required thickness:");
scanf("%d",&thickness);
cleardevice();
line(x1,y1,x2,y2);
if((y2-y1)/(x2-x1)<1)
{
wy=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-x1));
for(i=0;i<wy;i++)
{
line(x1,y1-i,x2,y2-i);
line(x1,y1+i,x2,y2+i);
}
}
else
{
wx=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1));
for(i=0;i<wx;i++)
{
line(x1-i,y1,x2-i,y2);
line(x1+i,y1,x2+i,y2);
}
}
printf("This is the line of thickness%d units.\n",thickness);
getch();
}
OUTPUT: -
CONCLUSION: We have studied and successfully plotted Thick Line for Coordinates
X1,Y1=(200,200) and X2,Y2=(300,300) and Thickness = 10 points using Bresenhams Thick Line
drawing Algorithm
THEORY: -
We need to plot the perimeter points of a circle whose center co-ordinates and radius are given
using the Mid-Point Circle Drawing Algorithm.
We use the above algorithm to calculate all the perimeter points of the circle in the first octant
and then print them along with their mirror points in the other octants. This will work only
because a circle is symmetric about it’s centre.
The boundary conditions for Midpoint Circle Drawing Algorithm are given below
For any given pixel (x, y), the next pixel to be plotted is either (x, y+1) or (x-1, y+1). This can be
decided by following the steps below.
1. Find the mid-point p of the two possible pixels i.e (x-0.5, y+1)
2. If p lies inside or on the circle perimeter, we plot the pixel (x, y+1), otherwise if it’s
outside we plot the pixel (x-1, y+1)
Boundary Condition : Whether the mid-point lies inside or outside the circle can be decided by
using the formula:-
Now,
xk+1 = xk or xk-1 , yk+1= yk +1
The first point to be plotted is (r, 0) on the x-axis. The initial value of P is calculated as follows:-
P1 = (r – 0.5)2 + (0+1)2 – r2
= 1.25 – r
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float p;
int i,x,y;
int xc,yc,r;
int gd=DETECT,gm; /*initialise graphics ----------------*/
initgraph(&gd,&gm,"..\\bgi");
printf("Enter the radius of the circle:"); /*Read the radius --------------*/
scanf("%d",&r);
printf("Enter the center coordinates of the circle:"); /*Read centre coordinates --------------*/
scanf("%d%d",&xc,&yc);
x=0;
y=r;
p=1.25-r;
do
{
putpixel(xc+x,yc+y,20);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,20);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,20);
putpixel(xc-y,yc+x,15);
putpixel(xc+y,yc-x,20);
putpixel(xc-y,yc-x,15);
if(p<0)
{
x=x+1;
y=y;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
}
while(x<y);
OUTPUT
CONCLUSION: We have studied and successfully plotted Midpoint Circle drawing Algirthm for
Centre Coordinates X,Y=(150,150) and radius = 100 using DDA Line drawing Algorithm
THEORY:
In Mid-point Ellipse drawing algorithm we use 4 way symmetry of the ellipse to generate it.We
perform calculations for one part and the other three parts will be drawn by using 4-way
symmetry.
(-x , y) (x , y)
For given parameter Rx and Ry (radii of ellipse) and (Xc,Yc)-center of ellipse, we determine the
point (x,y) for an ellipse in standard position centered on the origin and then we shift the points so
the ellipse is centered at (Xc,Yc).
Let us consider one quarter of an ellipse. The curve is divided into two regions. In region I, the
slope on the curve is greater than –1 while in region II less than –1.
Slope = -1
Region 1
Region 2
Ellipse
ALGORITHM
4. initialize dx and dy as
dx=2 * ry2 * x
dy=2 * rx2 * y
5. do
{
plot (x , y)
if (d1<0)
{
x=x+1
y=y
dx=dx+2*ry2
d1=d1+dx+ ry2
[d1=d1+2*ry2*x+2*ry2]
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
long d1, d2;
int i,gd,gm,x,y,xc,yc;
long rx, ry, rxsq, rysq, tworxsq, tworysq, dx, dy;
clrscr();
printf("enter the center coordinates of the ellipse:");
scanf("%d%d",&xc,&yc);
printf("enter the x radius of the ellipse:"); /* read the radius x and y ---------------------------*/
scanf("%ld",&rx);
printf("enter the y radius of the ellipse:");
scanf("%ld",&ry);
detectgraph(&gd,&gm); /* initialise graphics ----------------------------*/
initgraph(&gd,&gm,"..\\bgi");
rxsq=rx*rx;
rysq=ry*ry;
tworxsq=2*rxsq;
tworysq=2*rysq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+ tworysq;
d1=d1+dx+rysq;
}
else
{
x=x+1;
y=y-1;
dx=dx+tworysq;
dy=dy-tworxsq;
d1=d1+dx-dy+rysq;
}
}
while(dx<dy);
OUTPUT
THEORY:
1. Get four control points say A(xA, yA), B(xB, yB), C(xC, yC), D(xD, yD).
2. Divide the curve represented by points say A, B, C and D in two sections.
xAB =(xA+xB)/2
yAB = (yA+yB)/2
xBC = (xB+xC)/2
yBC = (yB+yC)/2
xCD = (xC+xD)2
yCD = (yC+yD)/2
xABC = (xAB+yBC)/2
yABC = (yAB+yBC)/2
xBCD = (xBC+yCD)/2
yBCD = (yBC+yCD)/2
xABCD = (xABC+xBCD)/2
yABCD = (yABC+yBCD)/2
3. Repeat the step 2 for section A, AB, ABC and ABCD and section ABCD, BCD, CD
and D.
4. Repeat step 3 until we have section so short that they can be replaced by straight lines.
5. Replace small section by straight by lines.
6. Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int maxx,maxy;
int gd=DETECT,gm;
float xxx[4][2];
void line1(float x2,float y2)
{
setcolor(RED);
line(xxx[0][0],xxx[0][1],x2,y2);
xxx[0][0]=x2;
xxx[0][1]=y2;
}
void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if(n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab=(xxx[0][0]+xb)/2;
yab=(xxx[0][1]+yb)/2;
xbc=(xb+xc)/2;
ybc=(yb+yc)/2;
xcd=(xc+xd)/2;
ycd=(yc+yd)/2;
xabc=(xab+xbc)/2;
yabc=(yab+ybc)/2;
xbcd=(xbc+xcd)/2;
ybcd=(ybc+ycd)/2;
xabcd=(xabc+xbcd)/2;
yabcd=(yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
}
}
void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("can't detect graph");
exit(1);
OUTPUT:
THEORY:
Boundary Fill is another seed fill algorithm in which edges of the polygon are drawn. Then
starting with some seed any point inside the polygon we examine the neighboring pixels to check
whether the boundary pixel is reached. If boundary pixels are not reached, pixels are highlighted
and process is continued until boundary pixels are reached.
Boundary Fill Algorithm starts at a pixel inside the polygon to be filled and paints the interior
proceeding outwards towards the boundary. This algorithm works only if the color with which the
region has to be filled and the color of the boundary of the region are different. If the boundary is
of one single color, this approach proceeds outwards pixel by pixel until it hits the boundary of
the region.
Boundary Fill Algorithm is recursive in nature. It takes an interior point(x, y), a fill color, and a
boundary color as the input. The algorithm starts by checking the color of (x, y). If it’s color is not
equal to the fill color and the boundary color, then it is painted with the fill color and the function
is called for all the neighbours of (x, y). If a point is found to be of fill color or of boundary color,
the function does not call its neighbours and returns. This process continues until all points up to
the boundary color for the region have been tested.
The boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.
4-connected pixels : After painting a pixel, the function is called for four neighboring points.
These are the pixel positions that are right, left, above and below the current pixel. Areas filled by
this method are called 4-connected.
1. current = getpixel(x,y).
then putpixel(x,y,fill)
boundary_fill(x,y+1,fill,bound);
boundary_fill(x+1, y+1,fill,bound);
3. END
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void b_fill(int,int,int,int);
void main()
{
int x1,x2,y1,y2,x,y;
int gd,gm;
/* initialise graphics mode
------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
printf("Enter object coordinates");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
printf("Enter seed pixel");
scanf("%d%d",&x,&y);
if(x<x1||x>x2||y<y1||y>y2)
printf("Enter seed pixel within given coordinaes of object");
else
b_fill(x,y,4,15);
getch();
closegraph();
}
void b_fill(seed_x,seed_y,foreground_col,background_col)
{
if(getpixel(seed_x,seed_y)!=background_col &&
getpixel(seed_x,seed_y)!=foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
b_fill(seed_x+1,seed_y,foreground_col,background_col);
b_fill(seed_x-1,seed_y,foreground_col,background_col);
b_fill(seed_x,seed_y+1,foreground_col,background_col);
b_fill(seed_x,seed_y-1,foreground_col,background_col);
b_fill(seed_x+1,seed_y+1,foreground_col,background_col);
b_fill(seed_x-1,seed_y-1,foreground_col,background_col);
b_fill(seed_x+1,seed_y-1,foreground_col,background_col);
b_fill(seed_x-1,seed_y+1,foreground_col,background_col);
}
}
THEORY:
Transformation means changing some graphics into something else by applying rules. We can
have various types of transformations such as translation, scaling up or down, rotation, shearing,
etc. When a transformation takes place on a 2D plane, it is called 2D transformation.
Transformations play an important role in computer graphics to reposition the graphics on the
screen and change their size or orientation.
Homogenous Coordinates
To perform a sequence of transformation such as translation followed by rotation and scaling, we
need to follow a sequential process −
Using standard trigonometric the original coordinate of point P(X, Y) can be represented as −
X=rcosϕ......(1)X=rcosϕ......(1)
Y=rsinϕ......(2)Y=rsinϕ......(2)
Same way we can represent the point P’ (X’, Y’) as −
x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......(3)x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......(3)
y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......(4)y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......(4)
Substituting equation (1) & (2) in (3) & (4) respectively, we will get
x′=xcosθ−ysinθx′=xcosθ−ysinθ
y′=xsinθ+ycosθy′=xsinθ+ycosθ
Representing the above equation in matrix form,
[X′Y′]=[XY][cosθ−sinθsinθcosθ]OR[X′Y′]=[XY][cosθsinθ−sinθcosθ]OR
P’ = P . R
Scaling
To change the size of an object, scaling transformation is used. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor to get the desired result.
Let us assume that the original coordinates are (X, Y), the scaling factors are (SX, SY), and the
produced coordinates are (X’, Y’). This can be mathematically represented as shown below −
X' = X . SX and Y' = Y . SY
The scaling factor SX, SY scales the object in X and Y direction respectively. The above
equations can also be represented in matrix form as below −
(X′Y′)=(XY)[Sx00Sy](X′Y′)=(XY)[Sx00Sy]
OR
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.
B=(x2,y2).
B’= T*B
6. Stop.
B=(x2,y2).
B’= R*B
6. Stop.
B=(x2,y2).
B’= S*B
6. Stop.
B=(x2,y2).
shear as Shy.
3.
i) for x-shear :-
B’=B;
A’=A;
4 Return Sheared object (line).
4. Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
int main()
{
int i, x, y, tx, ty, sx, sy, angle=10, xmax, ymax, xmid, ymid, op;
int gd,gm;
float p1[10] = { 50,50,
100,50,
100,100,
50,100,
50,50
};
int pi[10];
float b[3][3] = { 1,0,0,
0,1,0,
0,0,1
};
int c[1][1];
float a[1][1];
printf("\n Select The Transformation:");
printf("\n 1: Translation");
printf("\n 2: Rotation");
printf("\n 3: Scaling");
printf("\n 4: Rotation about arbitrary point");
printf("\n Enter the option : ");
scanf("%d", &op);
switch(op)
{
case 1: printf("\n Enter x translation : ");
scanf("%d", &tx);
printf("\n Enter y translation : ");
scanf("%d", &ty);
b[0][0] = 1;
b[0][1] = 0;
b[0][2] = 0;
b[1][0] = 0;
b[1][1] = 1;
b[1][2] = 0;
b[2][0] = tx;
break;
case 2: printf("\n Enter Rotation angle : ");
scanf("%d", &angle);
b[0][0] = cos(angle*3.142/180);
b[0][1] =-sin(angle*3.142/180);
b[0][2] = 0;
b[1][0] = sin(angle*3.142/180);
b[1][1] = cos(angle*3.142/180);
b[1][2] = 0;
b[2][0] = 0;
b[2][1] = 0;
b[2][2] = 1;
break;
b[0][0] = sx;
b[0][1] = 0;
b[0][1] = 0;
b[1][0] = 0;
b[1][1] = sy;
b[1][2] = 0;
b[2][0] = 0;
b[2][1] = 0;
b[2][2] = 1;
break;
tx = x;
ty = y;
b[0][0] = cos(angle*3.14/180);
b[0][1] = -sin(angle*3.142/180);
b[1][0] = sin(angle*3.142/180);
b[1][1] = cos(angle*3.142/180);
b[1][2] = 0;
}
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
xmax = getmaxx();
ymax = getmaxy();
xmid = xmax/2;
ymid = ymax/2;
setcolor(20);
line(xmid,0,xmid,ymax); //Draw y axis
line(0,ymid,xmax,ymid); //Draw x axis
setcolor(4);
for(i=0;i<8;i=i+2)
{
line(p1[i] + xmid,ymid - p1[i+1], xmid + p1[i+2], ymid - p1[i+3]);
}
for(i=0;i<9;i=i+2)
{
a[0][0] = p1[i];
a[0][1] = p1[i+1];
c[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]+b[2][0];
c[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1]+b[2][1];
pi[i] = c[0][0];
pi[i+1] = c[0][1];
}
setcolor(15);
for(i=0; i<8; i=i+2)
{
line(xmid + pi[i], ymid - pi[i+1], xmid + pi[i+2], ymid - pi[i+3]);
}
getch();
closegraph();
return(0);
}
AIM : Program to perform line clipping using Cohen and Sutherland Subdivision Line
Clipping
THEORY: Given a set of lines and a rectangular area of interest, the task is to remove lines
which are outside the area of interest and clip the lines which are partially inside the area.
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently
determines the lines and portions of lines that are inside the given rectangular area.
For any endpoint ( x , y ) of a line, the code can be determined that identifies which region the
endpoint lies. The code's bits are set according to the following conditions:
The sequence for reading the codes' bits is LRBT (Left, Right, Bottom, Top).
Once the codes for each endpoint of a line are determined, the logical AND operation of the codes
determines if the line is completely outside of the window. If the logical AND of the endpoint
codes is not zero, the line can be trivally rejected. For example, if an endpoint had a code of 1001
while the other endpoint had a code of 1010, the logical AND would be 1000 which indicates the
line segment lies outside of the window. On the other hand, if the endpoints had codes of 1001
and 0110, the logical AND would be 0000, and the line could not be trivally rejected.
1. Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0
(Both points are inside the rectangle)
2. Completely outside the given rectangle : Both endpoints share at least one outside region
which implies that the line does not cross the visible region. (bitwise AND of endpoints !=
0).
3. Partially inside the window : Both endpoints are in different regions. In this case, the
algorithm finds one of the two points that is outside the rectangular region. The intersection
of the line from outside point and rectangular window becomes new corner point and the
algorithm repeats
5. Determine the intersecting edge of the clipping window by inspecting the region codes of
two end points.
a. If region codes for both end points are non-zero, find intersection points P1 and P2
with boundary edges of clipping window with respect to it.
b. If region code for any one end point is non zero then find intersection points P1 or
P2 with the boundary edge of the clipping window with respect to it.
6. Divide the line segment considering intersection points.
7. Reject the line segment if any one end point of it appears outsides the clipping
window.
8. Draw the remaining line segments.
9. Stop.
FLOW CHART
PROGRAM :