You are on page 1of 39

FORM NO. F/ LAB/003


Rev.00 Date 01.01.2014

Dr. M.G.R.
R
socaonal and

EDUCATIONAL AND RESEARCH INSTITUTE A


NAAC

(Deemed to- be University) *

Maduravoyal, Chennai 600 095. Tamilnadu. India.


(An ISO 9001 : 2015 Certifed Institution)
University with Special Autonomy Status

RECORD NOTEBOOK

COMPUTER GRAPHICS LAB – (BCS18ET2)

2022 -2023 (ODD SEMESTER)

DEPARTMENT

Of
COMPUTER SCIENCE AND ENGINEERING

NAME MANISH SINGH SURYAVANSHI

REGISTER NO 201061101118
:
COURSE BTech
YEAR/ SEM /SEC : III/V/B
FORM NO. – F/ LAB/003
Rev.00 Date 01.01.2014

Dr. M.G.R.
shAOn and Re EDITED
WITH
G

EDUCATIONAL AND RESEARCH INSTITUTE A


NAAC

(Deemed to- be University)


Maduravoyal, Chennai 600 095. Tamilnadu. India.
(An ISO 9001 : 2015 Certified Institution)
University with Special Autonomy Status

BONAFIDE CERTIFICATE

REGISTER NO : 201061101118

NAME OF LAB :
COMPUTER GRAPHICS – (BCS18ET2)

DEPARTMENT : COMPUTER SCIENCE AND ENGINEERING

Certified that, this Record note book is a bonafide record of work done by MANISH
SINGH SURYAVANSHI of II Year B.Tech / CSE, Sec- B
in the COMPUTER
GRAPHICS LAB during the year 2022-2023.

Signature of Lab-in-Charge Signature of Head of Dept

Submitted for the Practical Examination held on

Internal Examiner External Examiner


INDEX

PAGE STAFF
S.NO. DATE NAME OF THE EXPERIMENT
No. SIGNATURE
1
03-08-2022 IMPLEMENTATION OF THE DDA 01 –03

ALGORITHM

2 10-08-2022 SUTHERLAND HODGEMAN POLYGON 04-10


CLIPPING ALGORITHM

3 17-08-2022 IMPLEMENTATION OF MID-POINT 11- 14


ELLIPSE ALGORITHM

4 24-08-2022 IMPLEMENTATION OF 2D 15–20


TRANSFORMATION

IMPLEMENTATION OF BRESENHAM's
07-09-2022 21- 23

LINE DRAWING ALGORITHM

6
14-09-2022 IMPLEMENTATION OF 2D WINDOWING
24- 26
IMPLEMENTATION OF COHEN
21-09-2022 27-33
SUDHERLAND AND LINE CLIPPING
ALGORITHM

21-09-2022 IMPLEMENTATION OF BRESENHAM's 34-36


CIRCLE ALGORITHM
Ex. No. 1 Date: 03-08-2022

IMPLEMENTATION OF THE DDA ALGORITHM

AIM

To write a C program to implement DDA Algorithm to draw a line.

ALGORITHM

Step1. Start the program


Step2. Include the "graphics.h" library file in the text program.
Step3. Declare the variables x, x2, y, y2, dx, dy, k, m
Step4. Take the input for initial and points i.e. x, x2, y, y2
Step5. Calculate dx and dy
i. dx=x2-x

ii. dy-y2-2
Step6. Calculate the slope i.e. m
i. m-dyldx
Step7. Calculate the number of steps to repeat the program to print all the points
if(abs(dx)>abs(dy)) then
k=abs(dx)
else abs(dy)
Step8. Use for loop to repeat the step till k
if m<0 then
X=X+1

y=ytm
else if m>0 then
X=X+(1/m)

yy+1
else x=x+1, y=y+l
Step9. To print all the points use putpixels(...)
Step10. Delay(...) is used to make slow printing
Step11. Stop the program

PROGRAM

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

201061101118 MANISH SINGH SURYAVANSHI


#include<stdlib.h>
#include<math.h>
int main)

int gd-DETECT, gm, i, j;


float x, x2, y, y2, dx, dy, k, m;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118");
printf("nEnter Your Initail c0-ordinates: ");
scanf("%f%f", &x, &y);
printf("Enter Your End co-ordinates: ");
scanf("%f%f", &x2, &y2);
dx=x2-x;
dy-y2-y;
m-dy/dx;
printf("nValue of n: %.0f", m);
if(abs(dx)>abs(dy))
k=abs(dx);
else
k=abs(dy);
printf("n Value of K: %.0fn", k);
for(i-0; i<k; itt)

