Вы находитесь на странице: 1из 58

//WAP TO PRINT NAME "SUDHIR".

#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:

//WAP TO DRAW A CLOCK


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
cleardevice();
//code for clock
//drawing circle
circle(300,100,50);
setcolor(3);
//printing 3, 6, 9, 12 in the clock
outtextxy(293,55,"12");
outtextxy(297,135,"6");
outtextxy(255,100,"9");
outtextxy(342,100,"3");
//drwawing hour and minute hand.
line(300,100,275,85);
line(300,100,335,85);
setcolor(15);
getch();
closegraph();
}

Output:

//WAP TO DRAW A CUBE USING DDA ALGORITHM


#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int) (a+.5))
//declare and define the function for DDA line algorithm
void linedda(int xa, int ya, int xb, int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy)) steps= abs(dx);
else steps =abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(round(x),round(y),15);
for(k=0;k<steps;k++)
{x+=xincrement;
y+=yincrement;
putpixel(round(x),round(y),15);}
}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
//printing a cube by calling linedda function
linedda(10,10,10,110);
linedda(10,110,110,110);
linedda(110,110,110,10);
linedda(110,10,10,10);
linedda(30,30,30,130);
linedda(30,130,130,130);
linedda(130,130,130,30);
linedda(130,30,30,30);
linedda(10,10,30,30);
linedda(10,110,30,130);
linedda(110,110,130,130);
linedda(110,10,130,30);
getch();
closegraph();
}

Output:

//PROGRAM TO DRAW LINE USING BRESHEM'S LINE ALGORITHAM WITH USER


INPUTS.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
//definition of breshem's line algoritham
void linebres(int xa, int ya, int xb, int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,15);
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p=+twodydx;
}
putpixel(x,y,15);
}
}

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

//WAP TO DRAW A LINE USING DDA ALGORITHM


#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int) (a+.5))
//DDA Line Algoritham
void linedda(int xa, int ya, int xb, int yb)
{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy)) steps= dx;
else steps =dy;
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(round(x),round(y),15);
for(k=0;k<steps;k++)
{x+=xincrement;
y+=yincrement;
putpixel(round(x),round(y),15);}
}
void main()
{
int gdriver=DETECT,gmode,a,b,c,d;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
cleardevice();
//user inputs
printf("enter co-ordinates of a line");
scanf("%d%d%d%d",&a,&b,&c,&d);
linedda(a,b,c,d);
//drawing line by calling DDA line algoritham
getch();
closegraph();
}

Output:
enter coordinates of the line
100
100
200
100

//WAP TO PRINT A TRIANGLE USING BRESHEMN'S LINE ALGORITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void linebres(int xa, int ya, int xb, int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
printf("%d\t%d\t",dx,dy);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,15);
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p=+twodydx;
}
putpixel(x,y,15);
}
}
//making a triangle by calling linebres function
void tria()
{
linebres(200,300,300,300);
linebres(100,200,200,300);
linebres(100,200,300,300);
}
void main()
{
int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"c:\tc\bgi");
tria();
getch();
closegraph();
}

Output:

//WAP TO DRAW A HOUSE.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//drawing a house
void house()
{
line(200,140,250,75);
line(250,75,300,140);
line(250,75,450,75);
line(450,75,500,140);
line(500,140,300,140);
line(205,135,205,340);
line(295,135,295,370);
line(295,370,205,340);
line(200,140,205,140);
line(295,370,480,370);
line(480,370,480,140);}
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
cleardevice();
house();
//passing control to house function
getch();
closegraph();
}
OUTPUT:

//BAR GRAPH USING INBUILD line function.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int nos[5],pass[5],per[5];
int pers(int a, int b) //calculating percentage
{
int per=0;
per=(b*100)/a;
return per;
}
void getdata(int i)
//inputing no of students appeared and passed
{
switch(i){
case 1:
printf("enter no of students appeared and passed in CG");
scanf("%d%d",&nos[i],&pass[i]);
break;
case 2:
printf("enter no of students appeared and passed in OS");
scanf("%d%d",&nos[i],&pass[i]);
break;
case 3:
printf("enter no of students appeared and passed in SE");
scanf("%d%d",&nos[i],&pass[i]);
break;
case 4:
printf("enter no of students appeared and passed in NW");
scanf("%d%d",&nos[i],&pass[i]);
break;
default:
break;}
}

void barr(int per,int i) //Printing bar of the particular subject


