Académique Documents
Professionnel Documents
Culture Documents
S.No
.
Practical
Page
No.
1.
Introduction to
COMPUTER GRAPHICS.
2-3
2.
4-6
3.
7-9
4.
10-12
5.
13-15
6.
16-18
7.
19-23
8.
24-35
9.
36-41
10.
42-44
Date
Signature
EXPERIMENT NO 1
1
USER INTERFACE
1. Graphic interface provide an attractive and easy interaction between users
and the computer.
2. Floating of the graphics and the charts:- in industry, educational
organization , business embedded computer graphics are used to create 2D
and 3D graphs of mathematics.
3. Office automation and desktop publishing
a) Desktop publishing on personal computer allows the user of
graphic for the creation and dissemination of information.
ANIMATION
We use animation for the creating animated movies, cartoon movies etc.
ADVANTAGES
1. It has an ability to show moving pictures and thus it is possible to produce
animation with computer graphics.
2. The computer graphics provide tools called MOTION DYNAMICS.
3. Computer graphics also provide facility called UPDATE DYNAMICS.
EXPERIMENT NO 2
3
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
closegraph();
}
OUTPUT
5
EXPERIMENT NO 3
6
while(i<dy);
getch();
closegraph();
}
OUTPUT
EXPERIMENT NO 4
9
OUTPUT
1
1
EXPERIMENT NO 5
WRITE A PROGRAM TO DRAW A CIRCLE USING BRESENHAM'S METHOD
1
2
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
float d;
int i,gd,gm,x,y;
int r;
clrscr();
cout<<"Enter the radius of circle";
cin>>r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
x=0;y=r;d=3-2*r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+y,200-x,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-y,200+x,15);
putpixel(200-x,200+y,15);
if(d==0)
{
d=d+4*x+6;
}
else{
d=d+4*x-4*y+10;
y=y-1;
}
x=x+1;
}
while(x<y) ;
getch();
closegraph;
}
1
3
OUTPUT
1
4
EXPERIMENT NO 6
1
5
getch();
}
OUTPUT
1
7
EXPERIMENT NO 7
1
8
1
9
case 0: cleardevice();
p1=resetendpt(p1,p2);
p1=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break();
}
getch();
closegraph();
return (0);
}
void drawwindow()
{
setcolor(red);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(pt p1,pt p1,int cl)
{
setcolor(cl);
line(p1.x,p1.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>450)
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
2
0
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y;
return(ptemp);
{
int visibility(pt p1,pt p2)
{
int i, flag=0;
for(i=0;i<4;i++)
{
if((p1.code[1]!='0'||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]&&(p1.code[i]=='i'))
flag=0;
}
if(flag==0)
return(1);
return(2);
}
pt resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,l;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]='1'||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
2
1
if(temp.y<=350&&temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]='1'||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(flot)p1.x+(float)(y-p1.x)/m;
temp.x=k;
temp.y=y;
for (i=0;i<4;i<++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
OUTPUT
2
2
EXPRIMENT NO-8
2
3
sum=0;
for(k=0;k<3;k++)
{
sum=sum+c[i][k]*p[k][j];
}
r[i][j]=sum;
}}
cleardevice();
}
void clock(float x)
{
int i,j,k;
float p[3][3]={1,0,0,0,1,0,0,0,1},sum;
p[0][0]=cos(x);
p[0][1]=sin(x);
p[1][0]=sin(x);
p[1][1]=cos(x);
for(i=0;i<n;i++)
{
for(j=0;i<3;j++)
{
sum=0
for(k=0;k<3;k++)
{
sum=sum+t[i][k]*p[k][j];
}
r[i][j]=sum;
}}
cleardevice();
}
void anti(float x)
{
int i,j,k;
float sum;
float p[3][3]={0,0,0,0,0,0,0,0,1};
p[0][0]=cos(x);
p[0][1]=sin(x);
p[1][0]=sin(x);
p[1][1]=cos(x);
for(i=0;i<n;i++)
{
2
7
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+t[i][k]*p[k][j];
}
r[i][j]=sum;
}}
cleardevice();
}
void origin(float x)
{
int opt;
cout<<"\n\t 1.rotate clockwise.";
cout<<"\n\t 2.rotate anticlockwise.";
cout<<"\n\t enter ur choice.";
cin>>opt;
switch(opt)
{
case 1:clock(x);
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
case 2:anti(x);
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
}}
void point(float x)
{
int opt;
int a,b,i,j;
cout<<"\n\t give coordinates of point:";
cin>>a>>b;
cout<<"\n\t 1.rotate clockwise.";
cout<<"\n\t 2.rotate anticlockwise.";
2
8
display(r);
getch();
break;
}}
void rotate()
{
int ch,i,opt;
char ans;
float angle;
cout<<"\n\t give angle";
cin>>angle;
angle=(angle*3.14)/180;
do
{
cout<<"\n\t 1.rotate w.r.t. origion.";
cout<<"\n\t 2.rotate w.r.t. some other point.";
cout<<"\n\t enter ur choice";
cin>>opt;
switch(opt)
{
case 1:origin(angle);
break;
case 2:point(angle);
break;
}
cout<<"\n\t do u want to continue[y/n]:";
cin>>ans;
}
while(ans=='y'||ans=='y');
}
void reflect()
{
int opt;
float tmat[3][3]={1,0,0,0,1,0,0,0,1};
clear device();
cout<<"\n 1.reflect abt y axis\n";
cout<<"\n 2.reflect abt x axis\n";
cout<<"\n 3.reflect abt origin\n";
cout<<"\n 4.reflect abt line y=x\n";
cout<<"\n 5.reflect abt line y=-x\n";
cout<<"\n give ur choice\n";
3
0
cin>>opt;
switch(opt)
{
case 1:tmat[0][0]=-1;
multy(tmat);
break;
case 2:tmat[1][1]=-1;
multy(tmat);
break;
case 3:tmat[0][0]=-1;
tmat[1][1]=-1;
multy(tmat);
break;
case 4:tmat[0][0]=0;
tmat[0][1]=1;
tmat[1][0]=1;
tmat[1][1]=0;
multy(tmat);
break;
case 5:tmat[0][0]=0;
tmat[0][1]=-1;
tmat[1][0]=-1;
tmat[1][1]=0;
multy(tmat);
break;
default:cout<<"\n invalid choice";
}
getch();
}
void shear()
{
int opt;
float tmat[3][3]={1,0,0,0,1,0,0,0,1},a,b;
clear device();
cout<<"\n 1.y-shear\n";
cout<<"\n 2.x-shear\n";
cout<<"\n 1.give ur choice\n";
cin>>opt;
switch(opt)
{
case 1:cout<<"\n enter value of a";
3
1
cin>>a;
tmat[0][1]=a;
multy(tmat);
break;
case 2:cout<<"\n enter value of b";
cin>>b;
tmat[1][0]=b;
multy(tmat);
break;
default:cout<<"\n invalid choice";
}
getch();
}
void main()
{
int opt;
char ans;
float tx,ty,sx,sy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi";
cleardevice();
getdata();
do
{
cleardevice();
cout<<"***operation on polygon***";
cout<<"\n\n1.display original polygon";
cout<<"\n\n2.translate";
cout<<"\n\n3.scale";
cout<<"\n\n4.rotate";
cout<<"\n\n5.reflect";
cout<<"\n\n6.shear";
cout<<"\n\n7.exit";
cout<<"\n\n give ur choice";
cin>>opt;
switch(opt)
{
case 1:cleardevice();
drawaxix();
break;
case 2:cout<<"\n give value of tx";
3
2
cin>>tx;
cout<<"\n give value of ty";
cin>>ty;
translate(tx,ty);
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
case 3:cout<<"\n give value of sx";
cin>>sx;
cout<<"\n give value of sy";
cin>>sy;
scale(sx,sy);
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
case 4:rotate();
break;
case 5:reflect();
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
case 6:shear();
drawaxix();
setcolor(GREEN);
display(r);
getch();
break;
case 7:exit(0);
}
cout<<"\n do u want to continue[y/n]:";
cin>>ans;
}
while(ans=='y'||ans=='y');
getch();
closegraph();
3
3
OUTPUT
3
4
EXPERIMENT NO-9
3
5
{
sum=0
for(l=0;l<4;l++)
sum+=a[i][j][l]*t[l][k];
b[i][j][k]=sum;
}}
void trans(float a[2][4][4])
{
int l,j,tx,ty,tz;
float b[2][4][4],t[4][4];
cout<<"\n enter the X translation:";
cin>>tx;
cout<<"\n enter the Y translation:";
cin>>ty;
cout<<"\n enter the Z translation:";
cin>>tz;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j)
t[i][j]=1;
else
t[i][j]=0;
}
t[3][0]=tx;
t[3][0]=ty;
t[3][2]=tz;
mul(a,t,b);
show(b);
}
void scale(float a[2][4][4])
{
int l,j;
float sx,sy,sz;
float b[2][4][4],s[4][4];
cout<<"\n enter the X scaling factor:";
cin>>sx;
cout<<"\n enter the Y scaling factor:";
cin>>sy;
cout<<"\n enter the Z scaling factor:";
cin>>sz;
3
7
for(i=0;i<4;i++)
for(j=0;j<4;j++)
s[i][j]=0;
s[0][0]=sx;
s[1][1]=sy;
s[2][2]=sz;
mul(a,s,b);
show(b);
void rot(float a[2][4][4])
{
int l,j,an,choice;
float b[2][4][4],r[4][4];
float angle;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
r[i][j]=0;
cout<<"\n\n abt which axis:";
cout<<"\n\n\n\t1.X axis";
cout<<"\n\n\n\t2.Y axis";
cout<<"\n\n\n\t3.Z axis";
cout<<"\n\n\n enter ur choice";
cin>>choice;
cout<<"\n enter the angle of rotation:";
cin>>an;
angle=an*pi/180;
switch(choice)
{
case 1:r[0][0]=1;
r[1][1]=r[2][2]=cos(angle);
r[1][2]=sin(angle);
r[2][1]=sin(angle);
break;
case 2:r[0][0]=r[2][2]=cos(angle);
r[0][2]=-sin(angle);
r[0][2]=sin(angle);
r[1][1]=1;
break;
case 3:r[0][0]=r[1][1]=cos(angle);
r[0][1]=sin(angle);
r[1][0]=sin(angle);
r[2][2]=1;
3
8
break;
}
r[3][3]=1;
mul(a,r,b);
show(b);
}
void ref(float a[2][4][4])
{
int i,j,choice;
float b[2][4][4],r[4][4];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j)
r[i][j]=1;
else
r[i][j]=0;
}
cout>>"\n\n w.r.t. which plane";
cout>>"\n\n\n\t 1.XY plane";
cout<<"\n\n\n\t 2.YZ plane";
cout<<"\n\n\n\t 3.XZ plane";
cout<<"\n\n\n enter ur choice";
cin>>choice;
{
Case 1:r[2][2]=-1;
break;
case 2:r[0][0]=-1;
break;
case 3:r[1][1]=-1;
break;
}
mul(a,r,b);
show(b);
}
void main()
{
int gd=DETECT,gm,choice;
char ans;
floatqb[2][4][4]={{{0,0,0,1},{50,0,0,1},{50,50,0,1},{0,50,0,1}},{{0,0,50,1},
{50,0,50,1},{50,50,50,1},{0,50,50,1}}
3
9
};
initgraph(&gd,&gm,"c:\\tc\\bgi");
do
{
cleardevice();
cout<<"\n\n\t Which transformation do u want";
cout<<"\n\n\t\t 1.Translation";
cout<<"\n\n\t\t 2.scaling";
cout<<"\n\n\t\t 3.rotation";
cout<<"\n\n\t\t 4.reflection";
cout<<"\n\n enter ur choice";
cin>>choice;
clrscr();
cleardevice();
line(320,240,320,0);
line(320,240,639,240);
line(320,240,90,470);
setcolor(4);
show(qp);
setcolor(14);
{
case 1:trans(qb);
break;
case 2:scale(qb);
break;
case 3:rot(qb);
break;
case 4:ref(qb);
break;
cout<<"\n do u want to continue\n";
cin>>ans;
}
while(ans=='Y'||ans=='Y');
getch();
closegraph();
}
OUTPUT
4
0
EXPERIMENT NO. 10
4
1
}
}
Void main()
{
Int I,n;
Clrscr();
Initgraph (&gd,&gm,c:\\tc\\bgi);
For (i=0; i<4; i++)
{
Cout<<\n enter (x,y) coordinates of point %d, i+1;
Cin>>%f%f, &xxx[i][0], xxx[i][1];
}
Cout<<enter the number of iterations for the curve \n;
Cin>>%d,&n;
Bezier(xxx[1][0], xxx[1][1], xxx[2][0], xxx[2][1], xxx[3][0], xxx[3][1], n);
Getch();
Closegraph();
}
OUTPUT
4
3
4
4