COMPUTER GRAPHICS LAB
PRACTICAL FILE
PRACTICAL FILE
SUBMITTED BY:-
Program 1 : A line
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void main()
{
clrscr();
int
x1,x2,y1,y2,gd=DETECT,gm;
cout<<"Input Cordinate For Point One\n";
cin>>x1>>y1;
cout<<"\nInput Cordinate For Point Two";
cin>>x2>>y2;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
getch();
closegraph();
getch();
}
Program 2 : DDA
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int
x1,x2,y1,y2,length,dx,dy,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(2);
cout<<"Input Cordinate For Point One\n";
cin>>x1>>y1;
cout<<"\nInput Cordinate For Point Two";
cin>>x2>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
length=dx;
else
length=dy;
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x1=+0.5;
y1=+0.5;
int i=0;
while(i<=length)
{
putpixel(abs(x1),abs(y1));
x1=+dy;
y1=+dy;
i++ ;
}
getch();
closegraph();
getch();
}
Program 3
: Bresenham line
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int x,y,x1,x2,y1,y2,dx,dy,p,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(3);
cout<<"Enter 1st point\n";
cin>>x1>>y1;
cout<<"Enter 2nd point\n";
cin>>x2>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
p=2*dy-dx;
int i=1;
do
{
if(p<0)
{
x=x+1;
putpixel(x,y,1);
p=p-2*dy;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,2);
p=p+2*(dy-dx);
}
i=i+1;
}
while(i<=dx);
getch();
closegraph();
getch();
}
Program 4: Draw a circle using Bresenhams Algorithm.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int r,x,y,gd=DETECT,gm;
float d;
clrscr();
cout<<"Enter the radius of a circle :";
cin>>r;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
y=r;
d=3-2*r;
do
{
putpixel(230+x,230+y,15);
putpixel(230+y,230+x,15);
putpixel(230+y,230-x,15);
putpixel(230+x,230-y,15);
putpixel(230-x,230-y,15);
putpixel(230-y,230+x,15);
putpixel(230-x,230+y,15);
putpixel(230-y,230-x,15);
if(d<=0)
{
d= d + 4*x + 6;
}
else
{
d= d
+ 4*(x-y) + 10;
y=y-1;
}
x=x+1;
} while(x<y);
getch();
closegraph();
}
Bresenhams Circle Output
Program 5: To draw a circle using midpoint method.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int r,x,y,gd=DETECT,gm;
float d;
clrscr();
cout<<"Enter the radius of a circle :";
cin>>r;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=0;
y=r;
d=1-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-x,200+y,15);
putpixel(200-y,200-x,15);
if(d<=0)
{
d= d + 2*x + 3;
}
else
{
d= d
+ 2*(x-y) + 5;
y=y-1;
}
x=x+1;
} while(x<y);
getch();
closegraph();
}
Midpoint method’s Output
Program 6: write a program to show scaling of an
object.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(45,55,90,55);
line(90,55,90,10);
line(90,10,45,10);
line(45,10,45,55);
int
a[3][4],b[3][3],x,y;
cout<<"enter scaling factor for x-coordinate: ";
cin>>x;
cout<<"enter scaling factor for y-cordinate: ";
cin>>y;
for(int
i=0;i<3;i++)
for(int
j=0;j<3;j++)
a[i][j]=1;
a[0][0]=45;
a[1][0]= 55;
a[0][1]=90;
a[1][1]= 55;
a[0][2]= 90;
a[1][2]= 10;
a[0][3]=45;
a[1][3]= 10;
int c[3][4];
for(i=0;i<3;i++)
for( j=0;j<3;j++)
b[i][j]=0;
b[0][0]=x;
b[1][1]=y;
b[2][2]=1;
for(i=0;i<3;i++)
{ for(int j=0;j<4;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]=c[i][j]+b[i][k]*a[k][j];
}
}
}
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][2],c[1][2],c[0][3],c[1][3]);
line(c[0][3],c[1][3],c[0][0],c[1][0]);
getch();
closegraph();
}
Output: Scaling
Program 7: Write a program to show translation.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(45,55,90,55);
line(90,55,90,10);
line(90,10,45,10);
line(45,10,45,55);
int
a[3][4],sc[3][3],sx,sy;
cout<<"\n\n\n\n\nenter translation for x-coordinate: ";
cin>>sx;
cout<<"enter translation factor for y-cordinate: ";
cin>>sy;
for(int
i=0;i<3;i++)
for(int
j=0;j<4;j++)
a[i][j]=1;
for(i=0;i<3;i++)
for(
j=0;j<3;j++)
sc[i][j]=0;
sc[0][0]=1;
sc[1][1]=1;
sc[2][2]=1;
sc[0][2]=sx;
sc[1][2]=sy;
a[0][0]=45;
a[1][0]= 55;
a[0][1]=90;
a[1][1]= 55;
a[0][2]= 90;
a[1][2]= 10;
a[0][3]=45;
a[1][3]= 10;
int c[3][4];
for(i=0;i<3;i++)
{ for(int j=0;j<4;j++)
{
c[i][j]=0;for(int k=0;k<3;k++)
{
c[i][j]=c[i][j]+sc[i][k]*a[k][j];
}
}
}
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][2],c[1][2],c[0][3],c[1][3]);
line(c[0][3],c[1][3],c[0][0],c[1][0]);
getch();
closegraph();
}
Output: Translation.
Program 8: WAP to Show Rotation.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{ int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(45,55,90,55);
line(90,55,90,10);
line(90,10,45,10);
line(45,10,45,55);
int
a[3][4],rt[3][3];
float p;
cout<<"\n\n\n\n\nenter anle of rotation : ";
cin>>p;
p=(3.14/180)*p;
for(int
i=0;i<3;i++)
for(int
j=0;j<4;j++)
a[i][j]=1;
for(i=0;i<3;i++)
for(
j=0;j<3;j++)
rt[i][j]=0;
rt[0][0]=cos(p);
rt[1][1]=cos(p);
rt[2][2]=1;
rt[0][1]=sin(p);
rt[1][0]=-sin(p);
a[0][0]=45;
a[1][0]= 55;
a[0][1]=90;
a[1][1]= 55;
a[0][2]=90;
a[1][2]= 10;
a[0][3]=45;
a[1][3]= 10;
int c[3][4];
for(i=0;i<3;i++)
{ for(int j=0;j<4;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]=c[i][j]+rt[i][k]*a[k][j];
} } }
line(c[0][0],c[1][0],c[0][1],c[1][1]);
line(c[0][1],c[1][1],c[0][2],c[1][2]);
line(c[0][2],c[1][2],c[0][3],c[1][3]);
line(c[0][3],c[1][3],c[0][0],c[1][0]);
getch();
closegraph();
}
Output: Rotation
For Updates Please revisit again or Bookmark us (ctrl+D)
No comments:
Post a Comment