if(m<0)

XFX+1;

y=ytm;

else if(m>0)

x=x+(1/m);
y-y+l;

else

X=X+1;
2
201061101118 MANISH SINGH SURYAVANSHI
y=y+l;

putpixel(x, y, 5);
delay(50);

getch();
closegraph():
return 0;
}

OUTPUT
This program is done by: MANISH SINGH SURYAUANSHI 201061101118
Enter Your Initail co-ordinates: 200 100
Enter Your End co-ordinates: 300 300

Va lue of m: 2
Va
lue of K: 200

RESULT

Thus, the program to implement DDA Algorithm to draw a line was executed and verified
successfully.

3
201061101118 MANISH SINGH SURYAVANSHI
Ex. No. 2 Date: 10-08-2022

SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM

AIM

To write a Ctt program to implement clip a polygon Algorithm


ALGORITHM

Step1. Start the program


Step2. Read the coordinates of all vertices of the Polygon.
Step3. Read the coordinates of the dipping window
Step4. Consider the left edge of the window
Step5. Compare the vertices of each edge of the polygon, individually with the clipping plane.
Step6. Save the resulting intersections and vertices in the new list of vertices according to four
possible relationships between the edge and the clipping boundary.
Step7. Repeat steps 4 and 5 for remaining edges or the clipping window. Each time the resultant list
of vertices is successively passed to process the next edge of the clipping window.
Step8. Stop the program

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) (int)\(a+0.5))
int k:
float xmin,ymin,xmax,ymax,ar[20],m;
void clipl(float x1,float y1,float x2,float y2)

if(x2-x1)
m-(y2-yl)/(x2-x1 );

else
m=100000;
if(x1 >= xmin && x2 >= Xmin)

arr[k]-x2;
arr[k+l]-y2;

4
201061101118 MANISH SINGH SURYAVANSHI
k+=2;

if(xl <Xmin && x2 >= xmin)

arr[k]=xmin;
arr[k+1]=yl+m*(xmin-x ); 1

arr[k+2]-x2;
arr[k+3]-y2;
kt=4;

if(x1>= Xmin && x2 < xmin)

arr[k]=xmin;
arr[k+1]-yl+m*(xmin-x l);
k+=2;

void clipt(float x1,float y1,float x2,float y2)

if(y2-y1)
m-(x2-xl )/(y2-yl);
else
m=100000;
if(yl <= ymax && y2 <= ymax)

arr[k]-x2;
arr[k+l]-y2:
k+=2;

if(yl > ymax && y2 <= ymax)

arr[k]=x1+m(ymax-yl);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]-y2;
kt=4;
5
201061101118 MANISH SINGH SURYAVANSHI
if(yl <=ymax && y2 > ymax)

arr[k]-xl+m*(ymax-yl);
arr[k+1]=ymax;
kt=2;

void clipr( float x1,float y1,float x2,float y2)

if(x2-x1)
m-(y2-yl)/(×2-x 1
);

else
m=100000:
if(xl <= Xmax && x2 <E xmax)

arr[k]-x2;
arr[k+l]-y2;
kt=2;

if(x1 > XImax && x2 <= xmax)

arr[k]-xmax;
arr[k+1]-yl +m*(xmax-xl );
arr[k+2]-x2;
arr[k+3]-y2;
kt=4;

if(xl <= Xmax && x2> XImax)

arr[k]-xmax;
arr[k+1]=y1 +m*(xmax-xl );
kt=2;

void clipb(float x1,float y1,float x2,float y2)


