www.pudn.com > jtlab.rar > Cut.cpp


#include "stdafx.h" 
#include "math.h" 
#include "VCad.h" 
#include "VCadDoc.h" 
#include "VCadView.h" 
#include "MainFrm.h" 
#include "Entity.h" 
 
#include "CreateCmd.h" 
#include "iostream.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
//增添裁剪所用函数的说明 
bool isVisible(const Position p,const CRectangle rect); 
bool notVisible(const Position s,const Position e, const CRectangle rect); 
void setCode(Position p, byte&code, const CRectangle rect); 
Position farVisible(Position p1,Position p2,bool& yesno,CRectangle rect); 
//类CCreateCut 
CCreateCut::CCreateCut() 
	: m_begin1(0,0), m_end1(0,0),m_LeftTop(0,0), m_RightBottom(0,0) 
{ 
	m_nStep = 0; // 初始化操作步为 0 
    rl = FALSE;     
} 
 
CCreateCut::~CCreateCut() 
{ 
} 
 
int CCreateCut::GetType() 
{ 
	return ctCreateCut;  
} 
 
int	CCreateCut::OnLButtonDown(UINT nFlags, const Position& pos)  
{   bool vis; 
	if(rl)//r1为真时,输入被裁直线段,否则输入裁剪矩形。 
	{ 
	  m_nStep ++; // 每次单击鼠标左键时操作步加 1  
      switch(m_nStep) // 根据操作步执行相应的操作 
	  { 
		case 1:  
		{ 
			m_begin1 = m_end1 = pos; 
			::Prompt("请输入被裁直线的末端点:"); 
			break; 
		} 
		case 2: 
		{ 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 				 
       
            CLine*	pTempLine = new CLine(m_begin1,m_end1);  
            pTempLine->Draw(pDC, dmDrag); 
			delete pTempLine; 
			// 如果在按鼠标左键的过程中同时按下了Shift键, 
			//	那么根据鼠标单击位置绘制水平线或竖直线 
			if( nFlags & MK_SHIFT ){  
				double dx = pos.x - m_begin1.x; 
				double dy = pos.y - m_begin1.y; 
				if(fabs(dx) <= fabs(dy)) // 如果鼠标单击位置在X方向靠近起点 
					m_end1.Set(m_begin1.x, pos.y); // 那么终点的x坐标与起点的相同 
				else 
					m_end1.Set(pos.x,m_begin1.y); 
			} 
			else// 如果未按下Shift键, 则终点为鼠标单击位置			 
		    m_end1 = pos; 
			Bisection(m_begin1,m_end1,m_rect,vis); 
			if (vis) 
			{ CLine*	pNewLine = new CLine(m_begin1,m_end1);// 根据起点和终点创建直线 
			  pNewLine->Draw(pDC,dmNormal); // 绘制直线 
			  g_pDoc->m_EntityList.AddTail(pNewLine); // 将直线指针添加到图元链表 
			  g_pDoc->SetModifiedFlag(TRUE);// set modified flag ; 
			} 
			g_pView->ReleaseDC(pDC); // 释放设备环境指针			 
			m_nStep = 0;  // 将操作步重置为 0 
			::Prompt("请输入被裁直线的起点:"); 
			break; 
		} 
	  } 
	} 
	else 
	{//r1为假时,输入裁剪矩形。 
		m_nStep ++; // 每次单击鼠标左键时操作步加 1  
	switch(m_nStep) // 根据操作步执行相应的操作 
	{ 
		case 1:  
		{ 
			m_LeftTop = m_RightBottom = pos; 
			::Prompt("请输入主裁矩形的右下角点:") ; 
			break; 
		} 
		case 2: 
		{ 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针  
        	// 擦除在拖动状态时显示的橡皮线 
			CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom);  
			pTempRect->Draw(pDC, dmDrag); 
			delete pTempRect; 
			m_RightBottom = pos;						 
			CRectangle*	pRect = new CRectangle(m_LeftTop, m_RightBottom); // 根据两点创建矩形 
			m_rect=*pRect; 
			pRect->Draw(pDC, dmNormal); 
			g_pDoc->m_EntityList.AddTail(pRect); // 将指针添加到图元链表 
			g_pDoc->SetModifiedFlag(TRUE);// set modified flag ; 
			g_pView->ReleaseDC(pDC); // 释放设备环境指针 
			m_nStep = 0;   //将操作步重置为 0 
			rl=TRUE; 
		break;} 
		 
 
	} 
	} 
	return 0; 
} 
 
 
 
int	CCreateCut::OnMouseMove(UINT nFlags, const Position& pos) 
{ 
	if(rl) 
	{ 
	// 用一静态变量nPreRefresh记录进入OnMouseMove状态时的刷新次数 
	static	int nPreRefresh = g_nRefresh;  
	// 布尔变量bRefresh说明在OnMouseMove过程中视窗是否被刷新 
	BOOL	bRefresh = FALSE;  
	// nCurRefresh用于记录当前的刷新次数 
	int		nCurRefresh = g_nRefresh;  
	// 如果nCurRefresh和nPreRefresh不相等,说明视窗曾被刷新过 
	if(nCurRefresh != nPreRefresh){  
		bRefresh = TRUE; 
		nPreRefresh = nCurRefresh;  
	} 
	switch(m_nStep) 
	{ 
		case 0: 
			::Prompt("请输入被裁直线的起点:"); 
			break; 
		case 1: 
		{ 
			Position	prePos, curPos; 
			prePos = m_end1; // 获得鼠标所在的前一个位置 
			// 如果在按鼠标左键的过程中同时按下了Shift键, 
			//	那么根据鼠标单击位置绘制水平线或竖直线 
			if( nFlags & MK_SHIFT ){ 
				double dx = pos.x - m_begin1.x; 
				double dy = pos.y - m_begin1.y; 
				if(fabs(dx)>=fabs(dy)) 
					curPos.Set(pos.x,m_begin1.y); 
				else 
					curPos.Set(m_begin1.x, pos.y); 
			} 
			else 
				curPos = pos; 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
			// 创建临时对象擦除上一条橡皮线 
			CLine*	pTempLine1 = new CLine(m_begin1, prePos); 
			if(!bRefresh) // 当视窗没有被刷新时,重画原来的橡皮线使其被擦除 
				pTempLine1->Draw(pDC, dmDrag); 
			delete pTempLine1; 
			// 创建临时对象,根据当前位置绘制一条橡皮线 
			CLine*	pTempLine2 = new CLine(m_begin1, curPos);	 
			pTempLine2->Draw(pDC, dmDrag); 
			delete pTempLine2; 
			g_pView->ReleaseDC(pDC); // 释放设备环境指针			 
			m_end1 = curPos; // 将当前位置设置为直线终点,以备下一次鼠标移动时用 
			break; 
		} 
	} 
	} 
	else 
	{ 
			static	int nPreRefresh = g_nRefresh;  
	// 布尔变量bRefresh说明在OnMouseMove过程中视窗是否被刷新 
	BOOL	bRefresh = FALSE;  
	// nCurRefresh用于记录当前的刷新次数 
	int		nCurRefresh = g_nRefresh;  
	// 如果nCurRefresh和nPreRefresh不相等,说明视窗曾被刷新过 
	if(nCurRefresh != nPreRefresh){  
		bRefresh = TRUE; 
		nPreRefresh = nCurRefresh;  
	} 
	switch(m_nStep) 
	{ 
		case 0: 
			::Prompt("请输入主裁矩形的左上角点:") ; 
			break; 
		case 1: 
		{ 
			Position	prePos, curPos; 
			prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
			curPos = pos; 
			CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
			// 创建临时对象擦除上一条橡皮线 
			CRectangle*	pTempRect = new CRectangle(m_LeftTop, prePos); 
			if(!bRefresh) // 当视窗没有被刷新时,重画原来的橡皮线使其被擦除 
				pTempRect->Draw(pDC, dmDrag); 
			delete pTempRect; 
			// 创建临时对象,根据当前位置绘制一条橡皮线 
			CRectangle*	pTempRect2 = new CRectangle(m_LeftTop, curPos); 
			pTempRect2->Draw(pDC, dmDrag); 
			delete pTempRect2; 
			g_pView->ReleaseDC(pDC); // 释放设备环境指针			 
			m_RightBottom = curPos; // 将当前位置设置为直线终点,以备下一次鼠标移动时用 
			break; 
		} 
	} 
	} 
	return 0; 
} 
// 单击鼠标右键取消当前的操作 
int	CCreateCut::OnRButtonDown(UINT nFlags, const Position& pos)  
{if(rl) 
	{ 
	// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_end1; // 获得鼠标所在的前一个位置 
		CLine*	pTempLine = new CLine(m_begin1, prePos);  
		pTempLine->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempLine; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("请输入被裁直线的起点:"); 
} 
else 
{ 
	 
	// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
		CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom); 
		pTempRect->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempRect; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("请输入主裁矩形的左上角点:") ; 
} 
	return 0; 
} 
// 调用Cancel 函数取消本次操作 
 
