计算机图形学-实验报告三-图形裁剪算法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

图形裁剪算法1.实验目的:理解区域编码设计直线裁剪算法编程实现直线裁剪算法2.实验描述:设置裁剪窗口坐标为:wxl=250;wxr=850;wyb=250;wyt=450;裁剪前如下图所示:裁剪后结果为:3.算法设计:直线裁剪算法:假设裁剪窗口是标准矩形,由上(y=wyt)、下(y=wyb)、左(x=wxl)、右(x=wxr)四条边组成,如下图所示。延长窗口四条边形成9个区域。根据被裁剪直线的任一端点P(x,y)所处的窗口区域位置,可以赋予一组4位二进制区域码C4C3C2C1。编码定义规则:第一位C1:若端点位于窗口之左侧,即XWxl,则C1=1,否则C1=0。第二位C2:若端点位于窗口之右侧,即XWxr,则C2=1,否则C2=0。第三位C3:若端点位于窗口之下侧,即YWyb,则C3=1,否则C3=0。第四位C4:若端点位于窗口之上侧,即YWyt,则C4=1,否则C4=0。裁剪步骤:1.若直线的两个端点的区域编码都为0,即RC1|RC2=0(二者按位相或的结果为0,即RC1=0且RC2=0),说明直线两端点都在窗口内,应“简取”。2.若直线的两个端点的区域编码都不为0,即RC1&RC2≠0(二者按位相与的结果不为0,即RC1≠0且RC2≠0,即直线位于窗外的同一侧,说明直线的两个端点都在窗口外,应“简弃”。3.若直线既不满足“简取”也不满足“简弃”的条件,直线段必然与窗口相交,需要计算直线与窗口边界的交点。交点将直线分为两段,其中一段完全位于窗口外,可“简弃”。对另一段赋予交点处的区域编码,再次测试,再次求交,直至确定完全位于窗口内的直线段为止。4.实现时,一般按固定顺序左(x=wxl)、右(x=wxr)、下(y=wyb)、上(y=wyt)求解窗口与直线的交点。4.源程序://1)TestView.hclassCTestView:publicCView{…….protected:doublePointx[2],Pointy[2];//用户绘制的直线intwxl,wxr,wyb,wyt;//左上与右下CDCPicture;//内存(预存)DC,防止屏幕闪烁charm_i;//第一个点还是第二个点BOOLm_Attatch;BOOLm_Draw;unsignedintRC,RC0,RC1;……..}2)//TestView.cppCTestView::CTestView(){//窗口位置坐标wxl=250;wxr=850;wyb=250;wyt=450;m_Attatch=FALSE;m_i=0;m_Draw=FALSE;RC0=0;RC1=0;}voidCTestView::OnDraw(CDC*pDC){CTestDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//装载位图CRectRect;GetClientRect(&Rect);//获得客户区的大小CBitmapBitmap,*pBitmap;Bitmap.LoadBitmap(IDB_BITMAP);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,窗口);//窗口标题//绘制窗口和直线CPenPen3,*pOldPen3;//定义3个像素宽度的画笔Pen3.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();CPenPen1,*pOldPen1;//定义1个像素宽度的画笔Pen1.CreatePen(PS_SOLID,1,RGB(0,0,255));pOldPen1=MemDC.SelectObject(&Pen1);if(m_i=1){MemDC.MoveTo(ROUND(Pointx[0]),ROUND(Pointy[0]));MemDC.LineTo(ROUND(Pointx[1]),ROUND(Pointy[1]));}MemDC.SelectObject(pOldPen1);Pen1.DeleteObject();CDC*dc=GetDC();dc-BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);MemDC.SelectObject(pBitmap);}voidCTestView::OnMENUClip()//裁剪菜单函数{Cohen();Invalidate(FALSE);}unsignedintCTestView::EnCode(doubleLinePx,doubleLinePy)//端点编码函数{//顺序左右下上RC=0;if(LinePxwxl){RC=RC|LEFT;}if(LinePxwxr){RC=RC|RIGHT;}if(LinePywyb){RC=RC|BOTTOM;}if(LinePywyt){RC=RC|TOP;}returnRC;}voidCTestView::OnMENUDrawLine()//绘制直线菜单函数{//TODO:Addyourcommandhandlercodehereif(FALSE==m_Attatch){Picture.CreateCompatibleDC(GetDC());CBitmap*Bitmap,*pBitmap;Bitmap=newCBitmap;Bitmap-LoadBitmap(IDB_BITMAP);pBitmap=Picture.SelectObject(Bitmap);m_Attatch=TRUE;}m_Draw=TRUE;m_i=0;Invalidate(FALSE);AfxGetMainWnd()-SetWindowText(案例10:Cohen-Sutherland直线裁剪算法);//显示标题MessageBox(请使用鼠标在屏幕上绘制直线,然后点击裁剪按钮进行裁剪,提示,MB_OKCANCEL);}voidCTestView::OnLButtonDown(UINTnFlags,CPointpoint)//单击鼠标左键函数{//TODO:Addyourmessagehandlercodehereand/orcalldefaultif(TRUE==m_Draw){if(m_i2){Pointx[m_i]=point.x;Pointy[m_i]=point.y;m_i++;}}CView::OnLButtonDown(nFlags,point);}voidCTestView::OnMouseMove(UINTnFlags,CPointpoint)//鼠标移动函数{//TODO:Addyourmessagehandlercodehereand/orcalldefaultif(TRUE==m_Draw){if(m_i2){Pointx[m_i]=point.x;Pointy[m_i]=point.y;Invalidate(FALSE);}}CView::OnMouseMove(nFlags,point);}voidCTestView::Cohen()//Cohen-Sutherland算法{BOOLChange;doublex,y;RC0=EnCode(Pointx[0],Pointy[0]);RC1=EnCode(Pointx[1],Pointy[1]);while(TRUE){Change=FALSE;if(0==(RC0|RC1)){//简取之return;}elseif(0!=(RC0&RC1)){//简弃之return;}else{if(0==RC0)//如果P0点在窗口内,交换P0和P1,保证p0点在窗口外{//交换点的坐标值doubleTPointx,TPointy;TPointx=Pointx[0];TPointy=Pointy[0];Pointx[0]=Pointx[1];Pointy[0]=Pointy[1];Pointx[1]=TPointx;Pointy[1]=TPointy;//交换点的编码值unsignedintTRC;TRC=RC0;RC0=RC1;RC1=TRC;}//按左、右、下、上的顺序裁剪if(RC0&LEFT)//P0点位于窗口的左侧{x=wxl;//求交点yy=Pointy[0]+(Pointy[1]-Pointy[0])*(x-Pointx[0])/(Pointx[1]-Pointx[0]);Pointx[0]=x;Pointy[0]=y;Change=TRUE;RC0=EnCode(Pointx[0],Pointy[0]);RC1=EnCode(Pointx[1],Pointy[1]);}if(RC0&RIGHT)//P0点位于窗口的右侧{x=wxr;//求交点yy=Pointy[0]+(Pointy[1]-Pointy[0])*(x-Pointx[0])/(Pointx[1]-Pointx[0]);Pointx[0]=x;Pointy[0]=y;Change=TRUE;RC0=EnCode(Pointx[0],Pointy[0]);RC1=EnCode(Pointx[1],Pointy[1]);}if(RC0&BOTTOM)//P0点位于窗口的下侧{y=wyb;//求交点xx=Pointx[0]+(Pointx[1]-Pointx[0])*(y-Pointy[0])/(Pointy[1]-Pointy[0]);Pointx[0]=x;Pointy[0]=y;Change=TRUE;RC0=EnCode(Pointx[0],Pointy[0]);RC1=EnCode(Pointx[1],Pointy[1]);}if(RC0&TOP)//P0点位于窗口的上侧{y=wyt;//求交点xx=Pointx[0]+(Pointx[1]-Pointx[0])*(y-Pointy[0])/(Pointy[1]-Pointy[0]);Pointx[0]=x;Pointy[0]=y;Change=TRUE;RC0=EnCode(Pointx[0],Pointy[0]);RC1=EnCode(Pointx[1],Pointy[1]);}if(FALSE==Change){return;}}}}5.运行结果:

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功