谁有C语言的画图应用程序
发布网友
发布时间:2022-04-29 23:38
我来回答
共1个回答
热心网友
时间:2022-06-26 03:56
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<bios.h>
#include<ctype.h>
#include<process.h>
#include<conio.h>
#define COLOR WHITE
#define F1 59
#define F2 60
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define HOME 71
#define END 79
#define PAGEUP 73
#define PAGEDOWN 81
int inc=1;
typedef union /*返回键盘输入值*/
{char c[2];
int i;
}KEY;
KEY key;
typedef struct /*定义点*/
{int x;
int y;
}DOT;
DOT dis,firstdot,secdot,dot1;
int point();
int do_that();
void mv_cp(int startx,int starty,int endx,int endy,int x,int y,int flag);/*移动函数*/
void fill(int startx,int starty,int endx,int endy);/*填充*/
void colour();/*颜色*/
void menu(void);/*主菜单*/
void save(void);/*保存*/
void load(void);/*打开文件*/
void xhairs(int a,int b);/*显示光标*/
void arr_key(char c);
/*--------------------------------主函数---------------------------------------*/
void main()
{
int k,j,m,r,x1,y1,xn,yn;
int driver,mode;
dis.x=200;
dis.y=200;
driver=DETECT;
mode=VGAHI;
registerbgidriver(EGAVGA_driver);
registerbgifont(TRIPLEX_FONT);
initgraph(&driver,&mode,"f:\\tc2.01\\BGI");
setbkcolor(BLUE);
cleardevice();
setcolor(YELLOW);
xhairs(dis.x,dis.y);
while(key.c[0]!='q')
{
menu();/*显示菜单*/
key.i=bioskey(0);
xhairs(dis.x,dis.y);
if(!key.c[0])
arr_key(key.c[1]);
else
switch(tolower(key.c[0]))
{
case 'f':
xhairs(dis.x,dis.y);colour();break;
case 'd':x1=y1=0;xhairs(dis.x,dis.y);setcolor(LIGHTRED);
outtextxy(10,22+(13)*30,"b:begine point");
outtextxy(10,22+(14)*30,"g:give up");
x1=point();
fill(6,410,149,430);
xhairs(dis.x,dis.y);
outtextxy(10,22+(13)*30,"e:end point");
outtextxy(10,22+(14)*30,"g:give up");
y1=point();
fill(6,410,149,460);
setcolor(COLOR);
if(x1&&y1)
{outtextxy(10,22+(13)*30,"move the +");
outtextxy(10,22+(14)*30,"y:do n:no");
}
x1=0;
xhairs(dis.x,dis.y);
x1=do_that();
if(x1)
{xn=dis.x;yn=dis.y;
mv_cp(firstdot.x,firstdot.y,secdot.x,secdot.y,xn,yn,0);
}
fill(6,410,149,460);break;
case 'l':x1=y1=0;/*画直线*/
xhairs(dis.x,dis.y);
setcolor(LIGHTRED);
outtextxy(10,22+(13)*30,"b:begin point");
outtextxy(10,22+(14)*30,"g:give up");
x1=point();
fill(6,410,149,430);
xhairs(dis.x,dis.y);
outtextxy(10,22+(13)*30,"e:end point");
outtextxy(10,22+(14)*30,"g:five up");
y1=point();
setcolor(COLOR);
if(x1&&y1)
line(firstdot.x,firstdot.y,secdot.x,secdot.y);
fill(6,410,155,460);
break;
case 'o':/*画圆形*/
x1=y1=0;
xhairs(dis.x,dis.y);
setcolor(LIGHTRED);
outtextxy(10,22+(13)*30,"b:radius");
outtextxy(10,22+(14)*30,"g:give up");
x1=point();
fill(6,410,149,430);
xhairs(dis.x,dis.y);
outtextxy(10,22+(13)*30,"e:radius");
outtextxy(10,22+(14)*30,"g:give up");
y1=point();
if(x1&&y1) setcolor(COLOR);
r=sqrt(fabs((secdot.x-firstdot.x)*(secdot.x-firstdot.x)+(secdot.y-firstdot.y)*(secdot.y-firstdot.y)));
if(x1&&y1) circle(firstdot.x,firstdot.y,r);
fill(6,410,149,460);break;
case 'h':/*画矩形*/
x1=y1=0;
xhairs(dis.x,dis.y);
setcolor(LIGHTRED);
outtextxy(10,22+(13)*30,"b:left top");
outtextxy(10,22+(14)*30,"g:give up");
x1=point();
fill(6,410,149,430);
xhairs(dis.x,dis.y);
outtextxy(10,22+(13)*30,"e:right bottom");
outtextxy(10,22+(14)*30,"g:give up");
y1=point();
if(x1&&y1)
{
setcolor(COLOR);
rectangle(firstdot.x,firstdot.y,secdot.x,secdot.y);
}
fill(6,410,155,460);break;
case 's':cleardevice();break;/*清屏*/
case 'w':save();break;
case 'r':load();break;
case 'm':x1=y1=0;/*图形移动*/
xhairs(dis.x,dis.y);
setcolor(LIGHTRED);
outtextxy(10,22+(13)*30,"b:begin point");
outtextxy(10,22+(14)*30,"g:give up");
x1=point();
fill(6,410,149,430);
xhairs(dis.x,dis.y);
outtextxy(10,22+(13)*30,"e:end point");
outtextxy(10,22+(14)*30,"g:give up");
y1=point();
fill(6,410,149,460);
setcolor(COLOR);
if(x1&&y1)
{
outtextxy(10,22+(13)*30,"move the +");
outtextxy(10,22+(14)*30,"y:do n:no");
}
x1=0;
xhairs(dis.x,dis.y);
x1=do_that();
if(x1)
{
xn=dis.x;yn=dis.y;
mv_cp(firstdot.x,firstdot.y,secdot.x,secdot.y,xn,yn,1);
}
fill(6,410,149,460);break;
}
xhairs(dis.x,dis.y);
}
closegraph();
}
/*--------------------------------显示光标函数--------------------------------*/
void xhairs(int a,int b)
{
int i;
for(i=a-3;i<=a+3;i++)
putpixel(i,b,15^getpixel(i,b));
for(i=b-3;i<=b+3;i++)
putpixel(a,i,15^getpixel(a,i));
}
/*------------------------------菜单函数------------------------------*/
void menu(void)
{
#define STARTX 10
#define STARTY 22
#define DISTANCE 30
int i;
char *menu_name[]={"F1:setp=1",
"F2:step=5",
"l:draw line",
"draw cirle",
"h:graw box",
"s:clear screen",
"m:move",
"d:copy",
"f:fill color",
"w:save",
"r:load",
"q:quit"};
setcolor(WHITE);
rectangle(5,5,150,470);
rectangle(5,403,150,470);
rectangle(155,5,635,470);
setcolor(LIGHTBLUE);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
outtextxy(36,STARTY,"menu");
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
for(i=0;i<12;i++)
outtextxy(STARTX,STARTY+(i+1)*DISTANCE,menu_name[i]);
}
/*---------------------------------保存函数---------------------------------*/
void save(void)
{int i,j;
FILE *fp;
if((fp=fopen("graph.dat","w+"))==NULL)
{printf("cannot open this file!");
exit(0);
}
fill(260,473,400,480);
for(i=5;i<470;i++)
for(j=155;j<=635;j++)
fputc(getpixel(j,i),fp);
fclose(fp);
outtextxy(260,471,"save over!");
}
/*-----------------------------------------打开函数------------------------*/
void load(void)
{
int i,j;
FILE *fp;
if((fp=fopen("graph.dat","r+"))==NULL)
{
outtextxy(260,471,"cannot open this file!");
getch();
exit(0);
}
fill(260,471,400,480);
outtextxy(280,471,"loading......");
for(i=5;i<=470;i++)
for(j=155;j<=635;j++)
putpixel(j,i,fgetc(fp));
fill(260,471,400,480);
fclose(fp);
outtextxy(280,471,"loading over");
}
/*-----------------------------------------移动复制函数---------------------------*/
void mv_cp(int startx,int starty,int endx,int endy,int x,int y,int flag)
{
int i,j;
unsigned char c;
if(startx>endx)
{i=startx;startx=endx;endx=i;}
if(starty>endy)
{i=starty;
starty=endy;endy=i;}
for(;startx<=endx;startx++,x++)
for(i=starty,j=y;i<=endy;i++,j++)
{c=getpixel(startx,i);
if(flag) putpixel(startx,i,BLUE);
putpixel(x,j,c);
}
}
/*----------------------------返回点坐标函数----------------------------------*/
int point()
{int valid_p=0,g=1;
key.c[0]='';
while(key.c[0]!='p')
{key.i=bioskey(0);
xhairs(dis.x,dis.y);
if(!key.c[0])
arr_key(key.c[1]);
else
switch(tolower(key.c[0]))
{case 'b':putpixel(dis.x,dis.y,COLOR);
firstdot.x=dis.x;
firstdot.y=dis.y;
key.c[0]='p';
valid_p=1;
break;
case 'e':
putpixel(dis.x,dis.y,COLOR);
secdot.x=dis.x;
secdot.y=dis.y;
key.c[0]='p';
valid_p=2;break;
case 'g':
firstdot.x=secdot.x;
firstdot.y=secdot.y;
key.c[0]='p';
g=0;break;
}
xhairs(dis.x,dis.y);
if(valid_p==1||valid_p==2||g==0)
break;
}
xhairs(dis.x,dis.y);
return valid_p;
}
/*------------------------------确认操作------------------------*/
int do_that()
{
int valid_p=0,g=1;
key.c[0]='';
while(key.c[0]!='p')
{key.i=bioskey(0);
xhairs(dis.x,dis.y);
if(!key.c[0])
arr_key(key.c[1]);
else
switch(tolower(key.c[0]))
{case 'y':
key.c[0]='p';
putpixel(dis.x,dis.y,COLOR);
valid_p=1;break;
case 'n':
key.c[0]='p';
firstdot.x=secdot.x;
firstdot.y=secdot.y;
g=0;break;}
xhairs(dis.x,dis.y);
if(valid_p==1||g==0)
break;
}
xhairs(dis.x,dis.y);
return valid_p;
}
/*-----------------------------填充-------------------------------*/
void fill(int startx,int starty,int endx,int endy)
{int i,j;
for(i=startx;i<=endx;i++)
for(j=starty;j<=endy;j++)
{putpixel(i,j,BLUE);
}
}
/*-----------------------------------颜色定义--------------------------------*/
void colour()
{
key.c[0]='';
setcolor(RED);
outtextxy(10,412,"R");
setcolor(GREEN);
outtextxy(40,412,"G");
setcolor(CYAN);
outtextxy(70,412,"C");
setcolor(LIGHTBLUE);
outtextxy(10,432,"L");
setcolor(MAGENTA);
outtextxy(40,432,"M");
setcolor(YELLOW);
outtextxy(70,432,"Y");
setcolor(WHITE);
outtextxy(10,452,"W");
setcolor(BROWN);
outtextxy(40,452,"N");
setcolor(LIGHTRED);
outtextxy(70,452,"D");
while(key.c[0]!='p')
{
key.i=bioskey(0);
xhairs(dis.x,dis.y);
if(!key.c[0])
arr_key(key.c[0]);
else
{switch(tolower(key.c[0]))
{case 'y' : setfillstyle(SOLID_FILL,YELLOW); break;
case 'r' : setfillstyle(SOLID_FILL,RED);break;
case 'g' : setfillstyle(SOLID_FILL,GREEN);break;
case 'c' : setfillstyle(SOLID_FILL,CYAN);break;
case 'l' : setfillstyle(SOLID_FILL,LIGHTBLUE);break;
case 'm' : setfillstyle(SOLID_FILL,MAGENTA);break;
case 'w' : setfillstyle(SOLID_FILL,WHITE);break;
case 'n' : setfillstyle(SOLID_FILL,BROWN);break;
case 'd' : setfillstyle(SOLID_FILL,LIGHTRED);break;
}
key.c[0]='p';}
/*-------------------------光标---------------------*/
xhairs(dis.x,dis.y);
}
xhairs(dis.x,dis.y);
floodfill(dis.x,dis.y,WHITE);
fill(6,410,149,460);
}
/*-----------------------------光标控制----------------------------*/
void arr_key(char c)
{switch(c)
{case LEFT : dis.x-=inc;break;
case RIGHT : dis.x+=inc;break;
case UP : dis.y-=inc;break;
case DOWN : dis.y+=inc;break;
case F1 : inc=1;break;
case F2 : inc=5;break;
}
}