int CCreateCut::Cancel() 
{if(rl) 
	{ 
	// 如果当前的操作步为 1 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_end1; // 获得鼠标所在的前一个位置 
		CLine*	pTempLine = new CLine(m_begin1, prePos);  
		pTempLine->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempLine; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("就绪"); // 等待提示新类型的命令操作 
} 
else 
{ 
 
	if(m_nStep == 1){  
		CDC*	pDC = g_pView->GetDC(); // 得到设备环境指针 
		Position	prePos = m_RightBottom; // 获得鼠标所在的前一个位置 
		CRectangle*	pTempRect = new CRectangle(m_LeftTop, m_RightBottom); 
		pTempRect->Draw(pDC, dmDrag); // 擦除上一次绘制的橡皮线 
		delete pTempRect; 
		g_pView->ReleaseDC(pDC); // 释放设备环境指针 
	} 
	m_nStep = 0; // 将操作步重置为 0  
	::Prompt("就绪"); // 等待提示新类型的命令操作 
} 
return 0; 
} 
 
//以下为中点裁剪算法,将自编的编码裁剪算法替换它 
void CCreateCut::Bisection(Position p1,Position p2,CRectangle rect,bool& vis1) 
{    
    Position s; 
	bool yn; 
	if(rect.m_LeftTop.x > rect.m_RightBottom.x) 
	{ s.x= rect.m_LeftTop.x ; 
      rect.m_LeftTop.x= rect.m_RightBottom.x ; 
      rect.m_RightBottom.x =s.x; 
    } 
	if(rect.m_LeftTop.y > rect.m_RightBottom.y) 
	{ s.y= rect.m_LeftTop.y ; 
      rect.m_LeftTop.y= rect.m_RightBottom.y ; 
      rect.m_RightBottom.y =s.y; 
    } 
	vis1=true; 
	if(notVisible(p1,p2,rect)) 
	{     
	    vis1=false; 
        return; 
    }     
	m_begin1=farVisible(p1,p2, yn, rect); 
	if(yn) 
    m_end1=farVisible(m_begin1,p1, yn, rect); 
    if(!yn)vis1=yn; 
    return; 
	 
    
} 
Position farVisible(Position p1,Position p2,bool& yesno,CRectangle rect) 
{    
    Position p11=p1,p22=p2,pm;	 
    yesno=true;     
	if(isVisible (p22,rect)) 
    { 
      return p22; 
	}     
	else 
    {     
		while(true) 
		{     
			pm.x=(p11.x+p22.x)/2; 
			pm.y=(p11.y+p22.y)/2; 
			if(sqrt((p11.x-pm.x)*(p11.x-pm.x)+ 
				(p11.y-pm.y)*(p11.y-pm.y))<1.e-4) 
			{ 
			  return pm;				 
			} 
			else 
			{ 
				if( notVisible(p22,pm,rect) ) 
				{ 
					p22=pm; 
				} 
				else 
				{ 
					p11=pm; 
				} 
			} 
		} 
	} 
     
    } 
    
 
bool isVisible(const Position p,const CRectangle rect) 
{     
	if((p.x>rect.m_LeftTop.x)&&(p.xrect.m_LeftTop.y)&&(p.yrect.m_RightBottom.x) 
			code=2; 
		if(p.yrect.m_RightBottom.y) 
			code+=8; 
		 
}