Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Computer Graphics Lab Manual

Uploaded by

deltagaming56432
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Computer Graphics Lab Manual

Uploaded by

deltagaming56432
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Computer Engineering / SSJCET, Asangaon

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

CG / CSL – 402 / SEM - IV Page 01


Computer Engineering / SSJCET, Asangaon

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.

CG / CSL – 402 / SEM - IV Page 02


Computer Engineering / SSJCET, Asangaon
FLOW CHART :-

CG / CSL – 402 / SEM - IV Page 03


Computer Engineering / SSJCET, Asangaon

CG / CSL – 402 / SEM - IV Page 04


Computer Engineering / SSJCET, Asangaon

PROGRAM :-

#include <graphics.h> dy = (y2 - y1);


#include <iostream.h> if(abs(dx) >= abs(dy))
#include <math.h> step = abs(dx);
#include <dos.h> else
#include <conio.h> step = abs(dy);
dx = dx / step;
void main( ) dy = dy / step;
{ x = x1;
float x, y, x1, y1, x2, y2, dx, dy, step; y = y1;
int i, gd = DETECT, gm; i = 1;
initgraph(&gd, &gm, while(i <= step) {
"C:\\TURBOC3\\BGI"); putpixel(x, y, 5);
x = x + dx;
cout << "Enter the value of x1 and y1 : "; y = y + dy;
cin >> x1 >> y1; i = i + 1;
cout << "Enter the value of x2 and y2: "; delay(100);
cin >> x2 >> y2; }
getch();
dx = (x2 - x1); closegraph();
}

OUTPUT: -

CONCLUSION: We have studied and successfully plotted Line for Coordinates


X1,Y1=(100,100) and X2,Y2=(150,150) using DDA Line drawing Algorithm

CG / CSL – 402 / SEM - IV Page 05


Computer Engineering / SSJCET, Asangaon
EXPERIMENT No. 02

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.

m = (y2 - y1)/(x2 - x1)

We multiply both sides by (x2 - x1)

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.

Also, it is generally preferred to compare with 0 than 1.

CG / CSL – 402 / SEM - IV Page 06


Computer Engineering / SSJCET, Asangaon
ALGORITHM

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.

CG / CSL – 402 / SEM - IV Page 07


Computer Engineering / SSJCET, Asangaon
FLOW CHART

CG / CSL – 402 / SEM - IV Page 08


Computer Engineering / SSJCET, Asangaon

CG / CSL – 402 / SEM - IV Page 09


Computer Engineering / SSJCET, Asangaon
PROGRAM :-
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;

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");

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);

return 0;
}

CG / CSL – 402 / SEM - IV Page 010


Computer Engineering / SSJCET, Asangaon

OUTPUT:-

CONCLUSION: We have studied and successfully plotted Line for Coordinates


X1,Y1=(100,100) and X2,Y2=(200,200) using Bresenhams Line drawing Algorithm

CG / CSL – 402 / SEM - IV Page 011


Computer Engineering / SSJCET, Asangaon
EXPERIMENT NO. 03

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 .

THICK LINE DRAWING

CG / CSL – 402 / SEM - IV Page 012


Computer Engineering / SSJCET, Asangaon

ALGORITHM

1. Inter the coordinates for the line .


p1 = (x1,y1) , p2 = (x2,y2)
2. enter the thickness.
3. draw line (x1,y1,x2,y2).
4. if ( (y2-y1) / (x2-x1) < 1)
{
wy = (thickness-1) * sqrt (pow ( (x2-x1),2)+ pow ((y2-y1),2))/(2*fabs(x2-x1));
while(i<wy)
{
i = i+1;
line(x1,y1-i,x2,y2-i);
line( x1,y1+i,x2,y2+i);
}
}
else
{

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;
}
}

6. determine the thickness of line.


7. stop.

CG / CSL – 402 / SEM - IV Page 013


Computer Engineering / SSJCET, Asangaon
FLOW CHART

N Y

CG / CSL – 402 / SEM - IV Page 014


Computer Engineering / SSJCET, Asangaon
PROGRAM :

#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();
}

CG / CSL – 402 / SEM - IV Page 015


