c语言简易绘图程序代码,简单的鼠标绘图程序
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
#include
#include
#include
union?REGS?regs;
int?X_max,Y_max,x_max,y_max;
void?Initgr(void)?/*屏幕初始化成圖形模式*/
{int?w,h,grdriver=DETECT,grmode;
initgraph(&grdriver,&grmode,"");/*在雙引號中可加你tc放的路徑,但要在tc里有EGAVGA.BGI這個來初始圖形*/
if(graphresult())/*若調用不成功,退出*/
{printf("\n?在雙引號中可加你tc放的路徑,但要在tc里有EGAVGA.BGI這個來初始圖形.\n");
exit(1);
}
X_max=getmaxx();/*求橫向象點坐標數*/
Y_max=getmaxy();/*求縱向象點坐標數*/
getaspecatio(&w,&h);/*求縱橫比*/
x_max=1000;?/*設置屏幕坐標的寬度*/
y_max=x_max*(float)Y_max*h/((float)X_max*w);
}
int?Msinit(int?Xlo,int?Xhi,int?Ylo,int?Yhi)/*鼠標初始化*/
{int?retcode;
regs.x.ax=0;/*初始化鼠標*/
int86(0x33,®s,®s);
retcode=regs.x.ax;
if(retcode==0)?return?0;
regs.x.ax=7;/*設置鼠標X方向的移動范圍*/
regs.x.cx=Xlo;
regs.x.dx=Xhi;
int86(0x33,®s,®s);
regs.x.ax=8;/*設置鼠標Y方向的移動范圍*/
regs.x.cx=Ylo;
regs.x.dx=Yhi;
int86(0x33,®s,®s);
regs.x.ax=15;/*設置mickey與象素的比,這各會影響鼠標移動速度*/
regs.x.cx=(int)(x_max/X_max);
regs.x.dx=(int)(y_max/Y_max);
int86(0x33,®s,®s);
return?retcode;
}
int?Msread(int?*px,int?*py,int?*pbuttons)/*讀鼠標位置及狀態*/
{static?int?x0=320,y0=240,but0=0;
int?xnew,ynew,ch;
do{
if(kbhit()){
ch=getch();
if(ch==13){
*pbuttons=1;
return?-1;
}
else?return?ch;?/*返回鍵盤輸入*/
}
regs.x.ax=3;?/*調用功能3,讀鼠標位置及狀態*/
int86(0x33,®s,®s);
xnew=regs.x.cx;/*返回鼠標當前的位置的X坐標*/
ynew=regs.x.dx;/*返回鼠標當前的位置的Y坐標*/
*pbuttons=regs.x.bx;/*返回鼠標當前的狀態*/
}while(xnew==x0&&ynew==y0&&*pbuttons==but0);
/*當鼠標狀態改變或位置改變終止循環*/
but0=*pbuttons;/*將鼠標狀態保存到靜態變量中*/
x0=xnew;y0=ynew;/*將鼠標位置保存到靜態變量中*/
*px=xnew;*py=(int)(y_max-ynew);
return?-1;
}
int?Xpixel(int?x)/*由象素坐標變換為屏幕坐標*/
{?return?(int)((long)X_max*x/x_max);}
int?Ypixel(int?y)
{?return?Y_max-(int)((long)Y_max*y/y_max);}
void?Cursor(int?x,int?y)?/*顯示十字光標*/
{int?X=Xpixel(x),Y=Ypixel(y),color;
char?*s="?\0";
line(X-8,Y,X-3,Y);
line(X,Y-8,X,Y-4);
line(X-3,Y,X-8,Y);
line(X,Y-4,X,Y-8);
color=getcolor();
setcolor(BLACK);
outtextxy(X_max-100,10,s);/*刪除前次顯示值*/
sprintf(s,"%d,%d",x,y);
setcolor(WHITE);
outtextxy(X_max-100,10,s);/*在屏幕右上角顯示當前光標的坐標*/
setcolor(color);
}
main()
{int?buttons,X,Y,x,y,a,b;
char?i;
Initgr();/*初始化圖形屏幕*/
setcolor(EGA_LIGHTRED);/*設置屏幕前景色*/
line(1,1,X_max-1,1);?/*在屏幕四周畫一矩形*/
line(1,1,1,Y_max-1);
line(X_max-1,1,X_max-1,Y_max-1);
line(1,Y_max-1,X_max-1,Y_max-1);
setcolor(EGA_WHITE);
printf("\n?按鼠標右鍵終止程序\n");
printf("?然后按任意鍵退出");
setwritemode(XOR_PUT);/*設置屏幕輸出模式*/
Msinit(0,(int)x_max,0,(int)y_max);/*初始化鼠標*/
a=x_max;b=y_max;
x=0;
Cursor(a,b);?/*在a=x_max;b=y_max;處畫指針*/
while(x!=2)
{
Msread(&X,&Y,&x);
Cursor(a,b);?/*刪除之前的鼠標,因為屏幕輸出模式的關系*/
if(x==1){
a=(int)(X*1.0/x_max*X_max);
b=(int)((y_max-Y)*1.0/y_max*Y_max);
circle(a,b,1);?}?/*畫點*/
Cursor(X,Y);
a=X;b=Y;
}
Cursor(X,Y);/*再調用一次把原來的指針削掉*/
getch();
closegraph();}
總結
以上是生活随笔為你收集整理的c语言简易绘图程序代码,简单的鼠标绘图程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高斯坐标正反算c语言,高斯投影坐标正反算
- 下一篇: c程序设计语言第五章,c程序设计语言第五