{
int static x =40;
line(x,450-per,x+20,450-per);
line(x,450-per,x,450);
line(x+20,450-per,x+20,450);
switch(i)
{
case 1:
outtextxy(x+3,455,"CG");
break;
case 2:
outtextxy(x+3,455,"OS");
break;
case 3:
outtextxy(x+3,455,"SE");
break;
case 4:
outtextxy(x+3,455,"NW");
break;
default:
break;
}
x=x+20;
}
void main()
{
int i;
int gd= DETECT,gm;
initgraph(&gd,&gm,"");
for(i=1;i<5;i++)
getdata(i);
//inputing data of each subject
for (i=1;i<5;i++)
{per[i]=pers(nos[i],pass[i]);} //calculating percentage of each subject
line(20,450,600,450);
line(40,150,40,470);
for(i=1;i<5;i++)
barr(per[i],i);
//calling bar for each subject
getch();
closegraph();
}

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

//PRGROMA TO PRINT CONCENTRIC CIRCLES USING MID POINT CIRCLE


ALGORITHAM..
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//definition of mid point circle algoritham
void cirmidpoint(int xcenter,int ycenter,int radius)
{
int x=0;
int y = radius;
int p =1-radius;
void cirplotpoints(int,int,int,int);
cirplotpoints(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if (p<0)
p+=2*x+1;
else{
y--;
p+=2*(x-y)+1;
}
cirplotpoints(xcenter,ycenter,x,y);
}}
//glowing pixel using circle 8 way symmetry
void cirplotpoints(int xcenter,int ycenter, int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);
putpixel(xcenter-x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter-x,ycenter-y,15);
putpixel(xcenter+y,ycenter+x,15);
putpixel(xcenter-y,ycenter+x,15);
putpixel(xcenter+y,ycenter-x,15);
putpixel(xcenter-y,ycenter-x,15);
}
void main()
{
int gd=DETECT,gm,x,y,r;
initgraph(&gd,&gm,"");
//drawing concetric circles using mid point circle algoritham
cirmidpoint(150,100,50);
cirmidpoint(150,100,60);
getch();
closegraph();
}

Output:

//PROGRAM TO DRAW A SEMI-CIRCLE USING MID POINT ALGORITHM.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//defintion of MID POINT Circle alogritham
void circ(int xcenter,int ycenter,int radius)
{
int x =0;
int y = radius;
int p=1-radius;
void circplot(int,int,int,int);
circplot(xcenter,ycenter,x,y);
while(x<y){
x++;
if(p<0) p+=2*x+1;
else
{y--; p+=2*(x-y)+1;}
circplot(xcenter,ycenter,x,y);}
}
//glowing pixels of the semi-circle with the symmtry of circle at 45 degree
void circplot(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter+y,ycenter+x,15);
putpixel(xcenter+y,ycenter-x,15);
}
void main()
{
int x,y,r,gd=DETECT,gm;
initgraph(&gd,&gm,"");
circ(150,100,50);
//calling circ(mid point circle algorithm) function
getch();
}

Output:

//PRGROMA TO PRINT CIRCLE USING MID POINT CIRCLE ALGORITHAM WITH


USER INPUTS.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//definition of mid point circle algoritham
void cirmidpoint(int xcenter,int ycenter,int radius)
{
int x=0;
int y = radius;
int p =1-radius;
void cirplotpoints(int,int,int,int);
cirplotpoints(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if (p<0)
p+=2*x+1;
else{
y--;
p+=2*(x-y)+1;
}
cirplotpoints(xcenter,ycenter,x,y);
}}
//glowing pixel using circle 8 way symmetry
void cirplotpoints(int xcenter,int ycenter, int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);
putpixel(xcenter-x,ycenter+y,15);
putpixel(xcenter+x,ycenter-y,15);
putpixel(xcenter-x,ycenter-y,15);
putpixel(xcenter+y,ycenter+x,15);
putpixel(xcenter-y,ycenter+x,15);
putpixel(xcenter+y,ycenter-x,15);
putpixel(xcenter-y,ycenter-x,15);
}

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

//DRAW VARIOUS SHAPES USING SWITCH CASE AND INBUILT FUNCTIONS


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
//drawing circle
void circ(){circle(200,200,60);}
//drawing square
void squa()
{
line(200,200,200,350);
line(200,350,350,350);
line(350,350,350,200);
line(350,200,200,200);
}
//drawing reactangle
void reac()
{
line(200,200,200,350);
line(200,350,400,350);
line(400,350,400,200);
line(400,200,200,200);
}
//drawing triangle
void tria()
{
line(200,200,300,200);
line(300,200,250,125);
line(250,125,200,200);
}
//drawing triangle inside a square
void tis()
{
line(200,300,300,300);
line(300,300,250,225);
line(250,225,200,300);
line(190,210,310,210);
line(310,210,310,330);
line(310,330,190,330);
line(190,330,190,210);
}

//drawing triangle inside a triangle