Computer Engineering / SSJCET, Asangaon

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

CG / CSL – 402 / SEM - IV Page 016


Computer Engineering / SSJCET, Asangaon
Experiment No: 04

AIM : Program to draw a Circle using Midpoint Circle 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:-

Given a circle centered at (0,0) and radius r and a point p(x,y)


F(p) = x2 + y2 – r2

if F(p)<0, the point is inside the circle

CG / CSL – 402 / SEM - IV Page 017


Computer Engineering / SSJCET, Asangaon
F(p)=0, the point is on the perimeter

F(p)>0, the point is outside the circle

Pk = (Xk — 0.5)2 + (yk + 1)2 – r2

Now,
xk+1 = xk or xk-1 , yk+1= yk +1

∴ Pk+1 = (xk+1 – 0.5)2 + (yk+1 +1)2 – r2


= (xk+1 – 0.5)2 + [(yk +1) + 1]2 – r2
= (xk+1 – 0.5)2 + (yk +1)2 + 2(yk + 1) + 1 – r2
= (xk+1 – 0.5)2 + [ – (xk – 0.5)2 +(xk – 0.5)2 ] + (yk + 1)2 – r2 + (yk + 1) + 1

= Pk + (xk+1 – 0.5)2 – (xk – 0.5)2 + 2(yk + 1) + 1


= Pk + (x2k+1 – x2k)2 + (xk+1 – xk)2 + 2(yk + 1) + 1
= Pk + 2(yk +1) + 1, when Pk <=0 i.e the midpoint is inside the circle
(xk+1 = xk)
Pk + 2(yk +1) – 2(xk – 1) + 1, when Pk>0 I.e the mid point is outside the circle(xk+1 = xk-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

CG / CSL – 402 / SEM - IV Page 018


Computer Engineering / SSJCET, Asangaon
ALGORITHM :

1. Read the radius r of the circle.


2. Initialize starting position as
x=0
y=r
3. Calculate initial value of decision parameter as
P=1.25-r
4. do
{
plot(x , y)
if (p<0)
{
x=x+1
y=y
p=p+2x+1
}
else
{
x=x+1
y=y-1
p=p+2(x-y)+1
}
}while (x<=y)
5. Determine symmetry point
6. Stop

CG / CSL – 402 / SEM - IV Page 019


Computer Engineering / SSJCET, Asangaon
FLOW CHART

CG / CSL – 402 / SEM - IV Page 020


Computer Engineering / SSJCET, Asangaon
PROGRAM :

#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);

CG / CSL – 402 / SEM - IV Page 021


Computer Engineering / SSJCET, Asangaon
getch();
closegraph();
}

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

CG / CSL – 402 / SEM - IV Page 022


Computer Engineering / SSJCET, Asangaon
Experiment no :05

AIM : Program to draw an ellipse using Midpoint Ellipse 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)

(-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

CG / CSL – 402 / SEM - IV Page 023


Computer Engineering / SSJCET, Asangaon
Consider the general equation of an ellipse,
ry2x2 + rx2y2 = rx2. ry2
where rx is the horizontal radius and ry is the vertical radius, we can define an function f(x,y) by
which the error due to a prediction coordinate (x,y) can be obtained. The appropriate pixels can be
selected according to the error so that the required ellipse is formed. The error can be confined

x is always incremented in each step, i.e. xk+1= xk + 1.yk+1= yk


if E is selected, or yk+1= yk – 1
if SE is selected.In order to make decision between S and SE, a prediction (xk +1, yk –½) is set at
middle between the two candidate pixels.

ALGORITHM

1. read radii rx and ry.


2. Initialize starting point as
x=0
y=ry
3. calculate the initial value of decision parameter in region 1 as
d1= ry2-rx2* ry+ (1/4)*rx2

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]

CG / CSL – 402 / SEM - IV Page 024


Computer Engineering / SSJCET, Asangaon
}
else
{
x=x+1
y=y-1
dx = dx+2*ry2
dy = dy-2rx2
d1=d1+dx-dy+ry2
[d1=d1+2*ry2*x+2ry2-(2rx2*y-2*rx2 )+ry2]
} while(dx<dy)

