C语言实现绘图措施
当前位置:以往代写 > C/C++ 教程 >C语言实现绘图措施
2019-06-13

C语言实现绘图措施

C语言实现绘图措施

笔者在开拓县级电网调治自动化系统的进程中,遇到一个要绘制电力系统一次接线图的问题。由于电力系统一次接线图较量巨大,图上有一些非凡的标记,象变压器标记、开关标记等等,用一般的编程要领来绘制较量坚苦,因此,笔者用C语言开拓了一个手工绘制电力系统一次接线图的键盘画图措施,利用该措施绘制的一次接线图切适用户需求,结果精采。

2.利用要领

措施中界说了几个非凡键:

"V”:画笔提起

"W”:开始绘图

"R”:开始擦图

"S”:当前图形存入文件

"E”:调出已有文件

"C”:画圆

措施一运行,屏幕上呈现一个黄色的边框来设定绘图的区域,区域中间呈现提起的画笔标记 ,当按下”W“键时,画笔标记变为 ,此时可移动偏向键(上、下、左、右、左上、左下、右上、右下)来绘图;当按下”R“键时,画笔标记变为 ,此时可移动偏向键来擦图;在绘图进程中,按下“C”键,可画出一个半径为20个象素点的圆;当竣事绘图时,按下“S”键,将画好的图形存盘;按下“E”键可调出已有的图形举办编辑。

3.源措施清单

# include "graphics.h"
# include "stdio.h"
# include "fcntl.h"
# include "stdlib.h"
main()
void save(),load();
void *wg,*rg,*vg,*fy;
int driver,mode;
int c=RED;
int x=320,y=225;
int x1,y1,x2,y2;
int k,k1,k2;
/* initialize grapher */
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"c:\tc");
/* write the pen */
bar(200,10,206,16);
line(203,7,200,10);
line(203,7,206,10);
line(243,7,240,16);
line(243,7,246,16);
line(283,7,280,10);
line(283,7,286,10);
line(283,7,283,16);
/* save the pen */
wg=malloc(imagesize(200,7,206,16));
rg=malloc(imagesize(240,7,246,16));
vg=malloc(imagesize(280,7,286,16));
fy=malloc(imagesize(200,7,206,16));
getimage(200,7,206,16,wg);
getimage(240,7,246,16,rg);
getimage(280,7,286,16,vg);
cleardevice();
/* write the box */
setcolor(YELLOW);
rectangle(4,19,637,447);
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
getimage(x1,y1,x2,y2,fy);
putimage(x1,y1,vg,XOR_PUT);
/* receive the command */
for (;;)
while (bioskey(1)==0);
k=bioskey(0);
putimage(x1,y1,fy,AND_PUT);
if (((k&0x00ff)|0x00)==0)
k1=k&0xff?0:k>>8; /* k1 is the specialkey value */
else
k2=k&0x00ff; /* k2 is the non-specialkey value */
if (((k&0x00ff)|0x00)==0) /* Special key */
switch(k1)
case 45:
restorecrtmode();
exit(0);
case 72:
if (y>20)
y=y-1;
break;
case 75:
if (x>5)
x=x-1;
break;
case 77:
if (x<636)
x=x+1;
break;
case 80:
if (y<446)
y=y+1;
break;
case 71:
if ((x>5)&&(y>20))
x=x-1;
y=y-1;
break;
case 79:
if ((x>5)&&(y<446))
x=x-1;
y=y+1;
break;
case 73:
if ((x<636)&&(y>20))
x=x+1;
y=y-1;
break;
case 81:
if ((x<636)&&(y<446))
x=x+1;
y=y+1;
break;
x1=x-3;
y1=y+1;
x2=x+3;
y2=y+10;
getimage(x1,y1,x2,y2,fy);
/* non-special key */
switch(k2)
case 118: /* 'v' */
case 86: /* 'V' */
putimage(x1,y1,vg,OR_PUT);
break;
case 119: /* 'w' */
case 87: /* 'W' */
putimage(x1,y1,wg,OR_PUT);
putpixel(x,y,c);
break;
case 114: /* 'r' */
case 82: /* 'R' */
putimage(x1,y1,rg,OR_PUT);
putpixel(x,y,BLACK);
break;
case 115: /* 's' */
case 83: /* 'S' */
save("pic.dat");
break;
case 101: /* 'e' */
case 69: /* 'E' */
load("pic.dat");
break;
case 99: /*'c'*/
case 67: /*'C'*/
setcolor(RED);
circle(x,y,20);
break;
default:continue;
/* function for screen picture save
*/
void save(char *fname)
FILE *fp;
int i;
register long j;
char far *ptr;
fp=fopen(fname,"wb");
for(i=0;i<4;i++)
outportb(0x3CE,4);
outportb(0x3CF,i);
ptr=(char far *) 0xA0000000L;
for (j=0;j<38400L;j++)
putc(*ptr,fp);
ptr++;
fclose(fp);
outportb(0x3CF,0);
/* function for screen picture display
*/
void load(char *fname)
FILE *fp;
register int i;
int k4=1;
register long j;
char far *ptr;
fp=fopen(fname,"rb");
for (i=0;i<4;i++)
outportb(0x3C4,2);
outportb(0x3C5,k4);
ptr=(char far *)0xA0000000L;
for (j=0;j<38400L;j++)
*ptr=getc(fp);
ptr++;
k4*=2;
fclose(fp);
outportb(0x3C5,0xF);

4.竣事语

该措施在Turbo C 2.0情况下运行通过,利用结果精采。可以按照详细需要,对该措施举办扩充,以增加画图成果。

    关键字:

在线提交作业