void tit()
{
line(200,300,300,300);
line(300,300,250,225);
line(250,225,200,300);
line(190,310,310,310);
line(310,310,250,215);
line(250,215,190,310);
}
//drawing a cube.
void cube()
{
line(200,200,200,300);
line(200,300,300,300);
line(300,300,300,200);
line(300,200,200,200);
line(230,230,230,330);
line(230,330,330,330);
line(330,330,330,230);
line(330,230,230,230);
line(200,200,230,230);
line(330,330,300,300);
line(200,300,230,330);
line(300,200,330,230);
}
//drawing two concentric circles
void cic()
{
circle(300,350,60);
circle(300,350,70);
}
//drawing triangle inside a circle
void tic()
{
line(200,300,300,300);
line(300,300,250,225);
line(250,225,200,300);
circle(250,270,65);
}

//choose which you want to print


void choice(int x)
{
switch (x)
{
case 1: circ(); break;
case 2: squa(); break;
case 3: reac(); break;
case 4: tria(); break;
case 5: cic(); break;
case 6: tic(); break;
case 7: cube(); break;
case 8: tis(); break;
case 9: tit(); break;
default:
printf("not a valid choice"); break;
}
}
void main()
{
int gdriver=DETECT,gmode,x;
char c='y';
initgraph(&gdriver,&gmode,"c:\tc\bgi");
do
{
cleardevice();
printf("Enter your choice");
printf("\nPress 1 for circle");
printf("\n\t2 for square");
printf("\n\t3 for reactangle");
printf("\n\t4 for triangle");
printf("\n\t5 for circle inside a circle");
printf("\n\t6 for triangle inside a circle");
printf("\n\t7 for cube");
printf("\n\t8 for triangle inside square");
printf("\n\t9 for triangle inside a triangle");
scanf("%d",&x);
choice(x);
printf("\n\t do you want to print any different image");
scanf("%c",&c);
}
while((c=='y')||(c=='Y'));
getch();
closegraph();
}

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

do you want to print any different imagey


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 triangle2

do you want to print any different imagey

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 triangle3

do you want to print any different imagey

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 triangle4

do you want to print any different imagey

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 triangle5

do you want to print any different imagey

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 triangle6

do you want to print any different imagey

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 triangle7

do you want to print any different imagey

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 triangle8

do you want to print any different imagey

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 triangle9

do you want to print any different imagey