6. calculate the initial value of decision parameter in region 2 as


d2=ry2(x+1/2)2 + rx2(y-1)2-rx2*ry2
7. do
{
plot (x,y)
if(d2>0)
{
x=x
y=y-1
dy=dy-2*rx2
d2=d2-dy+rx2
[d2=d2-(2*rx2y-2rx2)+rx2]
}
else
{
x=x+1
y=y-1
dy=dy-2*rx2
dx=dx+2*ry2
d2=d2+dx-dy+rx2
d2=[d2+2*ry2*x+2*ry2-(2*rx2*y-2*rx2)+ry2]
}
}while(y>0)
8. determine symmetrical points in other three quadrants
9. stop.

CG / CSL – 402 / SEM - IV Page 025


Computer Engineering / SSJCET, Asangaon
FLOW CHART

CG / CSL – 402 / SEM - IV Page 026


Computer Engineering / SSJCET, Asangaon

CG / CSL – 402 / SEM - IV Page 027


Computer Engineering / SSJCET, Asangaon
PROGRAM :

#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);

CG / CSL – 402 / SEM - IV Page 028


Computer Engineering / SSJCET, Asangaon
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
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);
delay(10);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2-dy+rxsq;
}
else
{
x=x+1;
y=y-1;
dy=dy-tworxsq;
dx=dx+tworysq;
d2=d2+dx-dy+rxsq;
}
}
while(y>0);
getch();
closegraph();
}

OUTPUT

CG / CSL – 402 / SEM - IV Page 029


Computer Engineering / SSJCET, Asangaon
CONCLUSION: We have studied and successfully plotted Ellipse using Midpoint Ellipse
drawing Algorithm for Centre Coordinates X,Y=(250,250) and X- radius = 200 and y-radius = 80.

CG / CSL – 402 / SEM - IV Page 030


Computer Engineering / SSJCET, Asangaon
Experiment No: 06

AIM : Program to draw a curve using Bezier Curve drawing algorithm.

THEORY:

CG / CSL – 402 / SEM - IV Page 031


Computer Engineering / SSJCET, Asangaon
ALGORITHM

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.

CG / CSL – 402 / SEM - IV Page 032


Computer Engineering / SSJCET, Asangaon
FLOW CHART

PROGRAM :

CG / CSL – 402 / SEM - IV Page 033


Computer Engineering / SSJCET, Asangaon

#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);

CG / CSL – 402 / SEM - IV Page 034


Computer Engineering / SSJCET, Asangaon
}
initgraph(&gd,&gm,"..//BGI");
}
void main()
{
int i;
float temp1,temp2;
igraph();
for(i=0;i<4;i++)
{
printf("enter x,y cordinates of point %d:",i+1);
scanf("%f%f",&temp1,&temp2);
xxx[i][0]=temp1;
xxx[i][1]=temp2;
}
line(xxx[0][0],xxx[0][1],xxx[1][0],xxx[1][1]);
line(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1]);
line(xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1]);
bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8);
getch();
closegraph();
}

OUTPUT:

CONCLUSION: We have studied and successfully implemented Bezier Curve drawing


Algorithm

CG / CSL – 402 / SEM - IV Page 035


Computer Engineering / SSJCET, Asangaon
Experiment No. 07

AIM:-Write a program of polygon filling using Boundary fill algorithm.

LANGUAGE USED:-Turbo C++.

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.

CG / CSL – 402 / SEM - IV Page 036


Computer Engineering / SSJCET, Asangaon
ALGORITHM:-

1. current = getpixel(x,y).

2. if((current==boundary) AND (current-1==fill))

then putpixel(x,y,fill)

//fill the boundary by checking Eight connected region as

boundary_fill(x,y+1,fill,bound);

boundary_fill(x, y-1,fill, bound);

boundary_fill(x+1,y, fill, bound);

boundary_fill(x-1,y ,fill, bound);

boundary_fill(x+1, y+1,fill,bound);

boundary_fill(x-1, y+1, fill, bound);

boundary_fill(x-1, y-1, fill, bound);

boundary_fill(x+1, y-1, fill, bound);

3. END

CG / CSL – 402 / SEM - IV Page 037


