绘制机械图c语言编程,怎样用c语言编写机械三视图啊……急……

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

m191

2013.10.11

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:56%    等级:12

已帮助:5172人

#include

#include

#include

#include

#include

#include

#include

#include

#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",

"o: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; }

}

00分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值