Manish Comp Graphics
Manish Comp Graphics
Dr. M.G.R.
R
socaonal and
RECORD NOTEBOOK
DEPARTMENT
Of
COMPUTER SCIENCE AND ENGINEERING
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
BONAFIDE CERTIFICATE
REGISTER NO : 201061101118
NAME OF LAB :
COMPUTER GRAPHICS – (BCS18ET2)
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.
PAGE STAFF
S.NO. DATE NAME OF THE EXPERIMENT
No. SIGNATURE
1
03-08-2022 IMPLEMENTATION OF THE DDA 01 –03
ALGORITHM
IMPLEMENTATION OF BRESENHAM's
07-09-2022 21- 23
6
14-09-2022 IMPLEMENTATION OF 2D WINDOWING
24- 26
IMPLEMENTATION OF COHEN
21-09-2022 27-33
SUDHERLAND AND LINE CLIPPING
ALGORITHM
AIM
ALGORITHM
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>
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
AIM
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;
arr[k]=xmin;
arr[k+1]=yl+m*(xmin-x ); 1
arr[k+2]-x2;
arr[k+3]-y2;
kt=4;
arr[k]=xmin;
arr[k+1]-yl+m*(xmin-x l);
k+=2;
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;
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;
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;
arr[k]-xmax;
arr[k+1]-yl +m*(xmax-xl );
arr[k+2]-x2;
arr[k+3]-y2;
kt=4;
arr[k]-xmax;
arr[k+1]=y1 +m*(xmax-xl );
kt=2;
arr[k]-x2;
arr[k+1]-y2;
k+=2;
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 :":;
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)
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
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
AIM
ALGORITHM
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)
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
ALGORITHM
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
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:
default:
cout<<"Invalid Selection"<<endl;
break:
}
closegraph();
return 0;
OUTPUT
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
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
AIM
ALGORITHM
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)
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
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
ALGORITHM
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
:
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
AIM
ALGORITHM
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
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()
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");
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 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);
}
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)
else
return(pl):
return(p2);:
}
32
201061101118 MANISH SINGH SURYAVANSHI
O101 O001 1001
CLIPPING WINDOWTh
is program is done by: MANISH SINGH SURYAUANSHI 2010611
01118
BEFORE CLIPPING
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
AIM
ALGORITHM
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
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;
getch);
closegraph();
return 0;
35
201061101118 MANISH SINGH SURYAVANSHI
OUTPUT
RESULT
Thus, the program to implement Bresenham's Circle Algorithm to draw a line was executed and
verified successfully.
36
201061101118 MANISH SINGH SURYAVANSHI