Computer Engineering / SSJCET, Asangaon
PROGRAM :

#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);
}
}

CONCLUSION:- Hence we have successfully implemented Boundary-fill Polygon filling.

CG / CSL – 402 / SEM - IV Page 038


Computer Engineering / SSJCET, Asangaon
Experiment No. 09

AIM:- Write a program to perform 2D Object Transformation

LANGUAGE USED:-Turbo C++.

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 −

● Translate the coordinates,


● Rotate the translated coordinates, and then
● Scale the rotated coordinates to complete the composite transformation.
To shorten this process, we have to use 3×3 transformation matrix instead of 2×2 transformation
matrix. To convert a 2×2 matrix to 3×3 matrix, we have to add an extra dummy coordinate W.
In this way, we can represent the point by 3 numbers instead of 2 numbers, which is
called Homogenous Coordinate system. In this system, we can represent all the transformation
equations in matrix multiplication. Any Cartesian point P(X, Y) can be converted to homogenous
coordinates by P’ (Xh, Yh, h).
Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).

From the above figure, you can write that −


CG / CSL – 402 / SEM - IV Page 039
Computer Engineering / SSJCET, Asangaon
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors.
P=[X][Y]P=[X][Y] p' = [X′][Y′][X′][Y′]T = [tx][ty][tx][ty]
We can write it as −
P’ = P + T
Rotation
In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following
figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate
with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will
get a new point P’ (X’, Y’).

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

CG / CSL – 402 / SEM - IV Page 040


Computer Engineering / SSJCET, Asangaon
Where R is the rotation matrix
R=[cosθ−sinθsinθcosθ]R=[cosθsinθ−sinθcosθ]
The rotation angle can be positive and negative.
For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below −
R=[cos(−θ)−sin(−θ)sin(−θ)cos(−θ)]R=[cos(−θ)sin(−θ)−sin(−θ)cos(−θ)]
=[cosθsinθ−sinθcosθ](∵cos(−θ)=cosθandsin(−θ)=−sinθ)=[cosθ−sinθsinθcosθ](∵cos(−θ)=cosθands
in(−θ)=−sinθ)

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.

CG / CSL – 402 / SEM - IV Page 041


Computer Engineering / SSJCET, Asangaon
ALGORITHM:-

I] Algorithm for Translation:-

1. Accept the starting co-ordinates A=(x1,y1) and ending co-ordinates

B=(x2,y2).

2. Accept translation point (tx,ty).

3. Calculate new position of starting co-ordinates as

A’=T*A. (:.T: translation matrix).

Calculate all values of Translation matrix & return to A’.

4. for ending point

B’= T*B

Calculate all values of Translation Matrix and return to B’.

5. Return Translated object(line).

6. Stop.

II] Algorithm for Rotation:-

1. Accept the starting co-ordinates A=(x1,y1) and ending co-ordinates

B=(x2,y2).

2. Accept rotation angle Θ (rx,ry).

3. Calculate new position of starting co-ordinates as

A’=R*A. (:.R: rotation matrix).

Calculate all values of Rotatin matrix & return to A’.

4. for ending point

B’= R*B

Calculate all values of Rotation Matrix and return to B’.

5. Return Rotated object(line).

6. Stop.

CG / CSL – 402 / SEM - IV Page 042


Computer Engineering / SSJCET, Asangaon
III] Algorithm for Scaling:-

1. Accept the starting co-ordinates A=(x1,y1 and ending co-ordinates

B=(x2,y2).

2. Accept scaling point (sx,sy).

3. Calculate new position of starting co-ordinates as

A’=S*A. (:.S: Scaling matrix).

Calculate all values of Scaling matrix & return to A’.

4. for ending point

B’= S*B

Calculate all values of Scaling Matrix and return to B’.

5. Return Scaling object(line).

6. Stop.

IV] Algorithm for Shearing:-

1. Accept the starting co-ordinates A=(x1,y1) and ending co-ordinates

B=(x2,y2).

2. Accept at which direction object (line) to be sheared as x-shear Shx and y-

shear as Shy.

3.

i) for x-shear :-

A’=x+Shx.y ; (:. Shx:- matrix for x-shear)

