Computer Graphics Practical File
Computer Graphics Practical File
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void name()
//drawing name
{
arc(100,100,0,270,35);
arc(100,170,180,90,35);
arc(185,160,180,0,40);
line(145,160,145,100);
line(225,160,225,100);
arc(260,150,245,110,50);
line(260,100,260,200);
line(325,100,325,200);
line(375,100,375,200);
line(325,150,375,150);
line(385,100,385,200);
line(405,100,405,200);
line(405,160,440,200);
arc(405,130,270,110,30);
arc(400,110,90,270,10);
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\tc\bgi");
cleardevice();
name();
getch();
closegraph();
}
Output:
Output:
Output:
void main()
{
int a,b,c,d,gdriver=DETECT,gmode;
//calling graphics file in the memory
initgraph(&gdriver,&gmode,"c:\tc\bgi");
//taking input of coordinates of the line
printf("enter coordinates of the line");
scanf("%d%d%d%d",&a,&b,&c,&d);
//calling linebres function by passing values of the coordinates
linebres(a,b,c,d);
getch();
closegraph();
}
Output:
enter coordinates of the line
100
100
200
100
Output:
enter coordinates of the line
100
100
200
100
initgraph(&gdriver,&gmode,"c:\tc\bgi");
tria();
getch();
closegraph();
}
Output:
Output:
enter no of students appeared and passed in CG
100
80
enter no of students appeared and passed in OS
100
76
enter no of students appeared and passed in SE
100
70
enter no of students appeared and passed in NW
100
84
Output:
Output:
void main()
{
int gd=DETECT,gm,x,y,r;
initgraph(&gd,&gm,"");
//taking user inputs
printf("enter center coordinates of circle and it's radius");
scanf("%d%d%d",&x,&y,&r);
//passing control to mid point circle algoritham
cirmidpoint(x,y,r);
getch();
closegraph();
}
Output:
enter center coordinates of circle and it's radius
150
100
50
Output:
Enter your choice
Press 1 for circle
2 for square
3 for reactangle
4 for triangle
5 for circle inside a circle
6 for triangle inside a circle
7 for cube
8 for triangle inside square
9 for triangle inside a triangle1
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
//moving the triangle to the visible region
r[0][1]-=400;
r[1][1]-=400;
r[2][1]-=400;
setcolor(4);
show(r);
}
//rotating the triangle
void rot(int a[][3])
{int i,j,k;
float z,r[3][3]={0},b[3][3];
printf("enter an angle");
scanf("%f",&z);
b[0][0]=cos(z*3.1415/180);
b[0][1]=sin(z*3.1415/180);
b[0][2]=0;
b[1][0]=-cos(z*3.1415/180);
b[1][1]=sin(z*3.1415/180);
b[1][2]=0;
b[2][0]=0;
b[2][1]=0;
b[2][2]=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
setcolor(4);
showf(r);
}
//scaling the triangle
void scale(int a[][3])
{
int r[3][3]={0},b[3][3]={2,0,0,0,2,0,0,0,1};
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
setcolor(4);
show(r);
}
//showing the triangle
void show(int a[][3])
{
int i,j,gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[0][0],a[0][1],a[2][0],a[2][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
setcolor(15);
}
//showing the triangle after rotation
void showf(float a[][3])
{
int i,j,gd=DETECT,gm;
initgraph(&gd,&gm,"");
//moving the triangle to the visible region
a[0][0]+=300;
a[1][0]+=300;
a[2][0]+=300;
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[0][0],a[0][1],a[2][0],a[2][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
setcolor(15);
}
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 0
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 1
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 2
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 3
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 4
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 5
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 6
case 6:
shear(a);
break;
default:
printf("Please enter a valid choice");
}
getch();
}
//translating the cube from one point to another
void translate(int a[][3])
{
int r[8][3]={0},b[3][3]={1,0,0,0,1,0,150,150,1};
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
setcolor(4);
show(r);
}
//reflecting around x axis
void xreflect(int a[][3])
{
int r[8][3]={0},b[3][3]={1,0,0,0,-1,0,0,0,1};
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
//translating to the visible region
r[0][1]+=500;
r[1][1]+=500;
r[2][1]+=500;
r[3][1]+=500;
r[4][1]+=500;
r[5][1]+=500;
r[6][1]+=500;
r[7][1]+=500;
setcolor(4);
show(r);
}
//reflecting around y axis
void yreflect(int a[][3])
{
int r[8][3]={0},b[3][3]={-1,0,0,0,1,0,0,0,1};
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
//translating to the visible region
r[0][0]+=600;
r[1][0]+=600;
r[2][0]+=600;
r[3][0]+=600;
r[4][0]+=600;
r[5][0]+=600;
r[6][0]+=600;
r[7][0]+=600;
setcolor(4);
show(r);
}
//performing shearing
void shear(int a[][3])
{
int r[8][3]={0},b[3][3]={1,3,0,0,1,0,0,0,1};
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
//translating to the visible region
r[0][1]-=400;
r[1][1]-=400;
r[2][1]-=400;
r[3][1]-=400;
r[4][1]-=400;
r[5][1]-=400;
r[6][1]-=400;
r[7][1]-=400;
setcolor(4);
show(r);
}
//rotating the cube
void rot(int a[][3])
{int i,j,k;
float z,r[8][3]={0},b[3][3];
printf("enter an angle");
scanf("%f",&z);
b[0][0]=cos(z*3.1415/180);
b[0][1]=sin(z*3.1415/180);
b[0][2]=0;
b[1][0]=-cos(z*3.1415/180);
b[1][1]=sin(z*3.1415/180);
b[1][2]=0;
b[2][0]=0;
b[2][1]=0;
b[2][2]=1;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
//translating to the visible region
r[0][0]+=300;
r[1][0]+=300;
r[2][0]+=300;
r[3][0]+=300;
r[4][0]+=300;
r[5][0]+=300;
r[6][0]+=300;
r[7][0]+=300;
setcolor(4);
showf(r);
}
//scaling the cube
void scale(int a[][3])
{
int r[8][3]={0},b[3][3]={2,0,0,0,2,0,0,0,1};
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
r[i][j]+=a[i][k]*b[k][j];
setcolor(4);
show(r);
}
//showing the cube
void show(int a[][3])
{
int i,j,gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[3][0],a[3][1]);
line(a[3][0],a[3][1],a[0][0],a[0][1]);
line(a[4][0],a[4][1],a[5][0],a[5][1]);
line(a[5][0],a[5][1],a[6][0],a[6][1]);
line(a[6][0],a[6][1],a[7][0],a[7][1]);
line(a[7][0],a[7][1],a[4][0],a[4][1]);
line(a[0][0],a[0][1],a[4][0],a[4][1]);
line(a[1][0],a[1][1],a[5][0],a[5][1]);
line(a[2][0],a[2][1],a[6][0],a[6][1]);
line(a[3][0],a[3][1],a[7][0],a[7][1]);
setcolor(15);
}
//showing the cube after rotation
void showf(float a[][3])
{
int i,j,gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(a[0][0],a[0][1],a[1][0],a[1][1]);
line(a[1][0],a[1][1],a[2][0],a[2][1]);
line(a[2][0],a[2][1],a[3][0],a[3][1]);
line(a[3][0],a[3][1],a[0][0],a[0][1]);
line(a[4][0],a[4][1],a[5][0],a[5][1]);
line(a[5][0],a[5][1],a[6][0],a[6][1]);
line(a[6][0],a[6][1],a[7][0],a[7][1]);
line(a[7][0],a[7][1],a[4][0],a[4][1]);
line(a[0][0],a[0][1],a[4][0],a[4][1]);
line(a[1][0],a[1][1],a[5][0],a[5][1]);
line(a[2][0],a[2][1],a[6][0],a[6][1]);
line(a[3][0],a[3][1],a[7][0],a[7][1]);
setcolor(15);
}
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 0
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 1
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 2
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 3
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 4
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 5
Output:
Enter your choice:
Press 0 for original image
Press 1 for scaling
Press 2 for reflecting along x axis
Press 3 for reflecting along y axis
Press 4 for rotating
Press 5 for translation
Press 6 for shearing 6
line(r[1][0],r[1][1],r[3][0],r[3][1]);
line(r[2][0],r[2][1],r[4][0],r[4][1]);
line(r[3][0],r[3][1],r[4][0],r[4][1]);
line(r[4][0],r[4][1],r[5][0],r[5][1]);
line(r[0][0],r[0][1],r[6][0],r[6][1]);
line(r[2][0],r[2][1],r[7][0],r[7][1]);
line(r[6][0],r[6][1],r[7][0],r[7][1]);
line(r[5][0],r[5][1],r[7][0],r[7][1]);
}
void main()
{
//declaring and defining the coordinates of house
float x[8][3]={150,100,1,100,150,1,200,150,1,100,400,1,200,400,1,550,400,1,450,100,1,550,150,1};
int gd=DETECT,gm;
//initialsing graphics drivers
initgraph(&gd,&gm,"");
//showing the house before rotation
showdata(x);
//rotating the house
rot(x);
getch();
}
Output:
for(i=0;i<30;i++)
{
cleardevice();
linebres(a,b++,c,d++);
delay(50);
}
getch();
closegraph();
}
Output:
enter coordinates of the line 100
100
200
200
Output:
enter co-ordinates of a line
100
100
200
200
putpixel(xcenter-x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter-x,ycenter-y,15);}
void main()
{
int gd=DETECT,gm;
//INITIALISING OF GRAPHICS DRIVERS
initgraph(&gd,&gm,"");
ellipsemid(100,150,50,75);
getch();
}
Output: