void CMyView::OnClipline() //线段裁剪消息处理函数 {
CDC* pDC=GetDC();
CPen newpen(PS_SOLID,1,RGB(0,255,0)); CPen *old=pDC->SelectObject(&newpen); if(flag!=1)
{MessageBox(\请先双击\警告!\(如图1-31所示) else {
float x,y,x1,x2,y1,y2; int i;
int code1,code2; RedrawWindow(); // 求两端点所在区号code for(i=0;i if(ptset[i].x if(ptset[i+1].x //线段与区域的相交情况(见图1-32) if(code1!=0&&code2!=0&&(code1&code2)==0) { if((LEFT&code1)!=0) //线段与左边界相交 { x=XL; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((RIGHT&code1)!=0) //线段与右边界相交 { x=XR; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((BOTTOM&code1)!=0) //线段与下边界相交 { y=YB; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset [i+1].y-ptset[i+1].y); } else if((TOP&code1)!=0) //线段与上边界相交 { y=YT; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset [i+1].y-ptset[i].y); } ptset[i].x=x; ptset[i].y=y; if((LEFT&code2)!=0) //线段与左边界相交 { x=XL; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((RIGHT&code2)!=0) //线段与右边界相交 { x=XR; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((BOTTOM&code2)!=0) //线段与下边界相交 { y=YB; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset [i+1].y-ptset[i+1].y); } else if((TOP&code2)!=0) //线段与上边界相交 { y=YT; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset [I+1].y-ptset[i].y); } ptset[i+1].x=x; ptset[i+1].y=y; pDC->MoveTo(ptset[i].x,ptset[i].y); pDC->LineTo(ptset[i+1].x,ptset[i+1].y); } if(code1==0&&code2==0) {pDC->MoveTo(ptset[i].x,ptset[i].y); pDC->LineTo(ptset[i+1].x,ptset[i+1].y); } if(code1==0&&code2!=0) { pDC->MoveTo(ptset[0].x,ptset[0].y); if((LEFT&code2)!=0) //线段与左边界相交 { x=XL; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((RIGHT&code2)!=0) //线段与右边界相交 { x=XR; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((BOTTOM&code2)!=0) //线段与下边界相交 { y=YB; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset [i+1].y-ptset[i+1].y); } else if((TOP&code2)!=0) //线段与上边界相交 { y=YT; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset [i+1].y-ptset[i].y); } ptset[i+1].x=x; ptset[i+1].y=y; pDC->LineTo(ptset[i+1].x,ptset[i+1].y); } if(code1!=0&&code2==0) { pDC->MoveTo(ptset[i+1].x,ptset[i+1].y); if((LEFT&code1)!=0) //线段与左边界相交 { x=XL; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((RIGHT&code1)!=0) //线段与右边界相交 { x=XR; y=ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset [i+1].x-ptset[i].x); } else if((BOTTOM&code1)!=0) //线段与下边界相交 { y=YB; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset [i+1].y-ptset[i+1].y); } else if((TOP&code1)!=0) //线段与上边界相交 { y=YT; x=ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset [i+1].y-ptset[i].y); } ptset[i].x=x; ptset[i].y=y; pDC->LineTo(ptset[i].x,ptset[i].y); } } } } //处理双击右键出现要裁剪的多边形(见图1-33) void CMyView::OnRButtonDblClk(UINT nFlags, CPoint point) { CDC* pDC=GetDC(); CPen newpen(PS_SOLID,1,RGB(255,0,0)); CPen *old=pDC->SelectObject(&newpen); pDC->MoveTo(ptset1[0]); for(int i=1;i<5;i++) { pDC->LineTo(ptset1[i]); } CView::OnRButtonDblClk(nFlags, point); } void CMyView::OnClippolygon() 多边形裁剪(见图1-34) { CDC* pDC=GetDC(); CPen newpen(PS_SOLID,1,RGB(0,255,0)); CPen *old=pDC->SelectObject(&newpen); if(flag!=1) {MessageBox(\请先双击鼠标右键\警告!\(见图1-35) else { int i,k; int code1,code2; int M=5; RedrawWindow(); // 求两端点所在区号code k=0; for(i=0;i if(ptset1[i].x