B’=B;

ii) for y-shear:-

B’= Shyx+y; (:. Shy:- matrix for y-shear)

A’=A;
4 Return Sheared object (line).

4. Stop.

CG / CSL – 402 / SEM - IV Page 043


Computer Engineering / SSJCET, Asangaon

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;

CG / CSL – 402 / SEM - IV Page 044


Computer Engineering / SSJCET, Asangaon
b[2][1] = ty;
b[2][2] = 1;

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;

case 3: printf("\n Enter x scaling : ");


scanf("%d", &sx);
printf("\n Enter y scaling : ");
scanf("%d", &sy);

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;

case 4: printf("\n Enter x coordinate of arbitary point : ");


scanf("%d", &x);
printf("\n Enter y coordinate of arbitrary point : ");
scanf("%d", &y);
printf("\n Enter Rotation angle : ");
scanf("%d", &angle);

tx = x;
ty = y;

b[0][0] = cos(angle*3.14/180);
b[0][1] = -sin(angle*3.142/180);

CG / CSL – 402 / SEM - IV Page 045


Computer Engineering / SSJCET, Asangaon
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] = -tx* cos(angle*3.142/180) + ty* sin(angle*3.142/180) + tx;


b[2][1] = -tx* sin(angle*3.142/180) - ty* cos(angle*3.142/180) + ty;
b[2][2] = 1;

}
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);
}

CG / CSL – 402 / SEM - IV Page 046


Computer Engineering / SSJCET, Asangaon
OUTPUT

CG / CSL – 402 / SEM - IV Page 047


Computer Engineering / SSJCET, Asangaon

CONCLUSION: Program to perform 2D Object Transformation is successfully implemented.

CG / CSL – 402 / SEM - IV Page 048


Computer Engineering / SSJCET, Asangaon
Experiment No. 10

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.

CG / CSL – 402 / SEM - IV Page 049


Computer Engineering / SSJCET, Asangaon
The logical OR of the endpoint codes determines if the line is completely inside the window. If
the logical OR is zero, the line can be trivally accepted. For example, if the endpoint codes are
0000 and 0000, the logical OR is 0000 - the line can be trivally accepted. If the endpoint codes are
0000 and 0110, the logical OR is 0110 and the line cannot be trivally accepted.

There are three possible cases for any given line.

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

CG / CSL – 402 / SEM - IV Page 050


Computer Engineering / SSJCET, Asangaon
ALGORITHM

1. Read two end points of the line say P1 (x2-y2).


2. Read two corners (left-top and right-bottom) of the window, say
(Wx1, Wy1 and Wx2, Wy2).
3. Assign the region codes for two endpoints P1 and P2 using following steps :
Initialize code with bits 0000
Set Bit 1 - if (x<Wx1)
Set Bit 2 - if (x>Wx2)
Set Bit 3 - if (y<Wy2)
Set Bit 4 - if (y>Wy1)
4. Check for visibility of line P1 P2
a. If region codes for both endpoints P1 and P2 are zero then the line is
completely visible. Hence draw the line and go to step 9.
b. If region codes for endpoints are not zero and the logical ending of them is
also nonzero then the line is completely invisible, so reject the line and go to
step 9
c. If region codes for two end point do not satisfy the conditions in 4a) and 4b)
the line is partially visible.

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.

CG / CSL – 402 / SEM - IV Page 051


Computer Engineering / SSJCET, Asangaon

FLOW CHART

CG / CSL – 402 / SEM - IV Page 052


Computer Engineering / SSJCET, Asangaon

CG / CSL – 402 / SEM - IV Page 053


Computer Engineering / SSJCET, Asangaon

CG / CSL – 402 / SEM - IV Page 054


Computer Engineering / SSJCET, Asangaon

PROGRAM :

#include<stdio.h> case 0: cleardevice(); /* Line completely


