BoundaryRect.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- //////////////////////////////////////////////////////////////////////
- #include "..stdafx.h"
- #include "math.h"
- #include "..includeQBaseObj.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////////
- //CBoundaryRect函数定义部分
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CBoundaryRect
- ///函数返回:无
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect::CBoundaryRect()
- {
- m_fMinX = m_fMinY = m_fMaxX = m_fMaxY = 0.0;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CBoundaryRect
- ///函数返回:无
- ///入口参数:CBoundaryRect & Box
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect::CBoundaryRect(CBoundaryRect & Box)
- {
- if(Box.IsValid())
- {
- m_fMinX = Box.GetLeftBottomX();
- m_fMinY = Box.GetLeftBottomY();
- m_fMaxX = Box.GetRightTopX();
- m_fMaxY = Box.GetRightTopY();
- }
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:~CBoundaryRect
- ///函数返回:无
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect::~CBoundaryRect()
- {
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Copy
- ///函数返回:无
- ///入口参数:CBoundaryRect & Box
- ///出口参数:无
- ///函数思路:边界矩形拷贝
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CBoundaryRect::Copy(CBoundaryRect & Box)
- {
- if(Box.IsValid())
- {
- m_fMinX = Box.GetLeftBottomX();
- m_fMinY = Box.GetLeftBottomY();
- m_fMaxX = Box.GetRightTopX();
- m_fMaxY = Box.GetRightTopY();
- }
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:PtInRect
- ///函数返回:BOOL
- ///入口参数:CQPoint & pt
- ///出口参数:无
- ///函数思路:点在矩形里
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CBoundaryRect::PtInRect(CQPoint & pt)
- {
- double px = pt.GetX(),py = pt.GetY();
- if(px>=m_fMinX && px<=m_fMaxX && py>=m_fMinY && py<=m_fMaxY)
- return TRUE;
- else
- return FALSE;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:SetRect
- ///函数返回:无
- ///入口参数:double fMinx,double fMinY,double fMaxX,double fMaxY
- ///出口参数:无
- ///函数思路:设置边界矩形的值
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CBoundaryRect::SetRect(double fMinx,double fMinY,double fMaxX,double fMaxY)
- {
- m_fMinX = fMinx;
- m_fMinY = fMinY;
- m_fMaxX = fMaxX;
- m_fMaxY = fMaxY;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CenterPoint
- ///函数返回:CQPOint
- ///入口参数:无
- ///出口参数:无
- ///函数思路:计算边界矩形的中心点
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CQPoint CBoundaryRect::CenterPoint()
- {
- double fx = (m_fMinX + m_fMaxX)/2;
- double fy = (m_fMinY + m_fMaxY)/2;
- return CQPoint(fx,fy);
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:IsValid
- ///函数返回:BOOL
- ///入口参数:无
- ///出口参数:无
- ///函数思路:判断边界矩形是否有效,根据角点位置
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CBoundaryRect::IsValid()
- {
- if(m_fMinX<=m_fMaxX && m_fMinY<=m_fMaxY)
- return TRUE;
- else
- return FALSE;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Offset
- ///函数返回:无
- ///入口参数:double dx,double dy
- ///出口参数:double dx,double dy
- ///函数思路:增量平移
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline void CBoundaryRect::Offset(double dx,double dy)
- {
- m_fMinX += dx;
- m_fMaxX += dx;
- m_fMinY += dy;
- m_fMaxY += dy;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Offset
- ///函数返回:无
- ///入口参数:CQPoint & pt
- ///出口参数:无
- ///函数思路:增量平移
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline void CBoundaryRect::Offset(CQPoint & pt)
- {
- m_fMinX += pt.GetX();
- m_fMaxX += pt.GetX();
- m_fMinY += pt.GetY();
- m_fMaxY += pt.GetY();
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:operator =
- ///函数返回:CBoundaryRect
- ///入口参数:CBoundaryRect & Box
- ///出口参数:无
- ///函数思路:重载赋值运算符
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect & CBoundaryRect::operator = (CBoundaryRect & Box)
- {
- m_fMinX = Box.GetLeftBottomX();
- m_fMinY = Box.GetLeftBottomY();
- m_fMaxX = Box.GetRightTopX();
- m_fMaxY = Box.GetRightTopY();
- return (*this);
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:UnionBoundary
- ///函数返回:CBoundaryRect *
- ///入口参数:CBoundaryRect * pBox
- ///出口参数:无
- ///函数思路:求两个外接矩形的并集
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect * CBoundaryRect::UnionBoundary(CBoundaryRect * pBox)
- {
- if(pBox->IsValid())
- {
- double fMinX = min(m_fMinX,pBox->GetLeftBottomX());
- double fMaxX = max(m_fMaxX,pBox->GetRightTopX());
- double fMinY = min(m_fMinY,pBox->GetLeftBottomY());
- double fMaxY = max(m_fMaxY,pBox->GetRightTopY());
- CBoundaryRect * pRect = new CBoundaryRect;
- pRect->SetRect(fMinX,fMinY,fMaxX,fMaxY);
- return pRect;
- }
- return NULL;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:
- ///返回类型:
- ///入口参数:
- ///出口参数:
- ///思路说明:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CBoundaryRect::Union(CBoundaryRect * pRect)
- {
- if(pRect->IsValid())
- {
- double fMinX = min(m_fMinX,pRect->GetLeftBottomX());
- double fMinY = min(m_fMinY,pRect->GetLeftBottomY());
- double fMaxX = max(m_fMaxX,pRect->GetRightTopX());
- double fMaxY = max(m_fMaxY,pRect->GetRightTopY());
- SetRect(fMinX,fMinY,fMaxX,fMaxY);
- }
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Cutout
- ///函数返回:CBoundaryRect *
- ///入口参数:CBoundaryRect * pBox
- ///出口参数:无
- ///函数思路:计算两个边界矩形的交集
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CBoundaryRect * CBoundaryRect::Cutout(CBoundaryRect * pBox)
- {
- if(pBox->IsValid())
- {
- if(!IsCross(pBox)) return NULL;
- if(m_fMinX<pBox->GetLeftBottomX()) m_fMinX = pBox->GetLeftBottomX();
- if(m_fMinY<pBox->GetLeftBottomY()) m_fMinY = pBox->GetLeftBottomY();
- if(m_fMaxX>pBox->GetRightTopX()) m_fMaxX = pBox->GetRightTopX();
- if(m_fMaxY>pBox->GetRightTopY()) m_fMaxY = pBox->GetRightTopY();
- return this;
- }
- return NULL;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:
- ///函数返回:
- ///入口参数:
- ///出口参数:
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CBoundaryRect::IsCross(CBoundaryRect * pBox)
- {
- if(pBox->IsValid())
- {
- if(m_fMinX>pBox->m_fMaxX ||
- m_fMaxX<pBox->m_fMinX ||
- m_fMinY>pBox->m_fMaxY ||
- m_fMaxY<pBox->m_fMinY)
- return FALSE;
- else
- return TRUE;
- }
- return FALSE;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:GetLeftBottomX
- ///函数返回:double
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline double CBoundaryRect::GetLeftBottomX()
- {
- return m_fMinX;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:GetLeftBottomY
- ///函数返回:double
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline double CBoundaryRect::GetLeftBottomY()
- {
- return m_fMinY;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:GetRightTopX
- ///函数返回:double
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline double CBoundaryRect::GetRightTopX()
- {
- return m_fMaxX;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:GetRightTopY
- ///函数返回:double
- ///入口参数:无
- ///出口参数:无
- ///函数思路:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- inline double CBoundaryRect::GetRightTopY()
- {
- return m_fMaxY;
- }
- void CBoundaryRect::SetLeftBottomX(double lx)
- {
- m_fMinX = lx;
- }
- void CBoundaryRect::SetLeftBottomY(double ly)
- {
- m_fMinY = ly;
- }
- void CBoundaryRect::SetRightTopX(double rx)
- {
- m_fMaxX = rx;
- }
- void CBoundaryRect::SetRightTopY(double ry)
- {
- m_fMaxY = ry;
- }
- double CBoundaryRect::Width()
- {
- return (m_fMaxX - m_fMinX);
- }
- double CBoundaryRect::Height()
- {
- return (m_fMaxY - m_fMinY);
- }
- IMPLEMENT_SERIAL(CBoundaryRect,CObject,1)
- void CBoundaryRect::Serialize(CArchive & ar)
- {
- if(ar.IsStoring())
- {
- ar.Write(&m_fMinX,sizeof(float));
- ar.Write(&m_fMinY,sizeof(float));
- ar.Write(&m_fMaxX,sizeof(float));
- ar.Write(&m_fMaxY,sizeof(float));
- }
- else
- {
- ar.Read(&m_fMinX,sizeof(float));
- ar.Read(&m_fMinY,sizeof(float));
- ar.Read(&m_fMaxX,sizeof(float));
- ar.Read(&m_fMaxY,sizeof(float));
- }
- }
- CString CBoundaryRect::VarToStr()
- {
- CString szResult;
- szResult.Format("左下角X坐标:%f左下角Y坐标:%f右上角X坐标:%f右上角Y坐标:%f",m_fMinX,m_fMinY,m_fMaxX,m_fMaxY);
- return szResult;
- }
- void CBoundaryRect::WriteToFile(CFile * pFile)
- {
- pFile->Write(&m_fMinX,sizeof(float));
- pFile->Write(&m_fMinY,sizeof(float));
- pFile->Write(&m_fMaxX,sizeof(float));
- pFile->Write(&m_fMaxY,sizeof(float));
- }
- void CBoundaryRect::ReadFromFile(CFile * pFile)
- {
- pFile->Read(&m_fMinX,sizeof(float));
- pFile->Read(&m_fMinY,sizeof(float));
- pFile->Read(&m_fMaxX,sizeof(float));
- pFile->Read(&m_fMaxY,sizeof(float));
- }
- void CBoundaryRect::Lager(double dStep)
- {
- dStep = static_cast<double>(fabs(dStep));
- m_fMinY -= dStep;
- m_fMinX -= dStep;
- m_fMaxX += dStep;
- m_fMaxY += dStep;
- }
- void CBoundaryRect::Smaller(double dStep)
- {
- dStep = static_cast<double>(fabs(dStep));
- m_fMinX += dStep;
- m_fMinY += dStep;
- m_fMaxX -= dStep;
- m_fMaxY -= dStep;
- }