graphics_programming
graphics_programming
GRAPHICS PROGRAM
#include"graphics.h"
#include"conio.h"
void main()
int gd=DETECT,gm;
circle(330,180,100);
getch();
closegraph();
restorecrtmode();
Code explanation
The first line to look at is: GRAPHICS.H ,this file contains definitions and
explaination of all the graphic functions and constants.While GRAPHICS.LIB file
contains standard graphic functions.
Turbo C++ graphic functions have two categaries :Text mode graphic functions and
graphic mode functions.Here we are dealing with graphic mode function.so just
forget about text mode function right now.To switch from text mode to graphic
mode,we have function called as ” initgraph ” .
initgraph : This function initialises the graphic mode.It selects the best
resolution and direct that value to mode in variable gm.The two int variables
gd, gm are graphic driver and graphic mode respectively.The gm handles value that
tells us which resolution and monitor we are using. The gd specifies the graphic
driver to be used.In our program we have gd=DETECT means we have passed the
highest possible value available for the detected driver.If you don’t want that
value then you have to assign the constant value for gd,gm.The ” &” symbol is
used for initgraph to pass address of the constants.
Circle( ) : Circle function takes X and Y values with respect to top left corner
of the screen and third co-ordinate is nothing but radius of circle.In our example
we have passed X=330,Y=180 and radius equal to 100 in terms of pixels as
arguments.
2
Closegraph( ) : The closegraph() swithces back the screen from grpahics mode to
text mode. If you don’t use this function then you may have undesirable
effects.Here this function is called afer the getch() function as screen shouldn’t
switch to text mode till user hits any key.
Restorcrtmode( ) : This mode will restore the original video mode detected by
initgraph function.
getch( ) : getch( ) function gets a character from console but does not echo it
on screen.This is used to pause the screen till user hits any key.
Note:
1) Make sure you have entered the correct path for the include & library
directories.You can change the path by pointing your mouse to : Options >
Directories.Enter the valid path for the include directory and libraries,and
output directories.
4) If you want help on specific function then point your mouse to “Help>
Contents“,and then browse the content for the function you want.If you want fast-
help then put the cursor on the first letter of the function or term and press
CTRL+F1,it will point you to the help file of that term/function.
Function Documentation
This procedure draws a circular arc, which always begins at the current point.
The arc itself has radius r, and starts at the angle specified by the parameter
start, relative to the center of the circle. This angle is measured in degrees
counterclockwise from the 3 o'clock position along the x-axis, as in
traditional mathematics. For example, if start is 0, the arc begins at the 3
o'clock position; if start is 90, the arc begins at the 12 o'clock position;
and so on. The fraction of the circle drawn is specified by the parameter
sweep, which is also measured in degrees. If sweep is 360, DrawArc draws a
complete circle; if sweep is 90, it draws a quarter of a circle. If the value
of sweep is positive, the arc is drawn counterclockwise from the current point.
If sweep is negative, the arc is drawn clockwise from the current point. The
current point at the end of the DrawArc operation is the final position of the
pen along the arc.
Examples:
3
DrawArc(r, 0, 360) Draws a circle to the left of the current point.
DrawArc(r, 90, 180) Draws the left half of a semicircle starting from the
12 o'clock position.
DrawArc(r, 0, 90) Draws a quarter circle from the 3 o'clock to the 12
o'clock position.
DrawArc(r, 0, -90) Draws a quarter circle from the 3 o'clock to the 6
o'clock position.
DrawArc(r, -90, -90) Draws a quarter circle from the 6 o'clock to the 9
o'clock position.
dx
void DrawLine ( double
,
double dy
)
This procedure draws a line extending from the current point by moving the pen
dx inches in the x direction and dy inches in the y direction. The final
position becomes the new current point.
double GetCurrentX ( )
double GetCurrentY ( )
double GetWindowHeight ( )
double GetWindowWidth ( )
void InitGraphics ( )
This procedure creates the graphics window on the screen. The call to
InitGraphics must precede any calls to other functions in this package and must
also precede any console output. In most cases, the InitGraphics call is the
first statement in the function main.
x
void MovePen ( double
,
double y
)
This procedure moves the current point to the position (x, y), without drawing
a line. The model is that of the pen being lifted off the graphics window
surface and then moved to its new position.
/* Transformation */
4
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
void main()
{
int i;
float x,y,x0,x1,y0,y1,tx,ty;
char msg[80];
int gd=DETECT,gm;
clrscr();
printf("Enter the value of x0 : ");
scanf("%f",&x0);
printf("Enter the value of y0 : ");
scanf("%f",&y0);
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of translation along x axis : ");
scanf("%f",&tx);
printf("Enter the value of translation along y axis : ");
scanf("%f",&ty);
x0=x0+300;
y0=-y0+250;
x1=x1+300;
y1=-y1+250;
initgraph( &gd, &gm, "");
setbkcolor(RED);
line(300,0,300,600);
line(0,250,800,250);
sprintf(msg, "(%d,%d)",(int)(x0-300),(int)(-y0+250));
outtextxy(x0, y0, msg);
line(x0,y0,x1,y1);
sprintf(msg, "(%d,%d)",(int)(x1-300),(int)(-y1+250));
outtextxy(x1, y1, msg);
x0=x0+tx;
y0=y0-ty;
x1=x1+tx;
y1=y1-ty;
setcolor(YELLOW);
sprintf(msg, "(%d,%d)",(int)(x0-300),(int)(-y0+250));
outtextxy(x0, y0, msg);
line(x0,y0,x1,y1);
sprintf(msg, "(%d,%d)",(int)(x1-300),(int)(-y1+250));
outtextxy(x1, y1, msg);
getch();
closegraph();
restorecrtmode();
/* Shearing */
5
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#define pi 3.1412
int x0,x1,y0,y1;
void main()
{
float x0,y0,x1,y1,x2,y2,x3,y3,b,c;
int gd=DETECT,gm;
clrscr();
printf("\nEnter the value of x0 : ");
scanf("%f",&x0);
printf("Enter the value of y0 : ");
scanf("%f",&y0);
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y2 : ");
scanf("%f",&y2);
printf("Enter the value of x3 : ");
scanf("%f",&x3);
printf("Enter the value of y3 : ");
scanf("%f",&y3);
printf("\nEnter the shear along x axis : ");
scanf("%f",&b);
printf("\nEnter the shear along y axis : ");
scanf("%f",&c);
x0=x0+300;
y0=-y0+250;
x1=x1+300;
y1=-y1+250;
x2=x2+300;
y2=-y2+250;
x3=x3+300;
y3=-y3+250;
initgraph(&gd, &gm, "");
setbkcolor(RED);
line(300,0,300,600);
line(0,250,800,250);
line(x0,y0,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x0,y0,x3,y3);
if(c==0)
{
x1 = x1 + b * y1;
y1 = -c * x1 + y1;
x2 = x2 + b * y2;
y2 = -c * x2 + y2;
}
if(b==0)
{
x2 = x2 + b * y2;
y2 = -c * x2 + y2;
x3 = x3 + b * y3;
y3 = -c * x3 + y3;
}
if((b!=0)&&(c!=0))
6
{
x1 = x1 + b * y1;
y1 = -c * x1 + y1;
x2 = x2 + b * y2;
y2 = -c * x2 + y2;
x3 = x3 + b * y3;
y3 = -c * x3 + y3;
}
setcolor(BLUE);
line(x0,y0,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x0,y0,x3,y3);
getch();
closegraph();
restorecrtmode();
}
/* ROTATION OF A LINE */
7
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#define pi 3.1412
int x0,x1,y0,y1;
void main()
{
double a,A;
char msg[80];
int gd=DETECT,gm;
clrscr();
printf("\nEnter the value of x0 : ");
scanf("%d",&x0);
printf("Enter the value of y0 : ");
scanf("%d",&y0);
printf("Enter the value of x1 : ");
scanf("%d",&x1);
printf("Enter the value of y1 : ");
scanf("%d",&y1);
printf("Enter the angle of rotation : ");
scanf("%lf",&a);
initgraph(&gd, &gm, "");
setbkcolor(BLUE);
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
line(x0,y0,x1,y1);
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
A=(a*pi)/180.0;
x1=(double)x1*cos(A)-(double)y1*sin(A);
y1=(double)x1*sin(A)+(double)y1*cos(A);
x1=(int)x1;
y1=(int)y1;
setcolor(RED);
line(x0,y0,x1,y1);
sprintf(msg, "(%d,%d)",x1,y1);
outtextxy(x1, y1, msg);
getch();
closegraph();
restorecrtmode();
}
void main()
{
int i;
float x,y,x0,x1,y0,y1,len,dx,dy;
int gd=DETECT,gm;
char msg[80];
clrscr();
printf("Enter the value of x0 : ");
scanf("%f",&x0);
printf("Enter the value of y0 : ");
scanf("%f",&y0);
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
initgraph( &gd, &gm, "");
setbkcolor(RED);
if(fabs(x1-x0)>fabs(y1-y0))
{
len = fabs(x1-x0);
}
else
{
len = fabs(y1-y0);
}
dx = (x1-x0)/len;
dy = (y1-y0)/len;
x = x0;
y = y0;
x = floor(x + 0.5);
y = floor(y + 0.5);
putpixel(x,y,6);
sprintf(msg, "(%d,%d)",(int)x,(int)y);
outtextxy(x, y, msg);
i = 1;
while(i<=len)
{
x = x + dx;
y = y + dy;
putpixel(x,y,6);
i++;
}
sprintf(msg, "(%d,%d)",(int)x,(int)y);
outtextxy(x, y, msg);
getch();
closegraph();
restorecrtmode();
}
void main()
{
float x1,y1,x2,y2,x,y,x3,y3,x0,y0,m;
char msg[80];
int gd=DETECT,gm;
clrscr();
printf("Enter the value of x1 : ");
scanf("%f",&x0);
printf("Enter the value of y1 : ");
scanf("%f",&y0);
printf("Enter the value of x2 : ");
scanf("%f",&x1);
printf("Enter the value of y2 : ");
scanf("%f",&y1);
if((x0>x1)&&(y0>y1))
{
x=x0;
x0=x1;
x1=x;
y=y0;
y0=y1;
y1=y;
}
m=(y1-y0)/(x1-x0);
if(m<0)
exit(1);
initgraph(&gd, &gm, "");
setbkcolor(RED);
line(175,0,175,680);
line(450,0,450,680);
line(0,150,800,150);
line(0,350,800,350);
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
x1=(int)x1;
y1=(int)y1;
line(x0,y0,x1,y1);
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
if(((x0<175)&&(x1<175))||((x0>450)&&(x1>450))||((y0<150)&&(y1<350))||((y0>150)&
&(y1>350)))
line(x0,y0,x1,y1);
else if((x0<=175)&&(x1>=450))//||||||((x1>=450)&&(y0<=150))))
{
x2=175;
x3=450;
y2=m*(175-x0)+y0;
y3=m*(450-x0)+y0;
if((x2>x3)&&(y2>y3))
{
x=x2;
x2=x3;
x3=x;
y=y2;
y2=y3;
10
y3=y;
}
setcolor(BLUE);
line(x2,y2,x3,y3);
}
else if((y0<=150)&&(y1>=350))
{
if(m==0)
{
y2=150;
y3=350;
x2=x0;
x3=x1;
}
else
{
y2=150;
y3=350;
x2=1/m*(150-y0)+x0;
x3=1/m*(350-y1)+x1;
}
if((x2>x3)&&(y2>y3))
{
x=x2;
x2=x3;
x3=x;
y=y2;
y2=y3;
y3=y;
}
setcolor(BLUE);
line(x2,y2,x3,y3);
}
else if((x0<=175)&&(y1>=350))
{
x2=175;
y3=350;
y2=m*(175-x0)+y0;
x3=1/m*(350-y1)+x1;
}
if((x2>x3)&&(y2>y3))
{
x=x2;
x2=x3;
x3=x;
y=y2;
y2=y3;
y3=y;
}
setcolor(BLUE);
line(x2,y2,x3,y3);
}
else
{
if(x2<175)
x2=175;
if(y2<150)
y2=150;
if(x3>450)
x3=450;
if(y3>350)
y3=350;
11
setcolor(BLUE);
line(x2,y2,x3,y3);
}
getch();
closegraph();
restorecrtmode();
}
void main()
{
int i,x,y,x0,x1,y0,y1,dx,dy,d;
char msg[80];
int gd=DETECT,gm;
clrscr();
printf("\nEnter the value of x0 : ");
scanf("%d",&x0);
printf("Enter the value of y0 : ");
scanf("%d",&y0);
printf("Enter the value of x1 : ");
scanf("%d",&x1);
printf("Enter the value of y1 : ");
scanf("%d",&y1);
initgraph(&gd, &gm, "");
setbkcolor(BLUE);
dx=abs(x1-x0);
dy=abs(y1-y0);
d=2*dy-dx;
if(x1>x0)
{
if(y1>y0)
{
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
while((x0<x1)&&(y0<y1))
{
if(d<0)
{
x0=x0+1;
d=d+2*dy;
putpixel(x0,y0,6);
}
else
{
x0=x0+1;
y0=y0+1;
d=d+2*(dy-dx);
putpixel(x0,y0,6);
}
}
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
}
else
{
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
while((x0<x1)&&(y1<y0))
{
if(d<0)
{
x0=x0+1;
d=d+2*dy;
putpixel(x0,y0,6);
}
else
{
13
x0=x0+1;
y0=y0-1;
d=d+2*(dy-dx);
putpixel(x0,y0,6);
}
}
}
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
}
else
{
if(y1>y0)
{
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
while((x1<x0)&&(y0<y1))
{
if(d<0)
{ x0=x0-1;
d=d+2*dy;
putpixel(x0,y0,6);
}
else
{
x0=x0-1;
y0=y0+1;
d=d+2*(dy-dx);
putpixel(x0,y0,6);
}
}
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
}
else
{
sprintf(msg, "(%d,%d)",(int)x0,(int)y0);
outtextxy(x0, y0, msg);
while((x1<x0)&&(y1<y0))
{
if(d<0)
{
x0=x0-1;
d=d+2*dy;
putpixel(x0,y0,6);
}
else
{
x0=x0-1;
y0=y0-1;
d=d+2*(dy-dx);
putpixel(x0,y0,6);
}
}
}
sprintf(msg, "(%d,%d)",(int)x1,(int)y1);
outtextxy(x1, y1, msg);
}
getch();
closegraph();
restorecrtmode();
}
/* Midpoint Circle Drawing */
14
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <graphics.h>
int x,y,xc,yc;
void setpoint();
void main()
{
int i,d,R;
char msg[80];
int gd=DETECT,gm;
printf("\nEnter the value of x of centre : ");
scanf("%d",&xc);
printf("Enter the value of y of centre : ");
scanf("%d",&yc);
printf("Enter the value of Radius : ");
scanf("%d",&R);
x = 0;
y = R;
initgraph(&gd, &gm, "");
setbkcolor(CYAN);
setcolor(RED);
putpixel(xc,yc,4);
sprintf(msg, "(%d,%d)",xc,yc);
outtextxy(xc, yc, msg);
d = 1 - R;
while(y>x)
{
if(d < 0)
{
x = x + 1;
d = d + 2*x + 3;
setpoint();
}
else
{
x = x + 1;
y = y - 1;
d = d + 2*(x-y) + 5;
setpoint();
}
}
getch();
closegraph();
restorecrtmode();
}
void setpoint()
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
int xc,yc,r;
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
putpixel(x1,y1,WHITE);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x2<x1) s1=-1;
else if(x2>x1) s1=1;
else s1=0;
if(y2<y1) s2=-1;
else if(y2>y1) s2=1;
else s2=0;
dp=2*dy-dx;
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
swap=1;
}
for(i=1;i<=dx;i++)
{
if(dp<0)
{
if(swap) putpixel(x,y=y+s2,WHITE);
else putpixel(x=x+s1,y,WHITE);
dp+=2*dy;
}
else
{
putpixel(x=x+s1,y=y+s2,WHITE);
dp=dp+2*dy-2*dx;
}
}
}
void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
bshmLine(x1,y1,x2,y2);
getch();
}
putpixel(x1,y1,WHITE);
if(abs(x2-x1)>=abs(y2-y1))
length=abs(x2-x1);
else
length=abs(y2-y1);
dx=(float)(x2-x1)/length;
dy=(float)(y2-y1)/length;
for(i=1;i<=length;i++)
{
x=x+dx;
y=y+dy;
putpixel((int)x,(int)y,WHITE);
}
}
void main()
{
int x1,y1,x2,y2;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
ddaLine(x1,y1,x2,y2);
getch();
}
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
19
getch();
}
/*--------------------------------------------------------------------*/
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}
/*-------------------------------------------------*/
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
void fill_right(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y);
x = x - 1 ;
fill_right(x,y-1);
fill_right(x,y+1);
}
}
void fill_left(x,y)
int x , y ;
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y);
x = x + 1 ;
fill_left(x,y-1);
fill_left(x,y+1);
}
}
/*------------------------------------------------------*/
void main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
cleardevice();
setcolor(WHITE);
fill_right(x,y);
fill_left(x-1,y);
getch();
}
myCircle::myCircle()
{
x=0;y=0;
cout<<"
"Enter The Major & Minor Axis Of Ellipse ":=";
cin>>major>>minor;
cout<<"
"Enter The Center Of The Ellipse ":=";
cin>>x>>y;
}
void myCircle::showCircle()
{
char *s;
int ax,ay;
float ar;
x1=1;
ratio=major/minor;
getaspectratio(&ax,&ay);
ar=1;
// ar=ay/ax;
while(x1<=major)
{
y1=minor*sqrt((1-(x1*x1/major*major)));
putpixel((x+x1*ar+320),(y+y1+240),5);
putpixel((x+x1*ar+320),(y-y1+240),5);
putpixel((x-x1*ar+320),(y-y1+240),5);
putpixel((x-x1*ar+320),(y+y1+240),5);
dtheta=1/sqrt(x1*x1+y1*y1);
// x2=x1+cos(dtheta)-ratio*y1*sin(dtheta);
// y2=y1+cos(dtheta)+1/ratio*x1*sin(dtheta);
x1++;
// y1=y2;
}
setcolor(5);
outtextxy(318+x,235+y,".");
setcolor(15);
sprintf(s,"Center(%d,%d)",x,y);
outtextxy(20,10,"The Center Is At");
outtextxy(20,20,s);
sprintf(s,"Radius=%d",r);
outtextxy(20,30,s);
getch();
}
void main()
{
int gd=DETECT,gm,i,j,xx=190,xxx=480;
23
clrscr();
myCircle a;
char *mess[]={"B","R","E","S","E","N","H","A","M","'","S","
","C","I","R","C","L","E"," ","A","L","G","O","R","I","T","H","M"};
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=27;i<16,j>=14;i++,j--)
{
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
}
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
{
if(i==320)
continue;
outtextxy(i,237,"+");
}
for(i=50;i<=430;i+=10)
{
if(i==240)
continue;
outtextxy(317,i,"-");
}
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showCircle();
}
float cord[10][3];
int n;
void myclrscr()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
}
void getPoints()
{
int i;
printf("Enter the no of defining vertices: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter coordinate %d: ",i+1);
scanf("%f %f",&cord[i][0],&cord[i][1]);
cord[i][2]=1;
}
}
void drawPolygon()
{
int i;
for(i=0;i<n-1;i++)
line(cord[i][0],cord[i][1],cord[i+1][0],cord[i+1][1]);
line(cord[0][0],cord[0][1],cord[i][0],cord[i][1]);
}
for(i=0;i<m1;i++)
for(j=0;j<m2;j++)
{
sum=0;
for(k=0;k<n;k++)
sum+=mat1[i][k]*mat2[k][j];
ans[i][j]=sum;
}
}
tMat[2][0]=tx;
tMat[2][1]=ty;
25
mul(cord,n,3,tMat,3,cord);
if(draw) drawPolygon();
}
tMat[0][0]=sx;
tMat[1][1]=sy;
tMat[2][0]=xf-sx*xf;
tMat[2][1]=yf-sy*yf;
mul(cord,n,3,tMat,3,cord);
drawPolygon();
}
angle*=PI/180;
translate(-xf,-yf,0);
tMat[0][0]=cos(angle);
tMat[0][1]=sin(angle);
tMat[1][0]=-sin(angle);
tMat[1][1]=cos(angle);
mul(cord,n,3,tMat,3,cord);
translate(xf,yf,0);
drawPolygon();
}
void main()
{
float xf,yf,sx,sy,tx,ty,r;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "e:\\proggy\\tc\\bgi");
getPoints();
drawPolygon();
getch();
myclrscr();
cout<<"1- Translation"<<"\n";
cout<<"2- Scaling"<<"\n";
cout<<"3- Rotation "<<"\n";
cout<<"0- Exit"<<"\n";
int choice;
while(1)
{
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
26
case 1 : printf("Enter the translating co-ordinates:");
scanf("%f %f",&tx,&ty);
myclrscr();
translate(tx,ty,1);
break;
case 2 : printf("Enter the Scaling factors:");
scanf("%f %f",&sx,&sy);
printf("Enter the refrence co-ordinates: ");
scanf("%f %f",&xf,&yf);
myclrscr();
scale(sx,sy,xf,yf);
break;
case 3: printf("Enter the Rotating angle :");
scanf("%f",&r);
printf("Enter the refrence co-ordinates: ");
scanf("%f %f",&xf,&yf);
myclrscr();
rotate(r,xf,yf);
break;
case 0 : exit(0);
getch();
}
}
getch();
}
int gd=DETECT,gm;
double x1,x2,y1,y2;
void show_message()
{
char *mess[]={"-","=","["," ","3","D","-","T","r","a","n","s",
"f","o","r","m","a","t","i","o","n"," ","]","=","-"};
int xx=28,xxx=52,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=24;i<15,j>=12;i++,j--)
{
gotoxy(xx,1);
cout<<mess[i];
xx++;
gotoxy(xxx,1);
cout<<mess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}
case 2:
29
cout<<"
" Enter The Angle ":=";
cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
temp=edge[i][0];
temp1=edge[i][2];
edge[i][0]=temp*cos(theta)+temp1*sin(theta);
edge[i][2]=-temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
case 3:
cout<<"
" Enter The Angle ":=";
cin>>theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
temp=edge[i][0];
temp1=edge[i][1];
edge[i][0]=temp*cos(theta)-temp1*sin(theta);
edge[i][1]=temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
}
}
case 2:
30
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
edge[i][0]=-edge[i][0];
edge[i][2]=-edge[i][2];
}
draw_cube(edge);
break;
case 3:
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
edge[i][0]=-edge[i][0];
edge[i][1]=-edge[i][1];
}
draw_cube(edge);
break;
}
}
case 2:
cout<<"
" Enter Q ":=";
cin>>q;
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1]/(edge[i][1]*q+1);
edge[i][0]=edge[i][0]/(edge[i][1]*q+1);
edge[i][2]=edge[i][2]/(edge[i][1]*q+1);
31
}
draw_cube(edge);
break;
case 3:
cout<<"
" Enter R ":=";
cin>>r;
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2]/(edge[i][2]*r+1);
edge[i][0]=edge[i][0]/(edge[i][2]*r+1);
edge[i][1]=edge[i][1]/(edge[i][2]*r+1);
}
draw_cube(edge);
break;
}
closegraph();
}
void main()
{
int choice;
double edge[20][3]=
{
100,0,0,
100,100,0,
0,100,0,
0,100,100,
0,0,100,
0,0,0,
100,0,0,
100,0,100,
100,75,100,
75,100,100,
100,100,75,
100,100,0,
100,100,75,
100,75,100,
75,100,100,
0,100,100,
0,100,0,
0,0,0,
0,0,100,
100,0,100
};
while(1)
{
clrscr();
show_message();
cout<<"
case 2:
scale(edge);
break;
case 3:
rotate(edge);
break;
case 4:
reflect(edge);
break;
case 5:
translate(edge);
break;
case 6:
perspect(edge);
break;
case 7:
exit(0);
default:
cout<<"
a" Press A Valid Key...!!! "";
getch();
break;
}
closegraph();
}
}