#include<conio.h> visible */
#include<stdlib.h> drawwindow();
#include<dos.h> drawline(p1,p2,15);
#include<math.h> break;
#include<graphics.h>
case 1: cleardevice(); /* Line Completely
/* Defining structure for end point of line */ invisible */
typedef struct coordinate drawwindow();
{ break;
int x,y; case 2: cleardevice(); /* line party visible
char code[4]; */
} p1=resetendpt(p1,p2);
PT; p2=resetendpt(p2,p1);
drawwindow();
void drawwindow(); drawline(p1,p2,15);
void drawline(PT p1,PT p2,int cl); break;
PT setcode(PT p); }
int visibility(PT p1,PT p2); getch();
PT resetendpt(PT p1,PT p2); closegraph();
int xmax,xmin,ymax,ymin; return(0);
}
{ /*Function to draw window */
int gd=DETECT, gm,v; void drawwindow()
PT p1,p2,ptemp; {
initgraph(&gd,&gm,"..\\bgi"); setcolor(RED);
cleardevice(); rectangle(xmin,ymin,xmax,ymax);
printf("Enter window coordinates }
(xmin,ymin) and (xmax,ymax)"); /* Functions to draw line between two
points */
scanf("%d%d%d%d,",&xmin,&ymin,&xma void drawline(PT p1, PT p2, int cl)
x,&ymax); {
printf("\n\n\t\tEnter END-POINT 1(x,y): "); setcolor(cl);
scanf("%d%d",&p1.x,&p1.y); line(p1.x,p1.y,p2.x,p2.y);
printf("\n\n\t\t Enter END-POINT 2 (x,y): }
"); /*Function to set code of the coordinates
scanf("%d%d",&p2.x,&p2.y); */
cleardevice(); PT setcode(PT p)
drawwindow(); {
getch(); PT ptemp;
drawline(p1,p2,15); if(p.y<ymin)
getch(); ptemp.code[0]='1';/*TOP */
p1=setcode(p1); else
p2=setcode(p2); ptemp.code[0]='0';
v = visibility(p1,p2); if(p.y>ymax)
switch(v) ptemp.code[1]='1'; /*BOTTOM */
{ else

CG / CSL – 402 / SEM - IV Page 055


Computer Engineering / SSJCET, Asangaon
ptemp.code[1]='0'; {
if(p.x>xmax) PT temp;
ptemp.code[2]='1';/*RIGHT*/ int i,x,y,z;
else float m,k;
ptemp.code[2]='0'; if(p1.code[3]=='1')/* Cutting Left Edge */
if(p.x<xmin)/*LEFT*/ x=xmin;
ptemp.code[3]='1'; if(p1.code[2]=='1')/* Cutting Right Edge
else */
ptemp.code[3]='0'; x=xmax;
ptemp.x=p.x; if((p1.code[3]=='1')||(p1.code[2]=='1'))
ptemp.y=p.y; {
return(ptemp); m=(float)(p2.y-p1.y)/(p2.x-p1.x);
} k=(p1.y+(m*(x-p1.x)));
/*Function to determine visibility of temp.y=k;
line */ temp.x=x;
int visibility(PT p1,PT p2) for(i=0;i<4;i++)
{ temp.code[i]=p1.code[i];
int i, flag=0; if(temp.y<=ymax&&temp.y>=ymin)
for(i=0;i<4;i++) return(temp);
{ }
if((p1.code[i]!='0')||(p2.code[i]=='0')) if(p1.code[0]=='1') /* Cutting Top Edge
flag=1; */
} y=ymin;
if(flag==0) if(p1.code[1]=='1')/* Cutting Bottom
return(0); Edge */
for(i=0;i<4;i++) y=ymax;
{ if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]==' m=(float)(p2.y-p1.y)/(p2.x-p1.x);
1')) k=(float)p1.x+(float)(y-p1.y)/m;
flag=0; temp.x=k;
} temp.y=y;
if(flag==0) for(i=0;i<4;i++)
return(1); temp.code[i]=p1.code[i];
return(2); return(temp);
} }
/*Function to find new end points */ else
PT resetendpt(PT p1, PT p2) return(p1);
}

CG / CSL – 402 / SEM - IV Page 056


Computer Engineering / SSJCET, Asangaon
OUTPUT

CONCLUSION: We have studied and successfully implmented Cohen Sutherland Line


Clipping Algorithm

CG / CSL – 402 / SEM - IV Page 057

You might also like