logo资料库

Cohen_Sutherland裁剪算法.doc

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
// LineClippingView.cpp // LineClippingView.cpp : implementation of the CLineClippingView class // #include "stdafx.h" #include "LineClipping.h" #include "LineClippingDoc.h" #include "LineClippingView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8 #define XL 100 #define XR 300 #define YT 100 #define YB 250 int encode (CPoint p); ///////////////////////////////////////////////////////////////////////////// // CLineClippingView IMPLEMENT_DYNCREATE(CLineClippingView, CView) BEGIN_MESSAGE_MAP(CLineClippingView, CView) //{{AFX_MSG_MAP(CLineClippingView) ON_WM_LBUTTONDBLCLK() ON_COMMAND(ID_COHEN_SUTHERLAND, OnCohenSutherland) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() /////////////////////////////////////////////////////////////////////////////
// CLineClippingView construction/destruction CLineClippingView::CLineClippingView() { // TODO: add construction code here } CLineClippingView::~CLineClippingView() { } BOOL CLineClippingView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CLineClippingView drawing void CLineClippingView::OnDraw(CDC* pDC) { CLineClippingDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CPen newpen(PS_SOLID,1,RGB(255,0,0)); CPen *old=pDC->SelectObject(&newpen); pDC->Rectangle(CRect(XL,YT,XR,YB));//剪切窗口 ptset[0]=CPoint(120,150); ptset[1]=CPoint(170,110); ptset[2]=CPoint(0,190); ptset[3]=CPoint(350,150); ptset[4]=CPoint(0,250); ptset[5]=CPoint(150,230); ptset[6]=CPoint(200,50); ptset[7]=CPoint(120,150); // ptset[6]=CPoint(20,20); // ptset[7]=CPoint(80,260); /* ptset1[0]=CPoint(20,150); ptset1[1]=CPoint(170,110); ptset1[2]=CPoint(250,150); ptset1[3]=CPoint(200,230); ptset1[4]=CPoint(20,150);*/
pDC->TextOut(0,20,"双击鼠标左键,出现要剪切的线段"); pDC->SelectObject(old); } ///////////////////////////////////////////////////////////////////////////// // CLineClippingView printing BOOL CLineClippingView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CLineClippingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CLineClippingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CLineClippingView diagnostics #ifdef _DEBUG void CLineClippingView::AssertValid() const { CView::AssertValid(); } void CLineClippingView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CLineClippingDoc* CLineClippingView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLineClippingDoc))); return (CLineClippingDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CLineClippingView message handlers void CLineClippingView::OnLButtonDblClk(UINT nFlags, CPoint point)
{ } CDC* pDC=GetDC(); CPen newpen(PS_SOLID,1,RGB(255,0,0)); CPen *old=pDC->SelectObject(&newpen); //绘制N条直线段; for(int i=0;iMoveTo(ptset[i]); pDC->LineTo(ptset[i+1]); i++; } CView::OnLButtonDblClk(nFlags, point); void CLineClippingView::OnCohenSutherland() { // TODO: Add your command handler code here CDC* pDC=GetDC(); CPen newpen(PS_SOLID,1,RGB(255,0,0)); CPen *old=pDC->SelectObject(&newpen); //XL,YT,XR,YB RedrawWindow(); for (int i=0; i
//y = (float)ptset[i+1].y; } // // (float)((ptset[i+1].y-ptset[i].y)/(ptset[i+1].x-ptset[i].x)); if(ptset[i].x != ptset[i+1].x) k = if ((code & LEFT) !=0) { Y = (float)ptset[i].y + (ptset[i+1].y-ptset[i].y) * (XL - ptset[i].x)/(ptset[i+1].x-ptset[i].x) ; X = XL; } else if ((code & RIGHT) !=0) { Y = (float)ptset[i].y + (ptset[i+1].y-ptset[i].y) * (XR - ptset[i].x)/(ptset[i+1].x-ptset[i].x); X = XR; } else if ((code & BOTTOM) !=0) { X = (float)ptset[i].x + (YB - ptset[i].y) * (ptset[i+1].x-ptset[i].x)/ (ptset[i+1].y-ptset[i].y); Y = YB; } else if ((code & TOP) !=0) { (ptset[i+1].x-ptset[i].x)/ (ptset[i+1].y-ptset[i].y); X = (float)ptset[i].x + (YT - ptset[i].y) * Y = YT; } if(code == code1) { ptset[i].x = (long)X; ptset[i].y = (long)Y; code1=encode(ptset[i]);} else { ptset[i+1].x = (long)X; ptset[i+1].y = (long)Y; code2=encode(ptset[i+1]);} } } if ((code1 & code2) != 0) continue; pDC->MoveTo(ptset[i]); pDC->LineTo(ptset[i+1]); } } int encode (CPoint p) {
int c=0; if(p.xXR) c|=RIGHT; if(p.y>YB) c|=BOTTOM; if(p.y
分享到:
收藏