⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dfdsview.cpp

📁 直线裁剪
💻 CPP
字号:
// dfdsView.cpp : implementation of the CDfdsView class
//

#include "stdafx.h"
#include "dfds.h"

#include "dfdsDoc.h"
#include "dfdsView.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

/////////////////////////////////////////////////////////////////////////////
// CDfdsView

IMPLEMENT_DYNCREATE(CDfdsView, CView)

BEGIN_MESSAGE_MAP(CDfdsView, CView)
	//{{AFX_MSG_MAP(CDfdsView)
	ON_COMMAND(ID_dd, Ondd)
	ON_WM_LBUTTONDBLCLK()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDfdsView construction/destruction

CDfdsView::CDfdsView()
{
	// TODO: add construction code here

}

CDfdsView::~CDfdsView()
{
}

BOOL CDfdsView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDfdsView drawing

void CDfdsView::OnDraw(CDC* pDC)
{
	CDfdsDoc* 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(130,200);
	pDC->TextOut(0,0,"双击鼠标左键出现要裁剪的直线");

	


}

/////////////////////////////////////////////////////////////////////////////
// CDfdsView printing

BOOL CDfdsView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDfdsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDfdsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDfdsView diagnostics

#ifdef _DEBUG
void CDfdsView::AssertValid() const
{
	CView::AssertValid();
}

void CDfdsView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDfdsDoc* CDfdsView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDfdsDoc)));
	return (CDfdsDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDfdsView message handlers

void CDfdsView::Ondd() 
{
	// TODO: Add your command handler code here
CDC* pDC=GetDC();
	CPen newpen(PS_SOLID,1,RGB(255,0,0));
	CPen *old=pDC->SelectObject(&newpen);

	float x,y;
	int i;
	int code1,code2;
			RedrawWindow();
	// 求两端点所在区号code
	for(i=0;i<N;i++,i++)
	{
		int c=0;
		if(ptset[i].x<XL) c=c|LEFT;
		else if (ptset[i].x>XR)c=c|RIGHT;
		if(ptset[i].y>YB) c=c|BOTTOM;
		else if(ptset[i].y<YT) c=c|TOP;
		code1=c;
		 c=0;
		if(ptset[i+1].x<XL) c=c|LEFT;
		else if(ptset[i+1].x>XR) c=c|RIGHT;
		if(ptset[i+1].y>YB) c=c|BOTTOM;
		else if(ptset[i+1].y<YT) c=c|TOP;
		code2=c;
	//线段与区域的相交情况
	if(code1!=0&&code2!=0&&(code1&code2)==0)
	{	
		if((LEFT&code1)!=0) 	        //线段与左边界相交
		{
		x=XL;
		y=(float)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=(float)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=(float)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=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
		}
	//	x1=x;
	//	y1=y;
		ptset[i].x=(long)x;
		ptset[i].y=(long)y;	
		if((LEFT&code2)!=0) 	        //线段与左边界相交
		{
		x=XL;
		y=(float)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=(float)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=(float)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=(float)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=(long)x;
		ptset[i+1].y=(long)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=(float)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=(float)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=(float)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=(float)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=(long)x;
		ptset[i+1].y=(long)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=(float)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=(float)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=(float)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=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y);
		}
		
		ptset[i].x=(long)x;
		ptset[i].y=(long)y;
		pDC->LineTo(ptset[i].x,ptset[i].y);
	}
	}
		




	}
	


void CDfdsView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC=GetDC();
	CPen newpen(PS_SOLID,1,RGB(255,0,0));
	CPen *old=pDC->SelectObject(&newpen);
	for(int i=0;i<N;i++)
	{
		pDC->MoveTo(ptset[i]);
	pDC->LineTo(ptset[i+1]);//
	i++;
}
pDC->SelectObject(old);

	
	CView::OnLButtonDblClk(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -