{
//对裁减矩形的四条边进行循环 for(i=0;i //对每条边进行循环 CPoint PointCross; int beforeI=BeforeIndex(i),afterI=AfterIndex(i); int afterrectNum = ((rectNum == 3)?0:rectNum+1); //判断是否跟线段相交 if(InterCross(Point[beforeI],Point[i],rectPoint[rectNum],rectPoint[afterrectNum],PointCross)) { if(PointCross==Point[i]) { myArray.Add(Point[i]);//交点在线段 裁剪图及填充图 上,直接添加点坐标在保存多边形的数组中 } 多边形裁剪 else bool CGraphics::CutRect(CRect rect) { if(PointCross==Point[beforeI]) CPoint rectPoint[4]; { rectPoint[0].x = rect.left; rectPoint[0].y = rect.top; if(IsInSquareRgn(rect,Point[i],rec rectPoint[1].x = rect.right; tNum)) rectPoint[1].y = rect.top; rectPoint[3].x = rect.left; myArray.Add(Point[i]);//判断是否可 rectPoint[3].y = rect.bottom; 视,若是,则添加点坐标 rectPoint[2].x = rect.right; } rectPoint[2].y = rect.bottom; else //获取裁减矩形的四个点的坐标,第一 { 个点为左上,第二个点为右上,第三个点为 右下,第四个点为左下 myArray.Add(PointCross);//跟线段相 int i; 交,但交点不在顶点上,添加交点坐标 CArray myArray.Add(Point[i]);//判断是否可视,若是,则添加点坐标 } } else if(IsInSquareRgn(rect,Point[i],rectNum)) myArray.Add(Point[i]);//线段不相交,但需判断是否可视,若是,则添加点坐标 } PointCount=myArray.GetSize(); if(Point) delete Point; Point = new CPoint[PointCount]; for(i=0;i Point[i]=myArray.GetAt(i);//重新赋予点坐标的值 myArray.RemoveAll(); } return true; } 直线段裁剪:(单独用的函数) #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8 #define XL 150 #define XR 350 #define YB 200 #define YT 300 #include #include encode(x,y,code) float x,y; int *code; { int c=0; if(x else if(x>XR)c=c|RIGHT; if(y return *code; } void M_lieCLip(float x1,float y1,float x2,float y2,float *x,float *y) { int code1,code2,code; float t=1,xx,yy; encode(x1,y1,&code1) ; encode(x2,y2,&code2); if (code1==0) { *x=x1; *y=y1;return;} while(code1&code2==0) { L1:xx=(x1+x2)/2; yy=(y1+y2)/2; encode(x,y,&code); if(abs((x2-xx)*(x2-xx)+(y2-yy)*(y2-yy)) {*x=xx;*y=yy;return;} if(code&code1!=0){x2=xx;y2=yy;} else{x1=xx;y1=yy;} } } void main() { float x1,y1,x2,y2,xx,yy,xxx,yyy,t; 圆的填充算法 int gdriver=DETECT,gmode; #include \ initgraph(&gdriver,&gmode,\#include \\#include { line(XL,YT,XL,YB); glBegin(GL_POINTS);//以点的形式 line(XR,YT,XR,YB); glVertex2i(xCoord, yCoord);//在(xCoord, x1=50;y1=150;x2=400;y2=300; yCoord)坐标下画点 setcolor(7); glEnd(); line(x1,y1,x2,y2); glFlush();//强制刷新 xx=0;yy=0;xxx=0;yyy=0; } M_lieCLip(x1,y1,x2,y2,&xx,&yy); M_lieCLip(x2,y2,xx,yy,&xxx,&yyyvoid Circle(GLint x,GLint y) ); { setcolor(11); int a=abs(x);//将x的绝对值赋给a line(xx,yy,xxx,yyy); int b=abs(y);//将y的绝对值赋给b int c=a*-1;//使c=a的相反数 getch(); int d=b*-1;//使d=b的相反数 closegraph(); draw(x, y); draw(y, x); draw(-x, y); draw(y, -x); } draw(x, -y); draw(-y, x); draw (-x, -y); draw(-y, -x);//按照圆的对称性以圆心为对称点将四个象限的圆周画出 for(int i=c;i<=a;i++) { for(int j=d;j<=b;j++) { draw(i,j); } }//以a,b,c,d为边界用点填充该圆 } //主函数 void BresenhamCircle(GLint r) { int d, d1, d2, direct; GLint x,y; x=0; y=r; d = 2*(1-r); while(y>=0) { Circle(x,y); if(d < 0) { d1 = 2* (d+ y) -1; if(d1 <=0) direct = 1; else direct = 2; } else { if( d > 0) { d2 = 2*(d-x)-1; if(d2 <= 0) direct = 2; else direct = 3; } else direct = 2; } switch(direct) { case 1: x++; d+=2*x + 1; break; case 2: x++; y--; d+=2*(x-y+1) + 1; break; case 3: y--; d+=-2*y + 1; break; } } } void RenderScene(void) { BresenhamCircle(50);//主函数调用 } //当窗口大小改变时由GLUT函数调用 void ChangeSize(GLsizei width, GLsizei Height) { GLfloat aspectRatio; if (Height == 0) { Height = 1; } glViewport(0, 0, width, Height);//指定视口矩形左下角 glMatrixMode(GL_PROJECTION);//指定当前矩阵,对投影矩阵应用随后的矩阵操glLoadIdentity();// 装载单位矩阵 aspectRatio = (GLfloat)width / (GLfloat) Height; if (width <= Height) { glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); } else { glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0); } glMatrixMode(GL_MODELVIEW);//指定当前矩阵,对模型视景矩阵堆栈应用随后的矩阵操作glLoadIdentity();// 装载单位矩阵 } //主程序入口 void main(void) { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//设置初始显示模式,指定单缓存窗口,指定 RGB 颜色模式的窗口 glutCreateWindow(\圆\创建窗口,窗口名称为“圆” glutDisplayFunc(RenderScene);//进行画图 glutReshapeFunc(ChangeSize);//重画回调函数 glutMainLoop();//进入GLUT事件处理循环,让所有的与“事件”有关的函数调用无限循环 } 指导老师意见: 成绩: 教师签名: 年 月 日