www.pudn.com > my2cadtest07261930.rar > SuperPoint.cpp, change:2006-07-17,size:9011b


// 静态模型
#include "stdafx.h"
#include "SuperPoint.h" 
#include "cxmatrix.h"
POINT CSuperPoint::GetPoint() const
{ 
	return m_Point;
	
}

void CSuperPoint::SetPoint(POINT point)
{ 
	m_Point = point;
	
}

void CSuperPoint::MoveTo(POINT direcPoint)
{ 
	m_Point = direcPoint;
	
} 
 
void CSuperPoint::ClearMark(HDC &hdc) 
{ 
	HPEN newPen; 
	if( m_status == OVER )		//没有任何状态 
	{ 
		m_status = NOTHING; 
		newPen = CreatePen(0,0,RGB(0,0,255) ^ GetBkColor(hdc) ); 
		//	m_status = OVER; 
		int oldRop2 = SetROP2(hdc,R2_XORPEN); 
		HPEN oldPen = (HPEN)SelectObject(hdc,newPen); 
		HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,GetStockObject(NULL_BRUSH)); 
	 
		::Rectangle(hdc,m_Point.x - 4,m_Point.y - 4,  
			m_Point.x + 4, m_Point.y + 4 ); 
			 
		SelectObject(hdc,oldPen); 
		SelectObject(hdc,oldBrush); 
		 
		DeleteObject(newPen); 
		 
		DeleteObject(oldPen); 
		DeleteObject(oldBrush); 
		 
		SetROP2(hdc,oldRop2); 
		 
	} 
 
	 
	 
 
}

//  功    能: 在通过颜色标记本点
//  输入参数: hWnd 点所在的窗口  markflag 打上何种色彩的标记
//  返 回 值: 无 
void CSuperPoint::SetMark(HDC& hdc/*,MARK_FLAG markflag*/ ) 
{ 
	 
	HPEN newPen; 
	 
	if( m_status == NOTHING )		//没有任何状态 
	{ 
		m_status = OVER; 
		newPen = CreatePen(0,0,RGB(0,0,255) ^ GetBkColor(hdc) ); 
		//	m_status = OVER; 
		int oldRop2 = SetROP2(hdc,R2_XORPEN); 
		HPEN oldPen = (HPEN)SelectObject(hdc,newPen); 
		HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,GetStockObject(NULL_BRUSH)); 
	 
		::Rectangle(hdc,m_Point.x - 4,m_Point.y - 4,  
			m_Point.x + 4, m_Point.y + 4 ); 
			 
		SelectObject(hdc,oldPen); 
		SelectObject(hdc,oldBrush); 
		 
		DeleteObject(newPen); 
		 
		DeleteObject(oldPen); 
		DeleteObject(oldBrush); 
		 
		 
		SetROP2(hdc,oldRop2); 
		 
	} 
	 
} 
 
