C語(yǔ)言如何實(shí)現(xiàn)畫(huà)圖教程
C語(yǔ)言如何實(shí)現(xiàn)畫(huà)圖教程
C語(yǔ)言有豐富的數(shù)據(jù)結(jié)構(gòu)和運(yùn)算符。包含了各種數(shù)據(jù)結(jié)構(gòu),如整型、數(shù)組類(lèi)型、指針類(lèi)型和聯(lián)合類(lèi)型等,用來(lái)實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)的運(yùn)算。以下是小編為大家搜索整理的C語(yǔ)言如何實(shí)現(xiàn)畫(huà)圖教程,希望能給大家?guī)?lái)幫助!更多精彩內(nèi)容請(qǐng)及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!
程序中定義了幾個(gè)特殊鍵:
"V”:畫(huà)筆提起
"W”:開(kāi)始畫(huà)圖
"R”:開(kāi)始擦圖
"S”:當(dāng)前圖形存入文件
"E”:調(diào)出已有文件
"C”:畫(huà)圓
程序一運(yùn)行,屏幕上出現(xiàn)一個(gè)黃色的`邊框來(lái)設(shè)定畫(huà)圖的區(qū)域,區(qū)域中間出現(xiàn)提起的畫(huà)筆符號(hào) ,當(dāng)按下”W“鍵時(shí),畫(huà)筆符號(hào)變?yōu)?,此時(shí)可移動(dòng)方向鍵(上、下、左、右、左上、左下、右上、右下)來(lái)畫(huà)圖;當(dāng)按下”R“鍵時(shí),畫(huà)筆符號(hào)變?yōu)?,此時(shí)可移動(dòng)方向鍵來(lái)擦圖;在畫(huà)圖過(guò)程中,按下“C”鍵,可畫(huà)出一個(gè)半徑為20個(gè)象素點(diǎn)的圓;當(dāng)結(jié)束畫(huà)圖時(shí),按下“S”鍵,將畫(huà)好的圖形存盤(pán);按下“E”鍵可調(diào)出已有的圖形進(jìn)行編輯。
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.結(jié)束語(yǔ)
該程序在Turbo C 2.0環(huán)境下運(yùn)行通過(guò),使用效果良好?梢愿鶕(jù)具體需要,對(duì)該程序進(jìn)行擴(kuò)充,以增加繪圖功能。
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶(hù)自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請(qǐng)發(fā)送郵件至 yyfangchan@163.com (舉報(bào)時(shí)請(qǐng)帶上具體的網(wǎng)址) 舉報(bào),一經(jīng)查實(shí),本站將立刻刪除