cs 线段裁剪算法 MFC下实现

时间:2013-07-04 07:14:09
【文件属性】:
文件名称:cs 线段裁剪算法 MFC下实现
文件大小:1.86MB
文件格式:RAR
更新时间:2013-07-04 07:14:09
cs 线段裁剪算法 MFC下实现 // 线段裁剪CSView.cpp : implementation of the CCSView class // #include "stdafx.h" #include "线段裁剪CS.h" #include "线段裁剪CSDoc.h" #include "线段裁剪CSView.h" #define left 200 //默认图形窗口 #define right 400 #define bottom 100 #define top 300 #define l 1 //区域编码 #define r 2 #define b 4 #define t 8 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCSView IMPLEMENT_DYNCREATE(CCSView, CView) BEGIN_MESSAGE_MAP(CCSView, CView) //{{AFX_MSG_MAP(CCSView) ON_WM_LBUTTONDOWN() ON_WM_RBUTTONDOWN() //}}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() ///////////////////////////////////////////////////////////////////////////// // CCSView construction/destruction CCSView::CCSView() { // TODO: add construction code here } CCSView::~CCSView() { } BOOL CCSView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CCSView drawing void CCSView::OnDraw(CDC* pDC) { CCSDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->TextOut(150,1," 单击左键定线段起点,右击定线段终点"); pDC->TextOut(150,20,"线段的裁剪结果会自动显示"); // TODO: add draw code for native data here pDC->Rectangle(left,top,right,bottom); } ///////////////////////////////////////////////////////////////////////////// // CCSView printing BOOL CCSView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CCSView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CCSView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CCSView diagnostics #ifdef _DEBUG void CCSView::AssertValid() const { CView::AssertValid(); } void CCSView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CCSDoc* CCSView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCSDoc))); return (CCSDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CCSView message handlers void CCSView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default origin=point; CView::OnLButtonDown(nFlags, point); } void encode(float x,float y,int &code) { int c=0; if(xright)c=c|r; if(ytop)c=c|t; code=c; } void CCSView::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default float x1,y1,x2,y2; x1=origin.x; y1=origin.y; x2=point.x; y2=point.y;//获取初始化线段的起点和终点 CsLine(x1,x2,y1,y2); CView::OnRButtonDown(nFlags, point); } int CCSView::CsLine(float x1, float x2, float y1, float y2) { CDC *pDC=GetDC(); int code1,code2,code; float x,y; encode(x1,y1,code1); encode(x2,y2,code2); while(code1!=0||code2!=0) { if(code1&code2)return 1;//线段在窗口外,返回 code=code1; if(code1==0)code=code2; if(l&code) { x=left; y=y1+(y2-y1)*(left-x1)/(x2-x1); } else if(r&code) { x=right; y=y1+(y2-y1)*(right-x1)/(x2-x1); } else if(b&code) { y=bottom; x=x1+(x2-x1)*(bottom-y1)/(y2-y1); } else if(t&code) { y=top; x=x1+(x2-x1)*(top-y1)/(y2-y1); } if(code==code1) { x1=x;y1=y;encode(x,y,code1); } else { x2=x;y2=y;encode(x,y,code2); } } pDC->MoveTo(x1,y1); pDC->LineTo(x2,y2); ReleaseDC(pDC); return 1; }
【文件预览】:
线段裁剪CS
----线段裁剪CS.rc(10KB)
----Resource.h(494B)
----res()
--------线段裁剪CS.ico(1KB)
--------Toolbar.bmp(1KB)
--------线段裁剪CS.rc2(402B)
--------线段裁剪CSDoc.ico(1KB)
----线段裁剪CS.plg(254B)
----线段裁剪CSDoc.cpp(2KB)
----线段裁剪CS.APS(28KB)
----线段裁剪CSDoc.h(1KB)
----线段裁剪CSView.h(2KB)
----线段裁剪CS.dsw(545B)
----线段裁剪CS.ncb(81KB)
----线段裁剪CSView.cpp(4KB)
----线段裁剪CS.cpp(4KB)
----线段裁剪CS.h(1KB)
----StdAfx.cpp(212B)
----MainFrm.cpp(2KB)
----线段裁剪CS.opt(53KB)
----MainFrm.h(2KB)
----StdAfx.h(1KB)
----线段裁剪CS.clw(2KB)
----Debug()
--------StdAfx.obj(104KB)
--------线段裁剪CS.res(7KB)
--------线段裁剪CSView.obj(26KB)
--------vc60.idb(209KB)
--------线段裁剪CSDoc.obj(15KB)
--------vc60.pdb(356KB)
--------线段裁剪CS.exe(116KB)
--------APPMODUL.obj(5KB)
--------线段裁剪CS.obj(23KB)
--------MainFrm.obj(20KB)
--------线段裁剪CS.ilk(321KB)
--------线段裁剪CS.pch(5.24MB)
--------线段裁剪CS.pdb(457KB)
----线段裁剪CS.dsp(5KB)
----ReadMe.txt(4KB)

网友评论

  • 可以 有帮助
  • 还不错 可以自己进行一些改进
  • 可以运行,不错的入门参考资料
  • 我试试了可以用那 哈哈哈哈