BOOL CSuperPoint::ClearLockMark( HDC& hdc ) 
{ 
	HPEN newPen; 
	if( m_status == LOCKED ) 
	{ 
		m_status = NOTHING; 
		newPen = CreatePen(0,0,RGB(255,0,0) ^ GetBkColor(hdc) ); 
		//	m_status = OVER; 
		int oldRop2 = SetROP2(hdc,R2_XORPEN); 
		HPEN oldPen = (HPEN)SelectObject(hdc,newPen); 
		HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,GetStockObject(NULL_BRUSH)); 
	 
		::Rectangle(hdc,m_Point.x - 4,m_Point.y - 4,  
			m_Point.x + 4, m_Point.y + 4 ); 
			 
		SelectObject(hdc,oldPen); 
		SelectObject(hdc,oldBrush); 
		 
		DeleteObject(newPen); 
		 
		DeleteObject(oldPen); 
		DeleteObject(oldBrush); 
		 
		SetROP2(hdc,oldRop2); 
		 
		 
		 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
 
	 
} 
BOOL CSuperPoint::SetLockMark(HDC& hdc) 
{ 
	HPEN newPen; 
	if( m_status == OVER ) 
	{ 
		ClearMark( hdc );				//清除待选蓝色标记 
		m_status = LOCKED; 
	 
		newPen = CreatePen(0,0,RGB(255,0,0) ^ GetBkColor(hdc) ); 
		//	m_status = OVER; 
		int oldRop2 = SetROP2(hdc,R2_XORPEN); 
		HPEN oldPen = (HPEN)SelectObject(hdc,newPen); 
		HBRUSH oldBrush = (HBRUSH)SelectObject(hdc,GetStockObject(NULL_BRUSH)); 
	 
		::Rectangle(hdc,m_Point.x - 4,m_Point.y - 4,  
			m_Point.x + 4, m_Point.y + 4 ); 
			 
		SelectObject(hdc,oldPen); 
		SelectObject(hdc,oldBrush); 
		 
		DeleteObject(newPen); 
		 
		DeleteObject(oldPen); 
		DeleteObject(oldBrush); 
		 
		SetROP2(hdc,oldRop2); 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
 
 
} 
 
 
 
 
bool CSuperPoint::IsOnLine(const POINT ptBeg, const POINT ptEnd) const 
{ 
	 
	CSuperPoint BegPos(ptBeg); 
	int Dist  = round(BegPos.DistanceTo(ptEnd)); 
    int Dist1 = round(DistanceTo(ptBeg)); 
    int Dist2 = round(DistanceTo(ptEnd)); 
    if( abs(Dist -(Dist1+Dist2)) <= 5 )              //上线 
    { 
        return true; 
    } 
    else 
    { 
        return false; 
    } 
} 
 
bool CSuperPoint::IsOnLine(const CSuperPoint& ptBeg,const CSuperPoint& ptEnd) const 
{ 
    return IsOnLine(ptBeg.GetPoint(),ptEnd.GetPoint()); 
} 
bool CSuperPoint::IsOnLine(const CSuperPoint& centerPt, int Radius) const 
{ 
	int Dist = round(DistanceTo(centerPt)); 
	if ( abs(Dist - Radius) < 3 ) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
     
} 
 
//功能:计算与另一点之间的距离 
//输入参数:CSuperPoint desPt 代表other point 
//返回参数:double 距离 
int CSuperPoint::DistanceTo(const CSuperPoint& desPt) const 
{ 
    int iDx = abs(desPt.GetPoint().x - m_Point.x); 
    int iDy = abs(desPt.GetPoint().y - m_Point.y); 
    double dDist ; 
    dDist = sqrt(iDx * iDx + iDy * iDy); 
    return round(dDist); 
     
} 
 
//功能:计算与另一点构成的直线与X正方向轴的夹角 
//      X轴向右, Y轴向下 
//输入参数:CSuperPoint desPt 代表other point 
//返回参数:double 夹角 
double CSuperPoint::AngleWithX(const CSuperPoint& desPt) 
{ 
    double dbAngle = 0.0; 
    double dbDx = desPt.GetPoint().x - m_Point.x; 
    double dbDy = desPt.GetPoint().y - m_Point.y; 
     
	if( fabs(dbDy) < _PRECISION_ ) 
    { 
		// 如果是水平直线 ... 
        if( dbDx > 0 ) dbAngle = 0; 
        if( dbDx < 0 ) dbAngle = PII; 
		 
    } 
	else if( fabs(dbDx) < _PRECISION_ )  
    { 
		// 如果是垂直直线 ... 
        if( dbDy > 0 )  dbAngle =  PII/ 2; 
        if( dbDy < 0 )  dbAngle =  (3*PII) / 2; 
		 
    } 
	else 
    { 
		// 一般情况 ... 
        dbAngle = atan(dbDy / dbDx); 
		 
		if( dbDx < 0 && dbDy > 0 ) 
			dbAngle = PII + dbAngle; 
		 
		if( dbDx < 0 && dbDy < 0 )  
			dbAngle = PII  + dbAngle; 
		 
		if( dbDx > 0 && dbDy < 0 )  
			dbAngle = PII*2 + dbAngle; 
		 
    } 
	 
    return dbAngle; 
} 
 
//功能:计算与另两点构成的直线之间的夹角 
//      X轴向右, Y轴向下 
//输入参数:CSuperPoint onedesPt, twodesPt 代表other point 
//返回参数:double 夹角 
double CSuperPoint::AngleWithTwoLine(const CSuperPoint& onedesPt,const CSuperPoint& twodesPt) 
{ 
	double oneAngle_x; 
	double twoAngle_x; 
	 
	oneAngle_x = AngleWithX(onedesPt); 
	twoAngle_x = AngleWithX(twodesPt); 
	 
	return twoAngle_x - oneAngle_x; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// 功能: 对自己进行平移变换 
//  
// 入口参数:dx, dy轴方向的平移量 
// 
// 出口参数:无 
// 
// 返回值  :对自己的引用 
//  
CSuperPoint& CSuperPoint::Move(int dx, int dy) 
{ 
    m_Point.x += dx; 
    m_Point.y += dy; 
	 
	return (*this); 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// 功能: 对自己绕指定点进行指定角度的旋转变换 
//  
// 入口参数:ptFix - 旋转中心点,dbAngle - 旋转角度 
// 
// 出口参数:无 
// 
// 返回值  :对自己的引用 
//  
CSuperPoint& CSuperPoint::RotateOnFix(const CSuperPoint& ptFix, double dbAngle) 
{ 
	CXMatrix matrix;	// 变换矩阵 
	double sa = sin(dbAngle); 
	double ca = cos(dbAngle); 
	 
	// 设置旋转变换矩阵 
	matrix.r11 =  ca; 
	matrix.r12 =  sa; 
	matrix.r13 = 0.0; 
	 
	matrix.r21 = -sa; 
	matrix.r22 =  ca; 
	matrix.r23 = 0.0; 
	 
	matrix.r31 = (1 - ca) * ptFix.GetPoint().x + ptFix.GetPoint().y * sa; 
	matrix.r32 = (1 - ca) * ptFix.GetPoint().y - ptFix.GetPoint().x * sa; 
	matrix.r33 = 1.0; 
	 
	 
	(*this) = matrix * (*this); 
	 
	 
	return (*this); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// 功能: 求根据相对指定点,指定角度的直线相对称的点 
// 入口参数:ptPos - 对称轴上的某点,dbAngle - 对称轴与x轴正方向的夹角 
// 出口参数:无 
// 返回值  :对自己的引用 
CSuperPoint& CSuperPoint::MirrorOnAxis(double dbAngle,const CSuperPoint& ptPos) 
{ 
	 
   	CXMatrix matrix;	//变换矩阵 
	 
	matrix.r11 =  1.0;             //a 
	matrix.r12 =  0.0;              //d     
	matrix.r13 =  0.0;              //0 
	 
	matrix.r21 =  0.0;              //b 
	matrix.r22 =  -1.0;             //e 
	matrix.r23 =  0.0;              //0 
	 
	matrix.r31 =  0.0;              //0 
	matrix.r32 =  0.0;              //0 
	matrix.r33 =  1.0;              //1 
	 
	if(fabs(dbAngle) <  _PRECISION_ )					//如果对称轴为水平线 
	{ 
		this->Move(-ptPos.GetPoint().x, -ptPos.GetPoint().y);			//平移坐标系 
		(*this) = matrix * (*this) ;					//矩阵运算计算对称点 
		this->Move(ptPos.GetPoint().x, ptPos.GetPoint().y);			//还原坐标系 
	} 
	else 
	{ 
		Move(-ptPos.GetPoint().x, -ptPos.GetPoint().y);			//平移坐标系 
		RotateOnFix(CSuperPoint(0,0), -dbAngle);			//旋转坐标轴使X轴与对称轴重合 
        (*this) = matrix *(*this);					        //还原坐标系	 
		RotateOnFix(CSuperPoint(0,0), dbAngle); 
		Move(ptPos.GetPoint().x, ptPos.GetPoint().y); 
	} 
	 
    return *this; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// 功能: 求根据相对比例缩放 
// 入口参数:缩放比例 
// 出口参数:无 
// 返回值  :对自己的引用 
////////////////////////////// 
CSuperPoint& CSuperPoint::ZoomIn(double sk,const CSuperPoint& centerPt) 
{ 
	CXMatrix matrix;	//变换矩阵 
	 
	matrix.r11 =  sk;             //a 
	matrix.r12 =  0.0;              //d     
	matrix.r13 =  0.0;              //0 
	 
	matrix.r21 =  0.0;              //b 
	matrix.r22 =  sk;             //e 
	matrix.r23 =  0.0;              //0 
	 
	matrix.r31 =  0.0;              //0 
	matrix.r32 =  0.0;              //0 
	matrix.r33 =  1.0;              //1 
	 
	this->Move(-centerPt.GetPoint().x,-centerPt.GetPoint().y); 
	 
	 
	(*this) = matrix*(*this); 
	 
	this->Move(centerPt.GetPoint().x,centerPt.GetPoint().y); 
	 
	return *this; 
	 
	 
}