计算机图形学课程设计(共11页).doc
精选优质文档-倾情为你奉上计算机图形学课程设计 运行环境turboc2.0 如果有疑问请发邮件到xuyuanfei本程序完成了橡皮筋直线,橡皮筋圆,橡皮筋矩形框,多边形裁剪和3阶B曲线的作图,且有比较友好的界面#ifndef _GRAPHICS_C_#define _GRAPHICS_C_#endif#define _MOUSE#define CIRCLE 1#define LINE 2#define RECTANGLE 3#include "stdio.h"#include "string.h"#include"graphics.h"#include"math.h"int flag,num;int x,y,oldx,oldy,px,py;int a4,b4;char string120;char zuobiao20;#include <dos.h>union REGS inreg,outreg;struct SREGS sr;void _mytreat(int,int,int,int);void _write(char*);void mycross(int x,int y)setcolor(15);outtextxy(500,468,zuobiao);sprintf(zuobiao,"x=%d,y=%d",x,y);setcolor(8);outtextxy(500,468,zuobiao);line(x-3,y-3,x+3,y+3);line(x+3,y-3,x-3,y+3);void mycircle(double x,double y,double r)double i;for(i=0.0;i<360.0;i=i+4)line(x+r*cos(i+1)/360*3.1415*2),y+r*sin(i+1)/360*3.1415*2),x+r*cos(i+4.0)/360*3.1415*2),y+r*sin(i+4.0)/360*3.1415*2);void _Bezer(int a3,int b3,int m)int k,n,a0,a1,a2,a3,b0,b1,b2,b3,x,y;double i,t;setwritemode(0);a0=(am%4+4*am%4+1+am%4+2)/6;a1=(am%4+2-am%4)/2;a2=(am%4-2*am%4+1+am%4+2)/2;a3=-(am%4-3*am%4+1+3*am%4+2-am%4+3)/6;b0=(bm%4+4*bm%4+1+bm%4+2)/6;b1=(bm%4+2-bm%4)/2;b2=(bm%4-2*bm%4+1+bm%4+2)/2;b3=-(bm%4-3*bm%4+1+3*bm%4+2-bm%4+3)/6;for(i=0;i<3;i+=0.1)t=i/3;x=a0+a1*t+a2*t*t+a3*t*t*t;y=b0+b1*t+b2*t*t+b3*t*t*t;if(i=0)moveto(x,y);lineto(x,y);setwritemode(1);void DisplayMouse()inreg.x.ax=1;int86(0x33,&inreg,&outreg);void CloseMouse()inreg.x.ax=2;int86(0x33,&inreg,&outreg);union REGS DisplayMouseStatus()inreg.x.ax=3;int86(0x33,&inreg,&outreg);return outreg;void meun(int x,int y,char *str)setcolor(8);outtextxy(x+5,28,str);setcolor(15);line(x,22,x,40);line(x,22,y,22);setcolor(8);line(x,40,y,40);line(y,22,y,40);void drawwindows()setbkcolor(7); /*7*/setcolor(8);rectangle(0,20,640,460);rectangle(0,0,640,20);setfillstyle(1,1);floodfill(1,1,8);rectangle(0,460,640,480);setfillstyle(1,15);floodfill(1,461,8);setcolor(RED);outtextxy(10,468,"Finished By XuYuanFei.");setcolor(15);line(0,41,640,41);line(0,459,640,459);setcolor(8);line(0,42,640,42);setcolor(15);rectangle(610,3,630,17);setfillstyle(1,7);floodfill(611,4,15);setcolor(8);line(610,17,630,17);line(630,3,630,17);line(611,4,628,16);line(611,16,628,4);rectangle(2,44,635,459);setcolor(15);rectangle(3,45,634,458);line(611,3,628,15);line(611,15,628,3);meun(5,55,"line");meun(65,165,"rectangle");meun(175,250,"circle");meun(260,330,"Bezer");meun(340,400,"Cut");void GetMouseXY(int *x,int *y)DisplayMouse();outreg=DisplayMouseStatus();*x=outreg.x.cx;*y=outreg.x.dx;int _cut()double a1120,b1120,a2120,b2120;int x1,y1,x2,y2,t,t1=0,t2,mark=0,w=0,i;num=0;flag=CIRCLE;while(outreg.x.bx=1)GetMouseXY(&x,&y);while(1)GetMouseXY(&x,&y);if(oldx!=x|oldy!=y)if(y<460)mycross(oldx,oldy);mycross(x,y);if(w=1)rectangle(x1,y1,oldx,oldy); rectangle(x1,y1,x,y);oldx=x;oldy=y;if(outreg.x.bx=1)if(y>43&&y<460)if(mark=0)a1num=x;b1num=y;_write("Draw duobianxing; wangcheng R_button");if(num>0)line(a1num-1,b1num-1,a1num,b1num);num+;if(mark=1)if(w=0)x1=x,y1=y;w=1;num=0;else if(w=1)x2=x,y2=y;rectangle(x1,y1,x2,y2);for(i=1;i<=t1;i+)line(a1i-1,b1i-1,a1i,b1i);line(a1t1,b1t1,a10,b10);if(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b1i);line(a1t1,b1t1,a10,b10);/*-*/_write("Quit:R_button");while(outreg.x.bx=1)GetMouseXY(&x,&y);if(outreg.x.bx=2)if(mark=0)mark=1;line(a1num-1,b1num-1,a10,b10);t1=num-1;_write("Draw rectangle to cut the doubianxing");else if(mark=1)_write("welcome to use my system");return 0;while(outreg.x.bx=2)GetMouseXY(&x,&y);void handlevent(int *flag,int x)num=0;if(x>170&&x<250) *flag=CIRCLE; _write("Draw circle");else if(x>5&&x<55) *flag=LINE; _write("Draw line");else if(x>65&&x<165) *flag=RECTANGLE; _write("Draw rectangle");else if(x>260&&x<330) _write("Draw bezer");Bezer(); else if(x>340&&x<400) _write("Draw doubianxing to be cut");_cut();int Bezer()num=1;while(1)GetMouseXY(&x,&y);if(oldx!=x|oldy!=y)if(y<460)mycross(oldx,oldy);mycross(x,y);oldx=x;oldy=y;if(outreg.x.bx=1)if(y>43&&y<460)_write("Quit: R_button");if(num=1)a0=x;b0=y;else if(num<4)anum-1=x;bnum-1=y;line(anum-2,bnum-2,anum-1,bnum-1);else if(num=4)anum-1=x;bnum-1=y;line(anum-2,bnum-2,anum-1,bnum-1);_Bezer(a,b,4);else if(num>4)line(a0,b0,a1,b1);a0=a1; b0=b1;a1=a2; b1=b2;a2=a3; b2=b3;a3=x; b3=y;line(a2,b2,a3,b3);_Bezer(a,b,4);num+;while(outreg.x.bx=1)GetMouseXY(&x,&y);if(outreg.x.bx=2)line(a0,b0,a1,b1);line(a1,b1,a2,b2);line(a2,b2,a3,b3);_write("welcome to use my system");return 0;void _mytreat(int oldx,int oldy,int x,int y)if(num=1)if(flag=LINE)line(px,py,oldx,oldy);line(px,py,x,y);if(flag=CIRCLE)mycircle(px,py,sqrt(double)(px-oldx)*(px-oldx)+(py-oldy)*(py-oldy);mycircle(px,py,sqrt(double)(px-x)*(px-x)+(py-y)*(py-y);if(flag=RECTANGLE)rectangle(px,py,oldx,oldy);rectangle(px,py,x,y);void mytreat(int x,int y)if(num=0)px=x;py=y;num=1;elsenum=0;void opengraph()int gdriver,gmode;gdriver=DETECT;initgraph(&gdriver,&gmode,"");void _write(char *str)setcolor(15);outtextxy(200,468,string);setcolor(8);strcpy(string,str);outtextxy(200,468,string);int main()flag=num=0;opengraph();drawwindows();DisplayMouse();setwritemode(1);strcpy(string,"welcome to use my system");outtextxy(200,468,string);GetMouseXY(&oldx,&oldy);mycross(oldx,oldy);while(1)GetMouseXY(&x,&y);if(oldx!=x|oldy!=y)if(y<460)mycross(oldx,oldy);mycross(x,y);_mytreat(oldx,oldy,x,y);oldx=x;oldy=y;if(outreg.x.bx=1)if(y<43)if(y>20)handlevent(&flag,x);else if(x>610&&x<630)exit(0);else if(y>43&&y<460)mytreat(x,y);while(outreg.x.bx=1)GetMouseXY(&x,&y);closegraph();专心-专注-专业