6
201061101118 MANISH SINGH SURYAVANSHI
if(y2-yl)
m-(x2-x1/(y2-y1);
else
m=100000;
if(yl>ymin && y2 = ymin)
{

arr[k]-x2;
arr[k+1]-y2;
k+=2;

if(yl < ymin && y2 = ymin)


arr[k]=x1+m*(ymin-yl);
arr[k+1]=ymin;
arr[k+2]-x2;
arr[k+3]-y2;
kt=4;

if(yl>ymin && y2 < ymin)

arr[k]=x1+m*(ymin-y l);
arr[k+1]=ymin;
k+=2;

void main()

int gdriver-DETECT,gmode,n,poly[20];
float xi,yi,xfyf,polyy[20]:
clrscr);
cout<<"This program is done by: MANISH SINGH SURYAVANSHI\t 201061101118\n";
cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
7
201061101118 MANISH SINGH SURYAVANSHI
cout<<"\nlnPolygon to be clipped :\nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates :":;

for(int i=0;i <2*n;itt)


cin>>polyy[i);
polyy[il -polyy[0]:

polyy[i+1]-polyy[1]:
for(i-0;i< 2*n+2;i++)
poly[i]=round(polyy[i):
initgraph(&gdriver,&gmode,"C:/TURBOC3/BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"This program is done by: MANISH SINGH SURYAVANSHI\t 201061101118\n";
cout<<"\tltUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch():
cleardevice();
k=0;
for(i-0;i < 2*n;it=2)
clipl(polyy[ilpolyy[i+1],polyy[i+2],polyy[i+3):
n=k/2;
for(i=0;i <k;it+)
polyy[i-ar[i]:
polyy[i]-polyy[0]:
polyy[it1polyy[1);
k-0;
for(i-0;i < 2*n;it=2)
clipt(polyyilpolyy[i+1 ],polyy[i+2],polyy[it3]):
n=k/2;
for(i-0;i<k;itt)
polyy[il-arrfil:
polyyli]-polyy[0]:
polyy[it1]polyy[1]:;
k=0;
for(i-0;i < 2*n;it-2)

201061101118 MANISH SINGH SURYAVANSHI


clipr(polyylil.polyy[it1 ],polyy[i+2],polyy[i+3]):
n=k/2;
for(i-0;i < k;itt)
polyy[i]-arr[i];
polyy[i]-polyy[0]:
polyy[i+1 ]-polyy[ 1];

k=0;
for(i-0;i < 2*n;it-2)
clipb(polyylil.polyy[i+ 1],polyy[i+2],polyy[it3]):
for(i=0;i<< k;itt)
poly[il-round(arr[i):
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"This program is done by: MANISH SINGH SURYAVANSHI\t 201061 101118n";
cout<<MtCLIPPED POLYGON":
getch();
closegraph);

OUTPUT
This program
done by : MAN ISH SINGH SURYAUANSHI
is
Coordinates of rectangu lar clip window
Xmin, ymin :200 200
XMax, ymax :40O 400

Polygon to be clipped :

Number of sides :3
Enter the coordinates :15O 300
300 150
300 300

9
201061101118 MANISH SINGH SURYAVANSHI
This program is done by: MAN ISH SINGH SURYAUANSHI 201061101118
UNCL IPPED POLYGON

This program is done by: MANISH SINGH SURYAUANS


HI 2010611011 18
CLIPPED POLYGON

RESULT

Thus, the program to implement clip a polygon Algorithm executed and verified successfully.

10
201061101118 MANISH SINGH SURYAVANSHI
Ex. No. 3 Date: 17-08-2022

IMPLEMENTATION OF MID-POINT ELLIPSE ALGORITHM

AIM

To write aC program to implement Mid-Point Ellipse Algorithm to draw a line.

ALGORITHM

Step1. Start the program


Step2. Include the "graphics.h'" library file in the text program.
Step3. Declare the variables xc, yc, x, y, Rx, Ry, rx_sq. ry_sq. p. p2, dx, dy
Step4. Take the input for ellipse coordinate and major-minnor axes i.e. XC, yc, Rx, Ry

Step5. Initialize x-0 and y-Ry


Step6. Calculate the decision parameter, p, dx, dy
P-(b*b)-a*a*b)+(a*a)/4)
dx=2*x**b*b

dy-2*y*aa
Step7. Update the value of dx, dy in each iteration
Step8. Use for loop to repeat the step till dx<=dy then plot (x, y)
ifp<0 then
X=X+1

Pptdx+(Ry*Ry)
dx=dx+(2*Ry*Ry)
else
y=y-1
X=X+1

p=ptdx-dy+(Ry*Ry)
dx=dx+(2*Ry*Ry)
dy-dy-(2*Rx*Rx)
Step9. When dx>-dy then ploat Region2
Step10. Calculate the decision parameter for Region2
P2-(Ry*Ry)*(x+0.5)*(x+0.5)+(Rx*Rx)*(y-1)*(y-1)-(Ry*Ry*Rx*Rx)
Step11. Repeat until y>0
if p>0 then
yry-l
p2-p2-dyt(Rx*Rx)
11
201061101118 MANISH SINGH SURYAVANSHI
dy=dy-(2*Rx*Rx)
else
y-y-1
X=X+1

p2-p2+dx-dy+(Rx*Rx)
dx=dx+(2*Ry*Ry)
dy=dy-(2*Rx*Rx)
Step12. To print all the points use putpixels(...)
Step13. Delay(...) is used to make slow printing
Step14. Stop the program

PROGRAM

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main(0

long xc, yc, x, y, Rx, Ry, rx_sq, ry_sq, P, p2, dx, dy;
int gd-DETECT, gm;
initgraph(&gd, &gm, "C:\TURBOC3\BGI"):
clrscr();
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118\n"):
printf(" nEnter Ellipse Co-ordinate points: "):
scanf("%ld%ld", &XC, &yc);
printf("Enter Ellipse Major axis: ");
scanf("%ld", &Rx);
printf("Enter Ellipse Minor axis: ");
scanf("%ld", &Ry);
x=0;
y-Ry;
rx_sq=Rx*Rx;
ry_sq=Ry*Ry;
pry_sq-(rx_sq*Ry)+(rx_sq*0.25);
dx=2*x*ry_sq;
dy-2*y*rx_sq:
while(dx<dy)
12
201061101118 MANISH SINGH SURYAVANSHI
putpixel(xc+x, ycty, RED);
putpixel(xc-x, yc-y,RED);
putpixel(xctx, yc-y, BLUE);
putpixel(xc-x, ycty, BLUE);
if(p<0)

X=X+1;

pptdx+ry _sq;

dx=dx+(2*ry_s):

else

x=x+1;
yy-1;
pptdx+-dy+ry sq;

dy-dy-(2*rx sq);
dx=dx+(2*ry_sq);

delay(50);

if(dx>=dy)

p2-(ry_sq*(«+0.5)*(x+0.5)))+(rx_ sq*((y-1)*(y-1)-(rx_sq*ry_sq);
while(y>0)

putpixel(xc+x, ycty, GREEN);


putpixel(xc-x, yc-y, YELLOW);
putpixel(xctx, yc-y, GREEN);
putpixel(xc-x, ycty, YELLOW);
if(p2>-0)

y=y-l;
p2-p2-dytrx_sq;
dy-dy-(2*rx_sq);
13
201061101118 MANISH SINGH SURYAVANSHI
else

X=X+1;

y=y-l;
p2-p2+dx-dy+rx _sq:
dy-dy-(2*rx_sq);
dx=dx+(2*ry_sq):

delay(50):

getch();
closegraph);
return 0:

OUTPUT
This program is done by : MANISH SINGH SURYAUANSHI 201061101118
Enter Ellipse Co-ordinate points: 200 200
EnterEllipse Ma jor
axis: 150
Enter Ell ipse Minoraxis: 80

RESULT

Thus, the program to implement Mid-Point Ellipse Algorithm to draw a line was executed and verified
successfully.
14
201061101118 MANISH SINGH SURYAVANSHI
Ex. No. 4 Date: 24-08-2022

IMPLEMENTATION OF 2D TRANSFORMATION

AIM

To write a C+tprogram to draw and manipulate the polygon in two dimensions.

ALGORITHM

Step1. Start Start the program


Step2. Declare the variables and detect the graph variables
Step3. Draw the polygon and transform the image from the original image using the array
Step4. Using the for loop get the polygon variables
Step5. Whether the function of the menu() is less than and construct the polygon in different manner
by using SWITCH statement
Step6. In case 1,transform the polygon by given input length from the original image

Step7. In case 2,rotate the polygon using the given angle and looping statement from the original

image
Step8. In case 3,to scale the polygon from the original image by given input

Step9. Menu() function give the menu options


Step10. Stop the program

PROGRAM

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
int main)
{

clrscr);
int gd-DETECT,gm,s;
initgraph(&gd,&gm, (char*)"C:\TURBOC3\BGI");
cout<<"This program is done by: MANISH SINGH SURYAVANSHI\t 201061101118\n";
cout<<"nl.Translation n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)

15
201061101118 MANISH SINGH SURYAVANSHI
case 1:
{ int xl=200,yl=150,x2=300,y2-250;
int tx-50,ty-50;
cout<<"Rectangle before translation"<<endl;
setcolor(3);
rectangle(xl,yl,x2,y2);
setcolor(4);
cout<<"Rectangle after translation"<<endl;
rectangle(xl+tx,yl+ty,x2+tx,y2+tty):
getch();
break;

case 2:
{ long xl=200,yl =200,x2=300.y2=300;
double a;
cout<<"Rectangle with rotation"'<<endl;
setcolor(3);
rectangle(x1,yl,x2,y2);
cout<<"Angle of rotation:";
cin>>a;
a-(a*3.14)/180;
long xr-x l+((x2-x1)*cos(a)-(y2-yl)*sin(a));
long yr=yl+(x2-x1y*sin(a)+(y2-y l)*cos(a)):
setcolor(2);
rectangle(xl,yl,xr,yr);
getch();
break;
}

case 3:

int x1=30,yl=30,x2=70,y2=70,y=2,x-2;
cout<<"Before scaling"<<endl;
setcolor(3);
rectangle(xl,y1,x2,y2);
cout<<"After scaling"<<endl;
16
201061101118 MANISH SINGH SURYAVANSHI
setcolor( 10);
rectangle(x1 *x,yl*y,x2*x,y2*y);
getch():
break;
}

case 4:

int x1=20,yl=300,x2=500,y2=300,x3=350,y3=400;
cout<<"triangle before reflection"<<endl;
setcolor(3):
line(x1,yl,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
cout<<"triangle after reflection"<<endl;
setcolor(5);
line(x1,-y l+500,x2,-y2+500);
line(x1,-yl+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
getch();
break;

case 5:

int xl=400, yl=100, x2=600, y2=100, x3=400, y3-200, x4=600,


y4-200, shx-2:
cout<<"Before shearing of rectangle"<<endl;
setcolor(3);
line(x1, yl, x2, y2);
line(x1, yl, x3, y3);
line(x3, y3, x4, y4);
line(x2, y2, x4, y4);
cout<<"After shearing of rectangle"<<endi;
xl=x1+shx*y1;
x2=x2+shx *y2;
X3=x3+shx*y3;
X4-x4tshx*y4;
17
201061101118 MANISH SINGH SURYAVANSHI
setcolor( 13);
line(x1, yl, x2, y2);
line(x1, yl, x3, y3);
line(x3, y3, x4, y4);
line(x2, y2, x4, y4);
getch();

default:

cout<<"Invalid Selection"<<endl;
break:
}

closegraph();
return 0;

OUTPUT

This program is done by: MANISH SINGH SURYAUANSHI 201061101118

1.Translation
2.Rotation
3.Scaling
4.Ref lection
5.Shearing
Selection:1
Rectangle before translat ion
Bectang le after translat ion

18
201061101118 MANISH SINGH SURYAVANSHI
This program is done by: MANISH SINGH SURYAUANSHI 201061101118
1.Translat ion
2.Rotation
3.Scaling
4. Ref lect ion
5.Shear ing
Selection:2
Rectang le with rotation
Ang
le of rotation :180

This program is done by: MANISH SINGH SURYAUANSHI 201061101118


1.Translat ion
2.Ratat ign
3.Scaling
4.Ref lection
5.Shear ng
Selection:3
Before scaling
Af ter scaling

This program is done by: MANISH SINGH SURYAUANSHI 201061101118

1.Translation
2.Rotation
3.Scaling
4.Ref lect ion
5.Shearing
Selection :5
Before shear ing of rectang le
Af ter shear ing of rectang le

19
201061101118 MANISH SINGH SURYAVANSHI
This program is done by: MANISH SINGH SURYAUANSHI 201061101118

1.Translat ion
2.Rotation
3.Scal ing
4.Ref lect ion
5.Shear ing
Select ion :4
triang le before ref lect ion
triangle after ref lect ion

RESULT

Thus, the program to draw and manipulate the polygon in two dimensions using c++ was executed and
verified successfully.

20
201061101118 MANISH SINGH SURYAVANSHI
Ex. No.5 Date: 07-09-2022

IMPLEMENTATION OF BRESENHAM's LINE DRAWING ALGORITHM

AIM

To write a C program to implement Bresenham's Line Drawing Algorithm to draw a line.

ALGORITHM

Step1. Start the program


Step2. Include the "graphics.h'" library file in the text program.
Step3. Declare the variables x, x2, y, y2, dx, dy, k, pk
Step4. Take the input for initial and points i.e. x, x2, y, y2
Step5. Calculate dx and dy
i. dx=x2-x

i. dy-y2-2
Step6. Calculate the number of steps to repeat the program to print all the points
if(abs(dx)>>abs(dy)) then
k=abs(dx)
else abs(dy)
Step7. Calculate the decision parameter pk, pk-(2*dy)-dx
Step8. Use for loop to repeat the step till k

if pk<0 then
pk-pk+(2*dy)
X=X+1

if pk>0 then
pk-pk+(2*dy)-(2*dx)
X=X+1

yy-1
Step9. To print all the points use putpixels(...)
Step10. Delay(...) is used to make slow printing
Step11. Stop the program

21
201061101118 MANISH SINGH SURYAVANSHI
PROGRAM

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int main)

int gd-DETECT, gm, i, j;


float x, x2, y, y2, dx, dy, k, pk, m;
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118n");
printf("nEnter Your Initail co-ordinates: "):
scanf("%f%f", &x, &y);
printf("Enter Your End co-ordinates:"):
scanf("%f%f", &x2, &y2);
dx=x2-x;
dy-y2-y;
if(abs(dx)>abs(dy))
k=abs(dx);
else
k=abs(dy);
pk-(2*dy)-dx;
for(i-0; i<k; itt)

if(pk<0)

pk-pk+(2*dy);
X=X+1;

y=y;
putpixel(x, y, 5);

if(pk>0)

pk-pk+(2*dy)-(2*dx);
X=X+1;

22
201061101118 MANISH SINGH SURYAVANSHI
y=y+l;
putpixel(x, y, 5);

delay(50);

getch();
closegraph():
return 0;
}

OUTPUT
This program is done by: MANISH SINGH SURYAUANSHI 201061101118

Enter Your Initail co-ordinates: 200 200


Enter Your End co-ord inates: 400 200

RESULT

Thus, the program to implement Bresenham's Line Drawing Algorithm to draw a line was exccuted
and verified successfully.

23
201061101118 MANISH SINGH SURYAVANSHI
Ex. No. 6 Date: 14-09-2022

IMPLEMENTATION OF 2D WINDOWING

AIM

To write a C program to implement 2D Windowing.

ALGORITHM

Step1. Start the program


Step2. Declare the variables and detect the graph variables.
Step3. Draw the line clipping and window-viewport mapping..
Step4. Using the for loop get the polygon variables.
Step5. Define windows coordinates
Step6. In case 1,transform the polygon by given input length from the original image.
Step7. In case 2,rotate the polygon using the given angle and looping statement from the original
image.
Step8. In case 3,to scale the polygon from the original image by given input

Step9. Menu() function give the menu options.


Step10. Stop the program

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{

int xwmin, ywmax, ywmin, xwmax, xvl, yv1, xvmin, yvmin, xvmax, yvmax, xw, yw,xv,yv;
int gd-DETECT.gm; initgraph(&gd,&gm," ");
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118n");
printf("\n ENTER THE WINDOW COORDINATES: nnln");

printf("nnXWMINnn");
scanf("%d",&xwmin):
printf("nnXWMAXnln");
scanf("%d",&xwmax);
printf("nnYWMINnn");

24
201061101118 MANISH SINGH SURYAVANSHI
scanf("%d",&ywmin);
printf("nnYWMAXnn");
scanf("%d",&ywmax);
line(xwmin-25,xwmin-25,xwmin-25.ywnmax+50);
line(xwmin+50.ywmax+25,xwmax+50,ywmax+25);
outtextxy(xwmin+5,ywmax+5,"WINDOw"):
line(xwmin,ywmin,xwmin,ywmax);
line(xwnmin,ywmax,xwmax,ywmax);
line(xwmax,ywmax,xwmax,ywmin);
line(xwmax,ywmin,xwmin,ywmin);
XV=XWmax/2;

XVmin=Xwmin/2;

yvmin=ywmin/2;
yvmax=ywmax/2;
line(xvmin+275,xvmin+275,xvmin+275,ywmax+325);
line(xvmin+255,yvmax+3 15,xvmax+325,yvmax+315);
outtextxy(xvmin+305,yvmax+305,"VIEW PORT");
line(xvmin+300,yvmin+300,xvmin+300,yvmax+300);
line(xvmin+300,yvmax+300,xvmax+300,yvmax+300);
line(xvmax+300,yvmax+300,xvmax+300,yvmin+300);
line(xvmax+300,yvmin+300,xvmin+300,yvmin+300);
xw=XWmin+50;
yw=ywmin+50;
putpixel(xw,yw, 1 3);
xvl=(xvmax-xvmin)/(xwmax-Xwmin)*(xw-xwmin);
XV=XVl+xvmin:

yvl-{(yvmax-yvmin)/(ywmax-ywmin))*(yw-ywmin);
y-yvltyvmin;
putpixel(xv+325,yvt325,13);
getch():
closegraph);
}

25
201061101118 MANISH SINGH SURYAVANSHI
OUTPUT
This program is done by: MANISH SINGH SURYAUANSHI 201061101118
:

ENTER THE WINDOW COORD INATES

XMIN

100

HINDOW

200

YuMIN

100

YWMAX

200
UIEH PORT

RESULT

Thus, the program to implement Mid-Point Line Algorithm to draw a line was executed and verified
successfully

26
201061101118 MANISH SINGH SURYAVANSHI
Ex. No.7 Date: 21-09-2022

IMPLEMENTATION OF COHEN SUDHERLAND AND LINE CLIPPING


ALGORITHM

AIM

To writeaCprogram to to draw and line clipping.

ALGORITHM

Step1. Start the program


Step2. Declare the variables and detect the graph variables.
Step3. Draw the line clipping and window-viewport mapping.
Step4. Using the for loop get the polygon variables.
Step5. Whether the function of the menu() is less than and construct the polygon in different manner
by using SWITCH statement.
Step6. In case 1,transform the polygon by given input length from the original image.

Step7. In case 2,rotate the polygon using the given angle and looping statement from the original

image.
Step8. In case 3,to scale the polygon from the original image by given input

Step9. Menu() function give the menu options.


Step10. Stop the program

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
typedef struct coord

int x,y;
char code[4);
}PT;
void drawwindow():;
void drawline(PT pl,PT p2,int cl);
PT setcode(PT p);

27
201061101118 MANISH SINGH SURYAVANSHI
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()

int gd=DETECT.gm,v; PT pl,p2,ptemp;


initgraph(&&gd,&gm," ");
cleardevice);
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118\n");
printf("n\nCOHEN SUDHERLAND AND LINE CLIPPING ALGORITHMn");
printf("\nnENTER THE TWO END POINTS PI(X,Y):");
scanf("%d%d",&pl.x,&pl.y);
printf("n\nENTER THE TWO END POINTSp2(X,Y):");
scanf("%d%d",&p2.x,&p2.y):
cleardevice(); printf("MnnCLIPPING WINDOW");
drawwindow();
getch();
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118n");
printf("\nnCOHEN SUTHERLAND LINE CLIPPING ALGORITHMnn");
printf("n\t\t BEFORE CLIPPING");
drawline(pl,p2,4); getch):
pl=setcode(pl): p2-setcode(p2):
v-visibility(pl.p2);
switch(v)

case 0: cleardevice);

drawwindow();
drawline(pl,p2,15);
break;
case 1: cleardevice();
drawwindow();
break;
case 2:

cleardevice();
pl=resetendpt(pl,p2);
p2-resetendpt(p2,p1);
drawwindow();
28
201061101118 MANISH SINGH SURYAVANSHI
printf("This program is done by: MANISH SINGH SURYAVANSHI
It 201061101118\n");
printf("\nCOHEN SUTHERLAND CLIPPING ALGORITHM");
printf("n \tlt AFTER CLIPPINGnln");
drawline(pl,p2,15);
break;

getch);
closegraph);

