《Cohen-Sutherland直线裁剪算法.doc》由会员分享,可在线阅读,更多相关《Cohen-Sutherland直线裁剪算法.doc(37页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateCohen-Sutherland直线裁剪算法Cohen-Sutherland直线裁剪算法实验三 图形裁剪算法1. 实验目的:理解区域编码(Region Code,RC)设计Cohen-Sutherland直线裁剪算法编程实现Cohen-Sutherland直线裁剪算法2. 实验描述:设置裁剪窗口坐标为:wxl=250;wxr=850;wyb=250;wyt=450;裁
2、剪前如下图所示:裁剪后结果为:3.算法设计:Cohen-Sutherland 直线裁剪算法:假设裁剪窗口是标准矩形,由上(y=wyt)、下(y=wyb)、左(x=wxl)、右(x=wxr)四条边组成,如下图所示。 延长窗口四条边形成 9个区域。根据被裁剪直线的任一端点 P(x,y)所处的窗口区域位置,可以赋予一组4位二进制区域码C4C3C2C1。为了保证窗口内直线端点的编码为零,编码规则定义如下:第一位:若端点位于窗口之左侧,即 xwxr,则 C2=1,否则 C2=0。第三位:若端点位于窗口之下侧,即 ywyt,则 C4=1,否则 C4=0。裁剪步骤:1. 若直线的两个端点的区域编码都为零,即
3、 RC1|RC2=0(二者按位相或的结果为零,即 RC1=0 且RC2=0),说明直线两端点都在窗口内,应“简取”之。2. 若直线的两个端点的区域编码都不为零,即 RC1&RC20(二者按位相与的结果不为零,即 RC10且 RC20,即直线位于窗外的同一侧,说明直线的两个端点都在窗口外,应“简弃”之。3. 若直线既不满足“简取”也不满足“简弃”的条件,直线必然与窗口相交,需要计算直线与窗口边界的交点。交点将直线分为两段,其中一段完全位于窗口外,可“简弃”之。对另一段赋予交点处的区域编码,再次测试,再次求交,直至确定完全位于窗口内的直线段为止。4. 实现时,一般按固定顺序左(x=wxl)、右(x
4、=wxr)、下(y=wyb)、上(y=wyt)求解窗口与直线的交点。4.源程序:1)/TestView.hclass CTestView : public CView.protected:double Pointx2,Pointy2;/用户绘制的直线intwxl,wxr,wyb,wyt;/左上与右下CDCPicture;/内存(预存)DC,防止屏幕闪烁char m_i; /第一个点还是第二个点BOOLm_Attatch;BOOLm_Draw;unsigned int RC,RC0,RC1;.2) /TestView.cpp#define ROUND(a) int(a+0.5)#define L
5、EFT 1#define RIGHT 2#define BOTTOM 4#define TOP 8CTestView:CTestView()/窗口位置坐标wxl=250;wxr=850;wyb=250;wyt=450;m_Attatch=FALSE;m_i=0;m_Draw=FALSE;RC0=0;RC1=0;void CTestView:OnDraw(CDC* pDC)CTestDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data hereCRect Rect;GetClientR
6、ect(&Rect);/获得客户区的大小CBitmapBitmap,*pBitmap;Bitmap.LoadBitmap(IDB_BITMAP1);CDCMemDC;MemDC.CreateCompatibleDC(GetDC();pBitmap=MemDC.SelectObject(&Bitmap);MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),&Picture,0,0,SRCCOPY);MemDC.TextOut(wxl+wxr)/2,wyb-20,窗口);/窗口标题/绘制窗口和直线CPen Pen3,*pOldPen3;/定义3个像素宽度的画笔P
7、en3.CreatePen(PS_SOLID,3,RGB(0,0,0);pOldPen3=MemDC.SelectObject(&Pen3);MemDC.MoveTo(wxl,wyt);MemDC.LineTo(wxr,wyt);MemDC.LineTo(wxr,wyb);MemDC.LineTo(wxl,wyb);MemDC.LineTo(wxl,wyt);MemDC.SelectObject(pOldPen3);Pen3.DeleteObject();CPen Pen1,*pOldPen1;/定义1个像素宽度的画笔Pen1.CreatePen(PS_SOLID,1,RGB(0,255,25
8、5);pOldPen1=MemDC.SelectObject(&Pen1);if(m_i=1)MemDC.MoveTo(ROUND(Pointx0),ROUND(Pointy0);MemDC.LineTo(ROUND(Pointx1),ROUND(Pointy1);MemDC.SelectObject(pOldPen1);Pen1.DeleteObject();CDC *dc=GetDC();dc-BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);MemDC.SelectObject(pBitmap);void CTestVie
9、w:OnMENUClip()/裁剪菜单函数 Cohen();Invalidate(FALSE);unsigned int CTestView:EnCode(double LinePx,double LinePy)/端点编码函数/顺序左右下上RC=0;if(LinePxwxr)RC=RC | RIGHT;if(LinePywyt)RC=RC | TOP;return RC;void CTestView:Cohen()/CohenSutherland算法BOOL Change;double x,y;RC0=EnCode(Pointx0,Pointy0);RC1=EnCode(Pointx1,Poi
10、nty1);while(TRUE)Change=FALSE;if(0 = (RC0|RC1)/简取之return;else if(0!=(RC0 & RC1)/简弃之return;elseif(0=RC0)/如果P0点在窗口内,交换P0和P1,保证p0点在窗口外/交换点的坐标值double TPointx,TPointy;TPointx=Pointx0;TPointy=Pointy0;Pointx0=Pointx1;Pointy0=Pointy1;Pointx1=TPointx;Pointy1=TPointy;/交换点的编码值unsigned int TRC;TRC=RC0;RC0=RC1;R
11、C1=TRC;/按左、右、下、上的顺序裁剪if(RC0 & LEFT )/P0点位于窗口的左侧x=wxl;/求交点yy=Pointy0+(Pointy1-Pointy0)*(x-Pointx0)/(Pointx1-Pointx0);Pointx0=x;Pointy0=y;Change=TRUE;RC0=EnCode(Pointx0,Pointy0);RC1=EnCode(Pointx1,Pointy1);if(RC0 & RIGHT )/P0点位于窗口的右侧x=wxr;/求交点yy=Pointy0+(Pointy1-Pointy0)*(x-Pointx0)/(Pointx1-Pointx0);
12、Pointx0=x;Pointy0=y;Change=TRUE;RC0=EnCode(Pointx0,Pointy0);RC1=EnCode(Pointx1,Pointy1);if(RC0 & BOTTOM )/P0点位于窗口的下侧y=wyb;/求交点xx=Pointx0+(Pointx1-Pointx0)*(y-Pointy0)/(Pointy1-Pointy0);Pointx0=x;Pointy0=y;Change=TRUE;RC0=EnCode(Pointx0,Pointy0);RC1=EnCode(Pointx1,Pointy1);if(RC0 & TOP )/P0点位于窗口的上侧y=
13、wyt;/求交点xx=Pointx0+(Pointx1-Pointx0)*(y-Pointy0)/(Pointy1-Pointy0);Pointx0=x;Pointy0=y;Change=TRUE;RC0=EnCode(Pointx0,Pointy0);RC1=EnCode(Pointx1,Pointy1);if(FALSE=Change)return;void CTestView:OnMENUDrawLine()/绘制直线菜单函数 / TODO: Add your command handler code hereif(FALSE=m_Attatch)Picture.CreateCompat
14、ibleDC(GetDC();CBitmap*Bitmap,*pBitmap;Bitmap=new CBitmap;Bitmap-LoadBitmap(IDB_BITMAP1);pBitmap=Picture.SelectObject(Bitmap);m_Attatch=TRUE;m_Draw=TRUE;m_i=0;Invalidate(FALSE);AfxGetMainWnd()-SetWindowText(Cohen-Sutherland直线裁剪算法);/显示标题MessageBox(请使用鼠标在屏幕上绘制直线,然后点击裁剪按钮进行裁剪,提示,MB_OKCANCEL);void CTest
15、View:OnLButtonDown(UINT nFlags, CPoint point) /单击鼠标左键函数/ TODO: Add your message handler code here and/or call defaultif(TRUE=m_Draw)if(m_i2)Pointxm_i=point.x;Pointym_i=point.y;m_i+;CView:OnLButtonDown(nFlags, point);void CTestView:OnMouseMove(UINT nFlags, CPoint point) /鼠标移动函数/ TODO: Add your message handler code here and/or call defaultif(TRUE=m_Draw)if(m_i2)Pointxm_i=point.x;Pointym_i=point.y;Invalidate(FALSE);CView:OnMouseMove(nFlags, point);5.运行结果:(屏幕截图)序号检查项目分值提交时间得分1算法设计(40%)0-402算法的程序实现(40%)0-403运行结果(10%)0-104实验报告规范性 (10%)0-10 成 绩-