//WAP TO PERFORM TRANSFORMATION ON A TRIANGLE


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
//DECLARATION OF FUNCTIONS
void scale(int a[][3]);
void xreflect(int a[][3]);
void yreflect(int a[][3]);
void rot(int a[][3]);
void translate(int a[][3]);
void shear(int a[][3]);
void show(int a[][3]);
void showf(float a[][3]);
void main()
{
int x;
int a[3][3]={100,100,1,200,200,1,100,200,1};
//ASKING FOR CHOICE OF THE USER.
printf("Enter your choice:");
printf("\n\t Press 0 for original image");
printf("\n\t Press 1 for scaling ");
printf("\n\t Press 2 for reflecting along x axis");
printf("\n\t Press 3 for reflecting along y axis");
printf("\n\t Press 4 for rotating");
printf("\n\t Press 5 for translation");
printf("\n\t Press 6 for shearing");
scanf("%d",&x);
//calling function for transformation
switch(x)
{
case 0:
show(a);
case 1:
scale(a);
break;
case 2:
xreflect(a);
break;
case 3:
yreflect(a);
break;
case 4:
rot(a);
break;
case 5:
translate(a);
break;
case 6:
shear(a);
break;
default:

printf("Please enter a valid choice");


}
getch();
}
//transting the triangle to a point
void translate(int a[][3])
{
int r[3][3]={0},b[3][3]={1,0,0,0,1,0,150,150,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);
}
//reflection around x axis
void xreflect(int a[][3])
{
int r[3][3]={0},b[3][3]={1,0,0,0,-1,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];
//moving the triangle to the visible region
r[0][1]+=300;
r[1][1]+=300;
r[2][1]+=300;
setcolor(4);
show(r);
}
//reflection around y axis
void yreflect(int a[][3])
{
int r[3][3]={0},b[3][3]={-1,0,0,0,1,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];
//moving the triangle to the visible region
r[0][0]+=200;
r[1][0]+=200;
r[2][0]+=200;
setcolor(4);
show(r);
}
//performing shearing
void shear(int a[][3])
{
int r[3][3]={0},b[3][3]={1,3,0,0,1,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];
//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

//WAP TO PERFORM TRANSFORMATION ON A CUBE


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
//DECLARATION OF FUNCTIONS
void scale(int a[][3]);
void xreflect(int a[][3]);
void yreflect(int a[][3]);
void rot(int a[][3]);
void translate(int a[][3]);
void shear(int a[][3]);
void show(int a[][3]);
void showf(float a[][3]);
void main()
{
int x;
int a[8][3]={100,100,1,100,200,1,200,200,1,200,100,1,130,130,1,130,230,1,230,230,1,230,130,1};
clrscr();
//ASKING FOR CHOICE OF THE USER.
printf("Enter your choice:");
printf("\n\t Press 0 for normal image");
printf("\n\t Press 1 for scaling ");
printf("\n\t Press 2 for reflecting along x axis");
printf("\n\t Press 3 for reflecting along y axis");
printf("\n\t Press 4 for rotating");
printf("\n\t Press 5 for translation");
printf("\n\t Press 6 for shearing");
scanf("%d",&x);
rot(a);
//calling the function for transformation
switch(x)
{
case 0:
show(a);
break;
case 1:
scale(a);
break;
case 2:
xreflect(a);
break;
case 3:
yreflect(a);
break;
case 4:
rot(a);
break;
case 5:
translate(a);
break;

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

//WAP TO ROTATE A HOUSE WITH AN ANGLE OF 60 DEGREE.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define z 60
void showdata(float r[][3]);
//rotating the house
//after rotating the house is shifting in the visible region
void rot(float a[][3])
{int i,j,k;
float r[8][3]={0},b[3][3];
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;
//performing transformation (ROTATION)
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];
//moving the house to the visible region
r[0][0]+=200;
r[1][0]+=200;
r[2][0]+=200;
r[3][0]+=200;
r[4][0]+=200;
r[5][0]+=200;
r[6][0]+=200;
r[7][0]+=200;
r[0][1]-=300;
r[1][1]-=300;
r[2][1]-=300;
r[3][1]-=300;
r[4][1]-=300;
r[5][1]-=300;
r[6][1]-=300;
r[7][1]-=300;
setcolor(4);
showdata(r);
}
//showing the house
void showdata(float r[8][3])
{
line(r[0][0],r[0][1],r[1][0],r[1][1]);
line(r[0][0],r[0][1],r[2][0],r[2][1]);
line(r[1][0],r[1][1],r[2][0],r[2][1]);

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:

//PROGRAM TO MOVING LINE IN DOWNWARD DIRECTION USING BRESHEM'S LINE


ALGORITHAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
//definition of breshem's line algoritham
void linebres(int xa, int ya, int xb, int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy,twodydx=2*(dy-dx);
int x,y,xend;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,15);
while(x<xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p=+twodydx;
}
putpixel(x,y,15);
}
}
//statrting of main
void main()
{
int i,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);
//MOVING THE LINE

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

//WAP TO A LINE IN UPWARD DIRECTION USING DDA ALGORITHM


#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int) (a+.5))
//DDA Line Algoritham
void linedda(int xa, int ya, int xb, int yb)
{int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if(abs(dx)>abs(dy)) steps= dx;
else steps =dy;
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(round(x),round(y),15);
for(k=0;k<steps;k++)
{x+=xincrement;
y+=yincrement;
putpixel(round(x),round(y),15);}
}
void main()
{
int gdriver=DETECT,gmode,a,b,c,d,i;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
cleardevice();
//user inputs
printf("enter co-ordinates of a line");
scanf("%d%d%d%d",&a,&b,&c,&d);
//drawing line by calling DDA line algoritham
linedda(a,b,c,d);
//MOVING THE LINE IN UPWARD DIRECTION
for(i=30;i>0;i--)
{
cleardevice();
linedda(a,b+i,c,d+i);
delay(30);
}
getch();
closegraph();
}

Output:
enter co-ordinates of a line
100
100
200
200

//WAP TO PRINT AN ELLIPSE USING MID POINT ALOGRITHM


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define round(a) ((int) (a+.05))
//PROCEDURE FOR GENERATING ELLIPSE USING MID POINT ALOGRITHM
void ellipsemid( int xcenter, int ycenter, int rx, int ry)
{
int rx2=rx*rx;
int ry2=ry*ry;
int tworx2=2*rx2;
int twory2=2*ry2;
int p;
int x=0;
int y=ry;
int px=0;
int py=tworx2*y;
void ellipseplot(int,int,int,int);
ellipseplot(xcenter,ycenter,x,y);
p=round(ry2-(rx2*ry2)+(.25*rx2));
while(px<py)
{
x++;
px+=twory2;
if(p<0)
px+=ry2+px;
else{
y--;
py-=tworx2;
p+=ry2+px-py;}
ellipseplot(xcenter,ycenter,x,y);
}
p=round(ry2*(x+.5)*(x+.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
py-=tworx2;
if(p<0)
p+=rx2-py;
else
{
x++;
px+=twory2;
px+=twory2;
p+=rx2-py+px;
}
ellipseplot(xcenter,ycenter,x,y);
}}
//PRINTING ELLIPSE
void ellipseplot(int xcenter, int ycenter, int x,int y)
{
putpixel(xcenter+x,ycenter+y,15);

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:

Вам также может понравиться