void drawwindow()

setcolor(RED);
setlinestyle(DOTTED LINE,1,l);
line(150, 100, 100, 100);
line(1 50,100,150,50);
line(450, 100, 500, 100);
line(450, 100, 450, 50);
line(450, 350, 500, 350);
line(450, 350, 450, 400);
line(150,350,150,400);
line(150, 350, 100, 350);
line(150, 100, 100, 100);
line(150, 100, 150, 50);
setlinestyle(SOLID LINE,1,1);
line(150, 100, 450, 100);
line(450, 100, 450, 350);
line(450, 350, 150, 350);
line(150, 350, 150, 100);
outtextxy(450, 30, "1001");
outtextxy(470, 200, "1000");
outtextxy(470, 370, "1010");
outtextxy(300, 370, "0010");
outtextxy(120, 370, "0110");
outtextxy(120, 200, "0100");
outtextxy(120, 30, "0101");
29
201061101118 MANISH SINGH SURYAVANSHI
outtextxy(300, 30, "0001");
outtextxy(300, 200, "0000");

void drawline(PT pl,PT p2,int cl)

setcolor(cl);
line(pl.x, pl.y, p2.x, p2.y):
}

PT setcode(PT p)

PT ptemp;
if(p.y<100)
ptemp.code[0]-1';
else
ptemp.code[0]="0';
if(p.y>350)
ptemp.code[1]='1;
else
ptemp.code[1]=0';
if(p.x>200)
ptemp.code[2]='1';
else
ptemp.code[2]="0';
if(p.x<150)
ptemp.code[3]=1';
else
ptemp.code[3]-0';
ptemp.x-p.x;
ptemp.y-P-y;
return(ptemp);

