Professional Documents
Culture Documents
CG
CG
INDEX
Ex.
No
Date Name of the Experiment
P.
No
Sign
1
BRESENHAMS LINE DRAWING
ALGORITHM
2
BRESENHAMS CIRCLE DRAWING
ALGORITHM
3
BRESENHAMS ELLIPSE DRAWING
ALGORITHM
4
OUTPUT PRIMITIVES
5
TWO DIMENSIONAL
TRANSFORMATION
6
COMPOSITE TWO DIMENSIONAL
TRANSFORMATION
7
COHEN SUTHERLAND 2D LINE
CLIPPING
8
WINDOWING TO VIEWPORT
MAPPING
9
SUTHERLAND HODGEMANN
POLYGON CLIPPING ALGORITHM
10
THREE DIMENSIONAL
TRANSFORMATION
11
COMPOSITE THREE DIMENSIONAL
TRANSFORMATION
12
GENERATING FRACTAL IMAGES
COMPUTER GRAPHICS
EX NO ! "RESEN#AM$S LINE DRA%ING ALGORIT#
PROGRAM
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2;
int gd!"#"$#,gm;
void line%res(int,int,int,int);
print&('"nter the t(o end points)');
scan&('*d*d*d*d',+x1,+x2,+y1,+y2);
initgraph(+gd,+gm,'');
cleardevice();
line%res(x1,y1,x2,y2);
getch();
line(x1,y1,x2,y2);
getch();
closegraph();
,
void line%res(int x1,int y1,int x2,int y2)
{
int dxa%s(x1-x2),dya%s(y1-y2);
int p,x,y,i,xend,yend;
i&(dx./)
{
p20dy-dx;
i&(x1>x2)
{
xx2;
yy2;
xendx1;
,
else
{
xx1;
yy1;
xendx2;
,
putpixel(x,y,2);
&or(ix;i<xend;i11)
{
x11;
i&(p</)
COMPUTER GRAPHICS
p120dy;
else
p120(dy-dx);
,
putpixel(x,y,2);
,
else
{
p20dx-dy;
i&(y1>y2)
{
xx2;
yy2;
yendy2;
,
putpixel(x,y,2);
&or(iy;i<yend;i11)
{
y11;
i&(p</)
p120dx;
else
{
x11;
p120(dx-dy);
,
putpixel(x,y,2);
,
,
,
O&TP&T
2resenham line dra(ing algorithm
"nter the co-ordinates
13/ 1// 23/ 4//
COMPUTER GRAPHICS
EX NO ' "RESEN#AM$S CIRCLE DRA%ING ALGORIT#M
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd!"#"$#,gm;
int x,y,r;
void cir(int,int,int);
print&('"nter the 5id points and 6adious)');
scan&('*d*d*d',+x,+y,+r);
initgraph(+gd,+gm,'');
cir(x,y,r);
getch();
closegraph();
,
void cir(int x1,int y1,int r)
{
int x/,yr,p1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
(hile(x<y)
{
x11;
i&(p</)
p120x11;
else
{
y--;
p120(x-y)11;
,
cliplot(x1,y1,x,y);
,
,
void cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr 1x,yctr 1y,1);
putpixel(xctr -x,yctr 1y,1);
putpixel(xctr 1x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr 1y,yctr 1x,1);
putpixel(xctr -y,yctr 1x,1);
COMPUTER GRAPHICS
putpixel(xctr 1y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch();
,
OUTPUT:
Enter the Mid points and Radious:100 100 50
COMPUTER GRAPHICS
EX NO ( "RESEN#AM$S ELLIPSE DRA%ING ALGORIT#M
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
main()
{
int gd!"#"$#,gm;
int xcenter,ycenter,rx,ry;
int p,x,y,px,py,rx1,ry1,rx2,ry2;
initgraph(+gd,+gm,'c)77tc77%gi');
print&('"nter #he 6adius 8alue)7n');
scan&('*d*d',+rx,+ry);
print&('"nter #he xcenter and ycenter 8alues)7n');
scan&('*d*d',+xcenter,+ycenter);
ry1ry0ry;
rx1rx0rx;
ry220ry1;
rx220rx1;
x/;
yry;
plotpoints(xcenter,ycenter,x,y);
p(ry1-rx10ry1(/.230rx1));
px/;
pyrx20y;
(hile(px<py)
{
xx11;
pxpx1ry2;
i&(p>/)
yy-1;
pypy-rx2;
i&(p</)
pp1ry11px;
else
pp1ry11px-py;
plotpoints(xcenter,ycenter,x,y);
p(ry10(x1/.3)0(x1/.3)1rx10(y-1)0(y-1)-rx10ry1);
(hile(y>/)
{
yy-1;
pypy-rx2;
i&(p</)
{
xx11;
pxpx1ry2;
COMPUTER GRAPHICS
,
i&(p>/)
pp1rx1-py;
else
pp1rx1-py1px;
plotpoints(xcenter,ycenter,x,y);
,,
getch();
return(/);
,
int plotpoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter1x,ycenter1y,9);
putpixel(xcenter-x,ycenter1y,9);
putpixel(xcenter1x,ycenter-y,9);
putpixel(xcenter-x,ycenter-y,9);
,
O&TP&T
"nter the 6adius 8alue ) 1/ 4/
"nter the : $enter and ; $enter) 4// 13/
COMPUTER GRAPHICS
EX NO ) O&TP&T PRIMITI*ES
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch<y<;
int gd!"#"$#,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(+gd,+gm,'');
(hile(ch<y<)
{
cleardevice();
set%=color(>);
outtextxy(1//,14/,'$hoose ?rom #he ?ollo(ing ');
outtextxy(1//,13/,'1. @ine');
outtextxy(1//,1A/,'2.$ircle');
outtextxy(1//,1>/,'4.2ox');
outtextxy(1//,21/,'B.Crc');
outtextxy(1//,24/,'3."llipse');
outtextxy(1//,23/,'9.6ectangle');
outtextxy(1//,2A/,'A."xit');
chgetch();
cleardevice();
s(itch(ch)
{
case <1<)
line(1//,2//,4//,B//);
%rea=;
case <2<)
circle(2//,2//,1//);
%rea=;
case <4<)
set&illstyle(3,B);
%ar(1//,4//,2//,1//);
%rea=;
case <B<)
set&illstyle(3,B);
arc(2//,2//,1//,4//,1//);
%rea=;
case <3<)
set&illstyle(3,B);
&illellipse(1//,1//,3/,1//);
%rea=;
case <9<)
settextstyle(!"?CD@#E?FG#,/,2);
outtextxy(12/,1B/,'C5H$F@@"I"');
COMPUTER GRAPHICS
line(1//,1//,1//,4//);
line(4//,4//,1//,4//);
line(1//,1//,4//,1//);
line(4//,1//,4//,4//);
%rea=;
case <A<)
closegraph();
return;
,
ch<y<;
getch();
,
,
O&TP&T
$hoose &rom the &ollo(ing
1.@ine
2. $ircle
4.2ox
B.Crc
3."llipse
9.6ectangle
A."xit
1. LINE '. CIRCLE
COMPUTER GRAPHICS
). ARC +. ELLIPSE
,. RECTANGLE
EX NO + T%O - DIMENSIONAL TRANSFORMATION
COMPUTER GRAPHICS
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdli%.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void re&lection();
int aJ1/KJ2K,i,x,option,temp,angle,tx,ty,&x,&y,sh,=,n,axis,y;
&loat sx,sy;
void menu()
{
print&('menu7n');
print&('1.#ranslation7n');
print&('2.rotation7n');
print&('4.scaling7n');
print&('B.shearing7n');
print&('3.re&lection7n');
print&('9.exit7n');
print&('enter the choice)');
scan&('*d',+option);
s(itch(option)
{
case 1)
input();
translation();
%rea=;
case 2)
input();
rotation();
%rea=;
case 4)
input();
scaling();
%rea=;
case B )
input();
shearing();
%rea=;
case 3)
COMPUTER GRAPHICS
input();
re&lection();
%rea=;
case 9)
exit(/);
%rea=;
,
,
void input()
{
print&('enter the num%er o& vertices)' );
scan&('*d',+n);
&or(i/;i<n;i11)
{
print&('enter the coordinates)');
scan&('*d*d*d*d',+aJiKJ/K,+aJiKJ1K,+aJi11KJ/K,+aJi11KJ1K);
,
,
void output()
{
cleardevice();
&or(i/;i<n;i11)
{
line(aJiKJ/K,aJiKJ1K,aJi11KJ/K,aJi11KJ1K);
,
,
void translation()
{
output();
print&('enter the tran&ormation vertex tx,ty)7n');
scan&('*d*d',+tx,+ty);
&or(i/;i<n;i11)
{
aJiKJ/KaJiKJ/K1tx;
aJiKJ1KaJiKJ1K1ty;
,
output();
delay(1/);
menu();
,
void rotation()
{
output();
print&('enter the rotating angle)');
scan&('*d',+y);
print&('enter the pivot point)');
scan&('*d*d',+&x,+&y);
=(y04.1B)L1M/;
COMPUTER GRAPHICS
&or(i/;i<n;i11)
{
aJiKJ/K&x1(aJiKJ/K-&x)0cos(=)-(aJiKJ1K-&y)0sin(=);
aJiKJ1K&y1(aJiKJ/K-&x)0sin(=)-(aJiKJ1K-&y)0cos(=);
,
output();
delay(1/);
menu();
,
void scaling()
{
output();
print&('enter the scaling &actor7n');
scan&('*&*&',+sx,+sy);
print&('enter the &ixed point)');
scan&('*d*d',+&x,+&y);
&or(i/;i<n;i11)
{
aJiKJ/KaJiKJ/K0sx1&y0(1-sx);
aJiKJ1KaJiKJ1K0sy1&y0(1-sy);
,
output();
delay(1/);
menu();
,
void shearing()
{
output();
print&('enter the shear value)');
scan&('*d',+sh);
print&('enter the &ixed point)');
scan&('*d*d',+&x,+&y);
print&('enter the axis &or shearing i& x-axis then 1 i& y-axis the /)');
scan&('*d',+axis);
&or(i/;i<n;i11)
{
i&(axis1)
{
aJiKJ/KaJiKJ/K1sh0(aJiKJ1K-&y);
,
else
{
aJiKJ1KaJiKJ1K1sh0(aJiKJ/K-&x);
,
,
output();
delay(1/);
menu();
,
void re&lection()
COMPUTER GRAPHICS
{
output();
&or(i/;i<n;i11)
{
tempaJiKJ/K;
aJiKJ/KaJiKJ1K;
aJiKJ1Ktemp;
,
output();
delay(1/);
menu();
,
void main()
{
int gd!"#"$#,gm;
initgraph(+gd,+gm,'c)77tc77%gi');
menu();
getch();
,
O&TP&T
Men.
#ranslation
1. 6otation
2. 6otation
4. Hcaling
B. Hhearing
3. 6e&lection
9. "xit
"nter the choice ) 1
"nter the num%er o& 8ertices) 4
"nter the coordinates ) 4/ 13/ 1/ 2//
"nter the coordinates ) 1/ 2// 9/ 2//
"nter the coordinates ) 9/ 2// 4/ 13/
COMPUTER GRAPHICS
"nter the translation vector #x, #y ) >/ 9/
ROTATION
"nter the choice ) 2
"nter the num%er o& 8ertices) 4
"nter the coordinates ) 4/ 13/ 1/ 2//
"nter the coordinates ) 1/ 2// 9/ 2//
"nter the coordinates ) 9/ 2// 4/ 13/
"nter the 6otating Cngle ) >/
"nter the Nivot Noint ) 1// 2//
SCALING
COMPUTER GRAPHICS
"nter the choice ) 4
"nter the num%er o& 8ertices) 4
"nter the coordinates ) 4/ 13/ 1/ 2//
"nter the coordinates ) 1/ 2// 9/ 2//
"nter the coordinates ) 9/ 2// 4/ 13/
"nter the scaling ?actor ) /.4 /.B
"nter the ?ixed Noint ) 1// 2//
S#EARING
"nter the choice ) B
"nter the num%er o& 8ertices) 4
"nter the coordinates ) 4/ 13/ 1/ 2//
"nter the coordinates ) 1/ 2// 9/ 2//
"nter the coordinates ) 9/ 2// 4/ 13/
"nter the shear 8alue ) 3
COMPUTER GRAPHICS
"nter the &ixed point ) 3/ 1//
"nter the Cxis &or shearing i& x-axis then 1
i& y-axis then /
REFLECTION
"nter the choice ) 3
"nter the num%er o& 8ertices) 4
"nter the coordinates ) 4/ 13/ 1/ 2//
"nter the coordinates ) 1/ 2// 9/ 2//
"nter the coordinates ) 9/ 2// 4/ 13/
COMPUTER GRAPHICS
COMPUTER GRAPHICS
EX NO , COMPOSITE T%O - DIMENSIONAL TRANSFORMATION
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdli%.h>
void main()
{
int gd,gm,n,i,xaJ1/K,yaJ1/K,op,tx,ty,xa1J1/K,ya1J1/K,theta,x&,y&,rx,ry,
sx,sy,shx,shy,xre&,yre&;
char d;
gd!"#"$#;
initgraph(+gd,+gm,'');
cout<<'enter the no o& points';
cin>>n;
&or(i/;i<n;i11)
{
cout<<'enter the coordinates'<<i11;
cin>>xaJiK>>yaJiK;
,
do
{
cout<<'menu';
cout<<'7n1.translation7n2.rotation7n4.scaling7nB.shearing7n3.re&lection7n9.exit';
cin>>op;
s(itch(op)
{
case 1)
cout<<'enter the translation vector';
cin>>tx>>ty;
&or(i/;i<n;i11)
{
xa1JiKxaJiK1tx;
ya1JiKyaJiK1ty;
,
cleardevice();
cout<<'%e&ore translation';
&or(i/;i<n;i11)
{
line(xaJiK,yaJiK,xaJ(i11)*nK,yaJ(i11)*nK);
,
cout<<'a&ter translation';
&or(i/;i<n;i11)
{
line(xa1JiK,ya1JiK,xa1J(i11)*nK,ya1J(i11)*nK);
COMPUTER GRAPHICS
,
getch();
cleardevice();
%rea=;
case 2)
cout<<'enter the rotation angle';
cin>>theta;
theta(theta04.1B)L1M/;
cout<<'enter the re&erence points';
cin>>x&>>y&;
&or(i/;i<n;i11)
{
xa1JiKx&1(xaJiK-x&)0cos(theta)-(yaJiK-y&)0sin(theta);
ya1JiKy&1(xaJiK-x&)0sin(theta)-(yaJiK-y&)0cos(theta);
,
cleardevice();
cout<<'%e&ore rotation';
&or(i/;i<n;i11)
{
line(xaJiK,yaJiK,xaJ(i11)*nK,yaJ(i11)*nK);
,
cout<<'a&ter rotation';
&or(i/;i<n;i11)
{
line(xa1JiK,ya1JiK,xa1J(i11)*nK,ya1J(i11)*nK);
,
getch();
cleardevice();
%rea=;
case 4)
cout<<'enter the scaling &actor';
cin>>sx>>sy;
cout<<'enter the re&erence point';
cin>>rx>>ry;
&or(i/;i<n;i11)
{
xa1JiKxaJiK0sx1rx0(1-sx);
ya1JiKyaJiK0sy1ry0(1-sy);
,
cleardevice();
cout<<'%e&ore scaling';
&or(i/;i<n;i11)
{
line(xaJiK,yaJiK,xaJ(i11)*nK,yaJ(i11)*nK);
,
cout<<'a&ter scaling';
&or(i/;i<n;i11)
{
line(xa1JiK,ya1JiK,xa1J(i11)*nK,ya1J(i11)*nK);
,
COMPUTER GRAPHICS
getch();
cleardevice();
%rea=;
case B)
cout<<'enter the shear value';
cin>>shx>>shy;
cout<<'enter the re&erence point';
cin>>xre&>>yre&;
cout<<'enter the shear direction x or y';
cin>>d;
i&(d<x<)
{
&or(i/;i<n;i11)
{
xa1JiKxaJiK1shx0(yaJiK-yre&);
ya1JiKyaJiK;
,
,
cleardevice();
cout<<'%e&ore shearing';
&or(i/;i<n;i11)
{
line(xaJiK,yaJiK,xaJ(i11)*nK,yaJ(i11)*nK);
,
cout<<'a&ter shearing';
&or(i/;i<n;i11)
{
line(xa1JiK,ya1JiK,xa1J(i11)*nK,ya1J(i11)*nK);
,
getch();
cleardevice();
%rea=;
case 3)
cout<<'%e&ore re&lection';
&or(i/;i<n;i11)
{
line(xaJiK,yaJiK,xaJ(i11)*nK,yaJ(i11)*nK);
,
cout<<'a&ter re&lection';
&or(i/;i<n;i11)
{
line(yaJiK,xaJiK,yaJ(i11)*nK,xaJ(i11)*nK);
,
getch();
cleardevice();
%rea=;
case 9)
exit(/);
%rea=;
,
COMPUTER GRAPHICS
,(hile(op.9);
,
O&TP&T
enter the no o& points) 4
enter the coordinates1) 3/ 13/
enter the coordinates2) 3/ 3/
enter the coordinates 4) A3 13/
menu
1. translation
2. rotation
4. scaling
B.shearing
3.re&lection
9.exit
1
enter the translation vector) 4/ B/
2"?F6" C?#"6
menu
1. translation
2. rotation
4. scaling
B.shearing
3.re&lection
9.exit
2
enter the rotation angle) 9/
COMPUTER GRAPHICS
enter the re&erence points) 4/ B/
2"?F6" C?#"6
menu
1. translation
2. rotation
4. scaling
B.shearing
3.re&lection
9.exit
4
"nter the scaling &actor) 4 B
"nter the re&erence points) 4/ B/
2"?F6" C?#"6
menu
1. translation
2. rotation
4. scaling
B.shearing
3.re&lection
9.exit
B
"nter the shear value) 4 B
"nter the re&erence point) 2/ 4/
COMPUTER GRAPHICS
"nter the shear direction x or y) :
2"?F6" C?#"6
menu
1. translation
2. rotation
4. scaling
B.shearing
3.re&lection
9.exit
3
2"?F6" C?#"6
EX NO / CO#EN S&T#ERLAND 'D LINE CLIPPING
PROGRAM
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
&loat cxl, cxr,cyt,cy%;
code(&loat,&loat);
void clip(&loat,&loat,&loat,&loat);
void rect(&loat,&loat,&loat,&loat);
COMPUTER GRAPHICS
void main()
{
&loat x1,y1,x2,y2;
int g/,d;
initgraph(+g,+d,'c)77tc77%in');
settextstyle(1,/,1);
outtextxy(B/,13,'2"?F6" $@ONNOGI');
print&('7n7n7n please enter the le&t,%ottom,right,top,o& clip (indo(');
scan&('*&*&*&*&',+cxl,+cyt,+cxr,+cyt);
rect(cxl,cy%,cxr,cyt);
getch();
print&('7n please enter the line(x1,y1,x2,y2))');
scan&('*&*&*&*&',+x1,+y1,+x2,+y2);
line(x1,y1,x2,y2);
getch();
cleardevice();
settextstyle(1,/,1);
outtextxy(B/,13,'a&ter clipping');
clip(x1,y1,x2,y2);
getch();
closegraph();
,
void clip(&loat x1,&loat y1,&loat x2,&loat y2)
{
int c,c1,c2;
&loat x,y;
c1code(x1,y1);
c2code(x2,y2);
getch();
(hile((c1./)PP(c2./))
{
i&((c1+c2)./)
goto out;
cc1;
i&(c/)
cc2;
i&((c+1)1)
{
yy11(y2-y1)0(cxl-x1)L(x2-x1);
xcxl;
,
else
i&((c+2)2)
{
yy11(y2-y1)0(cxl-x1)L(x2-x1);
xcxr;
,
else
i&((c+M)M)
{
COMPUTER GRAPHICS
xx11(x2-x1)0(cy%-y1)L(y2-y1);
ycy%;
,
else
i&((c+B)B)
{
xx11(x2-x1)0(cyt-y1)L(y2-y1);
ycyt;
,
i&(cc1)
{
x1x;
y1y;
c1code(x,y);
,
else
{
x2x;
y2y;
c2code(x,y);
,
,
out ) rect(cxl,cy%,cxr,cyt);
line(x1,y1,x2,y2);
,
code(&loat x,&loat y)
{
int c/;
i&(x<cxl)
c1;
else
i&(x>cxr)
c2;
i&(y<cy%)
ccPM;
else
i&(y>cyt)
ccPB;
return c;
,
void rect(&loat xl,&loat y%,&loat xr,&loat yt)
{
line(xl,y%,xr,y%);
line(xr,y%,xr,yt);
line(xr,yt,xl,yt);
line(xl,yt,xl,y%);
,
O&TP&T
COMPUTER GRAPHICS
"nter the le&t, %ottom, right ,top o& clip (indo(
2//
2//
B//
B//
enter the line coordinates
23/
4//
B//
B3/
EX NO 0 %INDO%ING TO *IE%PORT MAPPING
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
&loat sx,sy;
int (1,(2,(4,(B,x1,x2,x4,xB,y1,y2,y4,yB,v1,v2,v4,vB;
int gd!"#"$#,gm;
initgraph(+gd,+gm,'c)77tc77%gi');
COMPUTER GRAPHICS
print&('"nter #he $oordinate x1,y1,x2,y2,x4,y47n');
scan&('*d*d*d*d*d*d',+x1,+y1,+x2,+y2,+x4,+y4);
cleardevice();
(13;
(23;
(4943;
(BB93;
rectangle((1,(2,(4,(B);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x1,y1);
getch();
v1B23;
v2A3;
v433/;
vB23/;
sx(&loat)(v4-v1)L((4-(1);
sy(&loat)(vB-v2)L((B-(2);
rectangle(v1,v2,v4,vB);
x1v11&loor(((&loat)(x1-(1)0sx)1.3);
x2v11&loor(((&loat)(x2-(1)0sx)1.3);
x4v11&loor(((&loat)(x4-(1)0sx)1.3);
y1v21&loor(((&loat)(y1-(2)0sy)1.3);
y2v21&loor(((&loat)(y2-(2)0sy)1.3);
y4v21&loor(((&loat)(y4-(2)0sy)1.3);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x1,y1);
getch();
return /;
,
OUTPUT:
"nter #he $oordinate x1,y1,x2,y2,x4,y4
1//
2//
4//
B//
3//
43/
COMPUTER GRAPHICS
EX NO 1 S&T#ERLAND - #ODGEMANN POL2GON CLIPPING ALGORIT#M
PROGRAM
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#de&ine #6D" 1
COMPUTER GRAPHICS
#de&ine ?C@H" /
typede& unsigned int outcode;
outcode $ompFut$ode(&loat x,&loat y);
enum{
#FN /x1,
2F##F5 /x2,
6OIQ# /xB,
@"?# /xM
,;
&loat x(min,x(max,y(min,y(max;
void clip(&loat x/,&loat y/,&loat x1,&loat y1)
{
outcode outcode/,outcode1,outcodeFut;
int accept ?C@H",done ?C@H";
outcode/ $ompFut$ode(x/,y/);
outcode1 $ompFut$ode(x1,y1);
do
{
i&(.(outcode/Poutcode1))
{
accept #6D";
done #6D";
,
else
i&(outcode/ + outcode1)
done #6D";
else
{
&loat x,y;
outcodeFut outcode/Routcode/)outcode1;
i&(outcodeFut + #FN)
{
x x/1(x1-x/)0(y(max-y/)L(y1-y/);
y y(max;
,
else
i&(outcodeFut + 2F##F5)
{
x x/1(x1-x/)0(y(min-y/)L(y1-y/);
y y(min;
,
else
i&(outcodeFut + 6OIQ#)
{
y y/1(y1-y/)0(x(max-x/)L(x1-x/);
x x(max;
,
else
{
y y/1(y1-y/)0(x(min-x/)L(x1-x/);
COMPUTER GRAPHICS
x x(min;
,
i&(outcodeFutoutcode/)
{
x/ x;
y/ y;
outcode/ $ompFut$ode(x/,y/);
,
else
{
x1 x;
y1 y;
outcode1 $ompFut$ode(x1,y1);
,,,
(hile(done?C@H");
i&(accept)
line(x/,y/,x1,y1);
outtextxy(13/,2/,'NF@;IFG C?#"6 $@ONNOGI');
rectangle(x(min,y(min,x(max,y(max);
,
outcode $ompFut$ode(&loat x,&loat y)
{
outcode code /;
i&(y>y(max)
codeP#FN;
else
i&(y<y(min)
codeP2F##F5;
i&(x>x(max)
codeP6OIQ#;
else
i&(x<x(min)
codeP@"?#;
return code;
,
void main( )
{
&loat x1,y1,x2,y2;
L0 reSuest auto detection 0L
int gdriver !"#"$#, gmode, n,polyJ1BK,i;
clrscr( );
print&('"nter the no o& sides o& polygon)');
scan&('*d',+n);
print&('7n"nter the coordinates o& polygon7n');
&or(i/;i<20n;i11)
{
scan&('*d',+polyJiK);
,
polyJ20nKpolyJ/K;
polyJ20n11KpolyJ1K;
COMPUTER GRAPHICS
print&('"nter the rectangular coordinates o& clipping (indo(7n');
scan&('*&*&*&*&',+x(min,+y(min,+x(max,+y(max);
L0 initialiTe graphics and local varia%les 0L
initgraph(+gdriver, +gmode, 'c)77tc77%gi');
outtextxy(13/,2/,'NF@;IFG 2"?F6" $@ONNOGI');
dra(poly(n11,poly);
rectangle(x(min,y(min,x(max,y(max);
getch( );
cleardevice( );
&or(i/;i<n;i11)
clip(polyJ20iK,polyJ(20i)11K,polyJ(20i)12K,polyJ(20i)14K);
getch( );
restorecrtmode( );
,
O&TP&T
"nter the no o& sides o& polygon)3
"nter the coordinates o& polygon
3/
3/
2//
1//
43/
43/
M/
2//
B/
M/
"nter the rectangular coordinates o& clipping (indo(
13/
13/
4//
4//
POL2GON CLIPPING
COMPUTER GRAPHICS
EX NO !3 T#REE - DIMENSIONAL TRANSFORMATION
PROGRAM
COMPUTER GRAPHICS
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,/,midx,maxy);
line(/,midy,maxx,midy);
,
void main()
{
int gd,gm,x,y,T,o,x1,x2,y1,y2;
detectgraph(+gd,+gm);
initgraph(+gd,+gm,' ');
set&illstyle(/,getmaxcolor());
maxxgetmaxx();
maxygetmaxy();
midxmaxxL2;
midymaxyL2;
axis();
%ar4d(midx13/,midy-1//,midx19/,midy->/,3,1);
print&('"nter #ranslation ?actor');
scan&('*d*d*d',+x,+y,+T);
axis();
print&('a&ter translation');
%ar4d(midx1(x13/),midy-(y11//),midx1x19/,midy-(y1>/),3,1);
axis();
%ar4d(midx13/,midy11//,midx19/,midy->/,3,1);
print&('"nter Hcaling ?actor');
scan&('*d*d*d',+x,+y,+T);
axis();
print&('C&ter Hcaling');
%ar4d(midx1(x03/),midy-(y01//),midx1(x09/),midy-(y0>/),30T,1);
axis();
%ar4d(midx13/,midy-1//,midx19/,midy->/,3,1);
print&('"nter 6otating Cngle');
scan&('*d',+o);
x13/0cos(o04.1BL1M/)-1//0sin(o04.1BL1M/);
y13/0cos(o04.1BL1M/)11//0sin(o04.1BL1M/);
x29/0sin(o04.1BL1M/)->/0cos(o04.1BL1M/);
y29/0sin(o04.1BL1M/)1>/0cos(o04.1BL1M/);
axis();
print&('C&ter 6otation a%out U Cxis');
%ar4d(midx1x1,midy-y1,midx1x2,midy-y2,3,1);
axis();
print&('C&ter 6otation a%out : Cxis');
%ar4d(midx13/,midy-x1,midx19/,midy-x2,3,1);
COMPUTER GRAPHICS
axis();
print&('C&ter 6otation a%out ; Cxis');
%ar4d(midx1x1,midy-1//,midx1x2,midy->/,3,1);
getch();
closegraph();
,
O&TP&T
Tran45ation
"nter #ranslation ?actor ) 3/ 9/ A/
2e&ore #ranslation
C&ter #ranslation
S6a5ing
"nter Hcaling ?actor ) M/ >/ >3
COMPUTER GRAPHICS
C&ter Hcaling
Rotation
"nter 6otating Cngle ) 9/
C&ter 6otation a%out U-Cxis
COMPUTER GRAPHICS
C&ter 6otation a%out :-Cxis
C&ter 6otation a%out ;-Cxis )
COMPUTER GRAPHICS
EX NO !! COMPOSITE T#REE - DIMENSIONAL TRANSFORMATION
PROGRAM
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdli%.h>
class cu%e
{
pu%lic)
void dra(cu%e(int x1JK,int y1JK)
{
int i;
&or(i/;i<B;i11)
{
i&(i<4)
line(x1JiK,y1JiK,x1Ji11K,y1Ji11K);
line(x1J/K,y1J/K,x1J4K,y1J4K);
,
&or(iB;i<M;i11)
{
i&(i<A)
line(x1JiK,y1JiK,x1Ji11K,y1Ji11K);
line(x1JBK,y1JBK,x1JAK,y1JAK);
,
&or(i/;i<B;i11)
{
line(x1JiK,y1JiK,x1Ji1BK,y1Ji1BK);
,
,
,;
void main()
{
int i,x1JMK,y1JMK,x2JMK,y2JMK,T1JMK,x4JMK,y4JMK,T4JMK,xBJMK,yBJMK,theta,op,ch,tx,ty,tT,sx,sy,sT,x&,y&,T&,x,y,T,siTe;
int driver!"#"$#;
int mode;
initgraph(+driver,+mode,'$)77tc117%gi');
cout<<'enter the points on the cu%e)';
cin>>x>>y>>T;
cout<<'enter the siTe o& the edge)';
cin>>siTe;
x1J/Kx1J4Kx;
x1J1Kx1J2Kx1siTe;
x1JBKx1JAKx;
x1J3Kx1J9Kx1siTe;
y1J/Ky1J1Ky;
y1J2Ky1J4Ky1siTe;
y1JBKy1J3Ky;
y1J9Ky1JAKy1siTe;
T1J1KT1J2KT1J4KT1J/KT ;
T1JBKT1J3KT1J9KT1JAKT-siTe;
&or(i/;i<M;i11)
{
x2JiKx1JiK1T1JiKL2;
y2JiKy1JiK1T1JiKL2;
,
COMPUTER GRAPHICS
cu%e c;
getch();
cleardevice();
do
{
cout<<'menu'<<endl;
cout<<'7n1.translation7n2.rotation7n4.scaling7nB.exit7n';
cout<<'enter the choice)';
cin>>ch;
s(itch(ch)
{
case 1)
cout<<'enter the translation vector)';
cin>>tx>>ty>>tT;
&or(i/;i<M;i11)
{
x4JiKx1JiK1tx;
y4JiKy1JiK1ty;
T4JiKT1JiK1tT;
,
&or(i/;i<M;i11)
{
xBJiKx4JiK1T4JiKL2;
yBJiKy4JiK1T4JiKL2;
,
cleardevice();
cout<<'%e&ore translation';
c.dra(cu%e(x2,y2);
getch();
cleardevice();
cout<<'a&ter translation';
c.dra(cu%e(xB,yB);
getch();
cleardevice();
%rea=;
case 2)
cout<<'enter the rotation angle)';
cin>>theta;
theta(theta04.1B)L1M/;
cout<<'enter the direction'<<endl;
cout<<'1.rotation a%out x axis'<<endl<<'2.rotation a%out y axis'<<endl<<'4.rotation a%out T
axis'; cin>>op;
i&(op1)
{
&or(i/;i<M;i11)
{
x4JiKx1JiK;
y4JiKy1JiK0cos(theta)-T1JiK0sin(theta);
T4JiKy1JiK0sin(theta)1T1JiK0cos(theta);
,
,
else
i&(op2)
{
&or(i/;i<M;i11)
{
y4JiKy1JiK;
COMPUTER GRAPHICS
x4JiKT1JiK0cos(theta)-x1JiK0sin(theta);
x4JiKT1JiK0sin(theta)1x1JiK0cos(theta);
,
,
else
i&(op4)
{
&or(i/;i<M;i11)
{
T4JiKT1JiK;
x4JiKx1JiK0cos(theta)-y1JiK0sin(theta);
y4JiKx1JiK0sin(theta)1y1JiK0cos(theta);
,
,
else
cout<<'enter correct option';
&or(i/;i<M;i11)
{
xBJiKx4JiK1T4JiKL2;
yBJiKy4JiK1T4JiKL2;
,
cleardevice();
cout<<'%e&ore rotation';
c.dra(cu%e(x2,y2);
getch();
cleardevice();
cout<<'a&ter rotation';
c.dra(cu%e(xB,yB);
getch();
cleardevice();
%rea=;
case 4)
cout<<'enter the scaling &actor)';
cin>>sx>>sy>>sT;
cout<<'enter the re&erence point)';
cin>>x&>>y&>>T&;
&or(i/;i<M;i11)
{
x4JiKx&1(x1JiK0sx)1x&0(1-sx);
y4JiKy&1(y1JiK0sy)1y&0(1-sy);
T4JiKT&1(T1JiK0sT)1T&0(1-sT);
,
&or(i/;i<M;i11)
{
xBJiKx4JiK1T4JiKL2;
yBJiKy4JiK1T4JiKL2;
,
cleardevice();
cout<<'%e&ore scaling';
c.dra(cu%e(x2,y2);
getch();
cleardevice();
cout<<'a&ter scaling';
c.dra(cu%e(xB,yB);
getch();
cleardevice();
%rea=;
COMPUTER GRAPHICS
case B)
exit(/);
%rea=;
,
,
(hile(op.B);
getch();
,
OUTPUT :
"nter the points in the cu%e ) 1// 1// 1//
"nter the HiTe o& "dge ) 3/
5"GD
1.#ranslation
2.6otation
4.scaling
B.exit
"nter your choice ) 1
"nter the #ranslation 8ector 3 1/ 13
"nter your $hoice ) 2
Rotation
"nter the 6otation Cngle ) 9/
"nter the !irection
1.6otation a%out x-axis
2.6otation a%out y-axis
4.6otation a%out T-axis
COMPUTER GRAPHICS
COMPUTER GRAPHICS
MENU
1.#ranslation
2.6otation
4.scaling
B.exit
"nter your choice ) 4
"nter the Hcaling ?actor ) 4/ B/ 3/
"nter the 6e&erence point ) 2/ 43 B3
2"?F6"
C?#"6
COMPUTER GRAPHICS
EX NO !3 GENERATING FRACTAL IMAGES
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdli%.h>
#include <math.h>
#include <graphics.h>
void !ra(Hierpins=i(void);
void main(void)
{
int gd8IC;
int gm8ICQO;
initgraph(+gd, +gm, '77tc77%gi');
!ra(Hierpins=i();
getch();
,
void !ra(Hierpins=i(void)
{
char !irect;
int iterate;
unsigned int x1, y1, x2, y2;
x1 x2 42/;
y1 y2 /;
&or(iterate /; iterate < 1////; iterate11)
{
!irect random(4);
i&(!irect /)
{
x1 (x2 1 42/) L 2;
y1 (y2 1 /) L 2;
,
else i&(!irect 1)
{
x1 (x2 1 /) L 2;
y1 (y2 1 BM/) L 2;
,
else i&(!irect 2)
{
x1 (x2 1 9B/) L 2;
y1 (y2 1 BM/) L 2;
,
putpixel(x1, y1, VQO#");
x2 x1;
y2 y1;
,
,
COMPUTER GRAPHICS
O&TP&T