Post on 14-Dec-2015
Graphics in MS-DOS Environment
LEE HUN HEE
1.Real Coordinate and Windows Coordinate
●Real Coordinate ->(x,y)
●Windows Coordinate ->(wx,wy)
∴
∴
(x,y)->(wx,wy) •
(wx min,wy min)
(x min,y max)
(wx min, wy max)
(x min,y min)
(wx max,wy max)
(x max,ymin)
(wx max,wy min)
(x max,y max)
y
x
minmax
min
minmax
min
wxwx
wxwx
xx
xx
max
minmax
minmaxmin))((wx
xx
wxwxxxwx
minmax
minminmax
minmax
min))((
wywy
wywywywy
yy
yy
minminmax
minmax
minmaxmin )())((
wywywyyy
wywyyywy
(Graphic driver of graphic card must exist in folder)
2.Graphic card and Graphic mode
graphic card Graphic driver
HGA herc.bgi
EGA ega.bgi
VGA egavga.bgi
circle():circle draws a circlerectangle():draws a rectangle (graphics mode)line():line draws a line between two specified pointslineral():line draws a line between two specified pointssetlinestyle():sets the current line style and width or patternfloodfill():flood-fills a bounded regionsetfillstyle():sets the fill pattern and colorgetpixel(): getpixel gets the color of a specified pixelputpixel():putpixel plots a pixel at a specified point
settextstyle():sets the current text characteristicsouttextxy():outtextxy displays a string at the specified location (graphics mode)textheight, ():textheight returns the height of a string in pixelstextwidth():textwidth returns the width of a string in pixels
3.Keyword
getx():
getx returns the current position's x coordinate
gety():
gety returns the current position's y coordinate
getmaxx ():
returns maximum x screen coordinate
getmaxy():
returns maximum y screen coordinate
Initgraph():
initializes the graphics system
closegraph():
shuts down the graphics system
cleardevice:
clears the graphics screen
#include <graph.h>
void main()
{
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode," ");
.
.
graphic
.
.
closegraph();
}
4.program source
#include <graph.h>
void main()
{
int x;
double y;
int driver,mode;
driver=DETECT;
initgraph(&driver,&mode," ");
line(260,0,260,450);
line(0,30,550,30);
line(390,190,420,190);
outtextxy(10,40,"x");
outtextxy(270,400,"y");
outtextxy(270,20,"0");
outtextxy(420,190,"Y=-x*x");
for(x=60;x<460;x++)}
y=0.01*(x-260)*(x-260)+30;
rectangle(x,y,x+2,y+2);
}
getch();
closegraph();
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define c 32767
int ix,iy,select,select1;
int vx1,vy1,vx2,vy2;
double wx1,wy1,wx2,wy2,txi,tyi,ticx,ticy;
char H[10],V[10],f1[10],f2[10],f3[10],f4[10];
void plot(double, double, int);
void draw(double, double, double, double);
void drawxy(double, double);
void outstrxy(double, double, char s[]);
void ticxy();
void HVlabel();
void print(double, double, double);
double round(double);
void initialization();
void initialization();
{
printf("Input the coordinates of the viewport(vx1,vy1,vx2,vy2)");
printf("ex) 100 60 550 400 ");
scanf("%d %d %d %d",&vx1,&vy1,&vx2,&vy2);
printf("the coordinates of the window (wx1,wy1,wx2,wy2)");
printf("ex) -0.5 -2.0 0.5 2.0 ");
scanf("%lf %lf %lf %lf",&wx1,&wy1,&wx2,&wy2);
printf("the information of the ticx (txi,ticx,ix)");
printf("txi: position of the initial tic on the x-axis ");
printf("ticx: interval between the tic's ");
printf("ix: the value of the x coordinate at every (ix)th tic is shown");
printf("ex) -0.5 0.25 1 ");
scanf("%lf %lf %d",&txi,&ticx,&ix);
printf("the information of the ticy (tyi,ticy,iy)");
printf("tyi: position of the initial tic on the y-axis ");
printf("ticy: interval between the tic's ");
printf("iy: the value of the y coordinate at every (iy)th tic is shown");
printf("ex) -2.0 1.0 1 ");
scanf("%lf %lf %d",&tyi,&ticy,&iy);
printf("the information of the floating point to");
printf(" ticx(%%f,%%s) and ticy(%%f,%%s)");
printf("%%5.2f: print as floating point, at least 5 wide and 2 after");
5.graph.h
void draw(double x1,double y1,double x2,double y2)
{
int ix1,iy1,ix2,iy2;
double u1,v1,u2,v2;
u1 = (x1-wx1)/(wx2-wx1)*(vx2-vx1);
v1 = (wy2-y1)/(wy2-wy1)*(vy2-vy1);
u2 = (x2-wx1)/(wx2-wx1)*(vx2-vx1);
v2 = (wy2-y2)/(wy2-wy1)*(vy2-vy1);
if( fabs(u1) > c || fabs(v1) > c || fabs(u2) > c || fabs(v2) > c )
exit(-1);
ix1 = round(u1); iy1 = round(v1); ix2 = round(u2); iy2 = round(v2);
line(ix1,iy1,ix2,iy2);
}
void drawxy(double x,double y)
{
int ix,iy;
double u,v;
u = (x-wx1)/(wx2-wx1)*(vx2-vx1);
v = (wy2-y)/(wy2-wy1)*(vy2-vy1);
if( fabs(u) > c || fabs(v) > c ) exit(-1);
ix = round(u); iy = round(v);
lineto(ix,iy);
}
void outstrxy(double x,double y,char s[])
{
int ix, iy;
double u,v;
u = (x-wx1)/(wx2-wx1)*(vx2-vx1);
v = (wy2-y)/(wy2-wy1)*(vy2-vy1);
if( fabs(u) > c || fabs(v) > c ) exit(-1);
ix = round(u); iy = round(v);
outtextxy(ix,iy,s);
}
printf(" decimal point; [ex) -1.22 -> %%5.2f, -1.222 -> %%6.3f] ");
printf("%%5s: print as characters, at least 5 wide;");
printf(" [ex)-1.22 -> %%5s, -1.222 -> %%6s]");
printf("ex) %%5.2f %%5s %%6.3f %%6s");
scanf("%s %s %s %s",f1,f2,f3,f4);
printf("(tic_in = 1 or tic_out = 0)");
printf("(tic_in [tic_out]: tic's are shown inside [outside] the frame.)");
scanf("%d",&select);
printf("(tic_up_right = 1 or not = 0)");
printf("(The tics are always shown on the left and bottom axis; however,");
printf(" the tics on the right and top axis are optional.)");
scanf("%d",&select1);
printf("the label on the horizontal axis");
scanf("%s",H);
printf("the label on the vertical axis");
scanf("%s",V);
}
double round(double x)
{
return(( x>0 ) ? floor(x + .5) : ceil(x - .5));
}
void plot(double x, double y,int color)
{
int ix,iy;
double u,v;
u = (x-wx1)/(wx2-wx1)*(vx2-vx1);
v = (wy2-y)/(wy2-wy1)*(vy2-vy1);
if( fabs(u) > c || fabs(v) > c ) exit(-1);
ix = round(u); iy = round(v);
putpixel(ix,iy,color);
}
void ticxy()
{
char s[20],buf[20];
i nt I;
double ticxl,ticyl,x1,y1,x2,y2,x22,y22;
ticxl = 6.0 * (wy2-wy1)/(vy2-vy1);
ticyl = 6.0 * (wx2-wx1)/(vx2-vx1);
x1 = txi; y1 = wy1; x2 = x1;
I = 0;
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
if(select == 0)
y2 = y1-ticxl*2;
y22 = y1-ticxl;
do
{
draw(x1,y1,x2,y2); if(select1 == 1) draw(x1,wy2,x2,wy2+ticxl*2);
if(x1+ticx/2 <= wx2)
draw(x1+ticx/2,y1,x2+ticx/2,y22);
if(select1 == 1) draw(x1+ticx/2,wy2,x2+ticx/2,wy2+ticxl);
}
if(I%ix == 0) {
sprintf(buf,f1,x1);
sscanf(buf,f2,s);
outstrxy(x1,y2-1.5*ticxl,s);
/* if(select1 == 1) outstrxy(x1,wy2+ticxl*2+1.5*ticxl,s);*/
x1 = x1+ticx; x2 = x1;
I++;
} while(x1 <= wx2);
}
else{
y2 = y1+ticxl*2;
y22 = y1+ticxl;
do{
draw(x1,y1,x2,y2); if(select1 == 1) draw(x1,wy2,x2,wy2-ticxl*2);
if(x1+ticx/2 <= wx2){
draw(x1+ticx/2,y1,x2+ticx/2,y22);
if(select1 == 1) draw(x1+ticx/2,wy2,x2+ticx/2,wy2-ticxl);
}
if(I%ix == 0){
sprintf(buf,f1,x1);
sscanf(buf,f2,s);
outstrxy(x1,y1-4*ticxl,s);
/* if(select1 == 1) outstrxy(x1,wy2+4*ticxl,s);*/
}
x1 = x1+ticx; x2 = x1;
I++;
} while(x1 <= wx2);
}
y1=tyi; x1=wx1; y2=y1;
I = 0;
settextstyle(DEFAULT_FONT,VERT_DIR,1);
Do{
draw(x1,y1,x2,y2);
if(select1 == 1) draw(wx2,y1,wx2-ticyl*2,y2);
if(y1+ticy/2 <= wy2){
draw(x1,y1+ticy/2,x22,y2+ticy/2);
if(select1 == 1) draw(wx2,y1+ticy/2,wx2-ticyl,y2+ticy/2);
if(I%iy == 0){
sprintf(buf,f3,y1);
sscanf(buf,f4,s);
outstrxy(x1-4*ticyl,y2,s);
/* if(select1 == 1) outstrxy(wx2+4*ticyl,y2,s);*/ }
y1 = y1+ticy; y2 = y1;
I++;
} while(y1 <= wy2);}
}
void HVlabel()
{
double hl,vl,xh,yh;
hl = 11.0 * (wy2-wy1)/(vy2-vy1);
vl = 11.0 * (wx2-wx1)/(vx2-vx1);
xh = wx1 + 0.5*(wx2-wx1);
yh = wy1 + 0.5*(wy2-wy1);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outstrxy(xh,wy1-5.0*hl,H);
settextstyle(DEFAULT_FONT,VERT_DIR,1);
outstrxy(wx1-5.0*vl,yh,V);
}
if(select == 0){
x2 = x1-ticyl*2;
x22 = x1-ticyl;
do{
draw(x1,y1,x2,y2); if(select1 == 1) draw(wx2,y1,wx2+ticyl*2,y2);
if(y1+ticy/2 <= wy2){
draw(x1,y1+ticy/2,x22,y2+ticy/2);
if(select1 == 1) draw(wx2,y1+ticy/2,wx2+ticyl,y2+ticy/2);
}
if(I%iy == 0){
sprintf(buf,f3,y1);
sscanf(buf,f4,s);
outstrxy(x2-1.5*ticyl,y2,s);
/* if(select1 == 1) outstrxy(wx2+ticyl*2+1.5*ticyl,y2,s);*/ }
y1 = y1+ticy; y2 = y1;
I++;
} while(y1 <= wy2);
}
else{
x2 = x1+ticyl*2;
x22 = x1+ticyl;