int visibility(PT p1,PT p2)

int i,flag-0;
for(i-0;i<4;itt)

30
201061101118 MANISH SINGH SURYAVANSHI
if(pl.code[i]!="0)|l(p2.code[i]!-0))
flag-1;

if(flag--0)
return 0;
for(i=0;i<4;i++)

if(pl.code[il--p2.code[il) &&(pl.code[i]1))
flag-0;

if(flag==0)
return(1);
return(2);
}

PT resetendpt(PT pl,PT p2)

PT temp; int x,y,i; float m,k;


if(pl.code[3]-1)
x=150;
if(pl.code[2]1)
x=450;
if(pl.code[3]==1"l(pl.code[2]-I))

m-(float)(p2.y-pl.y)/(p2.x-pl.x); k-(pl.yt(m*(x-pl.x));
temp.y=k;
temp.xFX;
for(i-0;i<4;it)

temp.code[lpl.code[i];
if(temp.yK=350&&temp.y>=100)
return(temp);

if(pl.code[0]-1)
y=100;
if(pl.code[l]=)
y-350;
31
201061101118 MANISH SINGH SURYAVANSHI
if(pl.code[0]--1')||(pl.code[1]F)

m-(float)(p2.y-pl.y)p2.x-p1.x); k-(float)(p l.x+(float)(y-pl.y)/m):


temp.y=y;
temp.x=k;
for(i-0;i<4;it+)
temp.code[ilpl.code[i);
return(temp);

else
return(pl):
return(p2);:
}

OUTPUT This program is done by: MANISH SINGH SURYAUANSHI 201061101118

COHEN SUDHERLAND AND LINE CLIPP ING ALGOR ITHM

ENTER THE TWO END POINTS P1(X,Y):100 200

ENTER THE THO END POINTSp2 (X, Y):600 500

O101 O001 1001

CLIPP ING WINDOW10d


DO00 1000

O110 OO10 1010

32
201061101118 MANISH SINGH SURYAVANSHI
O101 O001 1001

CLIPPING WINDOWTh
is program is done by: MANISH SINGH SURYAUANSHI 2010611
01118

COHEN SUTHERLAND LINE CLIPPING ALGORITHM

BEFORE CLIPPING

0110 O010 1010

O101 O001 1001

O100 O000 1000

Thìs program is done by:MANISH SINGH SURYAUANSH


I 201061101118

COHEN SUTHERLAND CLIPP ING ALGORITHM


AFTER CLIPP ING O010 1010

RESULT

Thus, the program to implement DAA Algorithm to draw a line was executed and verified successfully.

33
201061101118 MANISH SINGH SURYAVANSHI
Ex. No. 8 Date: 21-09-2022

IMPLEMENTATION OF BRESENHAM's CIRCLE ALGORITHM

AIM

To write aC program to implement Bresenham's Circle Algorithm to draw a line.

ALGORITHM

Step1. Start the program


Step2. Include the "graphics.h'" library file in the text program.
Step3. Declare the variables xc, yc, r, p, x, y
Step4. Take the input for center of the circle and radius i.e. xc, yc, r
Step5. Initialize center-x with zero and center-y with radius
Step6. Calculate the decision parameter, p=3-(2*r)
Step7. Use for loop to repeat the step till x<=y
if p<0 then
Ppt(4*x) +6

else
y=y-1
Pp+(4*(x-y) +10
Step8. To print all the points use putpixels(...)
Step9. Delay(...) is used to make slow printing
Step10. Stop the program

PROGRAM
#include<graphics.h
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int main(0

int xc, yc, I, p, x, y, gd-DETECT, gm;


initgraph(&gd, &gm, "C:/TURBOC3/BGI");
clrscr);
printf("This program is done by: MANISH SINGH SURYAVANSHI \t 201061101118\n");
printf("nEnter Circle Center points(Xo,Yo): ");

34
201061101118 MANISH SINGH SURYAVANSHI
scanf("%d%d", &xc, &yc);
printf("nEnter the radius: ");
scanf("%d",&r);
x-0;
y=r;
p-3-(2*r);
for(x-0; x<-y; x++)

if(p<0)
p=pt(4*x) +6;
else

y=y-1;
p-pt(4*(x-y)+10;

putpixel(xctx, ycty, RED);


putpixel(xc-x, ycty, RED);
putpixel(xctx, yc-y, BLUE);
putpixel(xc-x, yc-y, BLUE);
putpixel(xcty, yctx, GREEN);
putpixel(xc-y,yetx, GREEN);
putpixel(xcty, yc-x, YELLOW);
putpixel(xc-y, yc-x, YELLOW);

getch);
closegraph();
return 0;

35
201061101118 MANISH SINGH SURYAVANSHI
OUTPUT

This program is done by: MANISH SINGH SURYAUANSHI 201061101118


Enter Circle Center points (X0, Yo): 200 200

Enter the radius: 66

RESULT

Thus, the program to implement Bresenham's Circle Algorithm to draw a line was executed and
verified successfully.

36
201061101118 MANISH SINGH SURYAVANSHI

You might also like