C_QGISAlgorithmLib.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:10k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // QGISAlgorithmLib->cpp: implementation of the CQGISAlgorithmLib class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "....QObjectsincludeResource.h"
- #include "..includeResource.h"
- #include "..includeC_QGISAlgorithmLib.h"
- #include "....QObjectsincludeQLineObj.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CQGISAlgorithmLib::CQGISAlgorithmLib()
- {
- }
- CQGISAlgorithmLib::~CQGISAlgorithmLib()
- {
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:IsLineCross
- ///返回类型:BOOL
- ///入口参数:CQPoint p1,CQPoint p2,CQPoint q1,CQPoint q2
- ///出口参数:无
- ///思路说明:边界矩形,矢量跨立实验
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CQGISAlgorithmLib::IsLineCross(CQPoint p1,CQPoint p2,CQPoint q1,CQPoint q2)
- {
- //创建两条直线对象 用于进行边界矩形的判断
- if(p1 == q1 || p1 == q2 || p2 == q1 || p2 == q2)
- return TRUE; //直接得出直线相交
- CQLineObj * pLine1 = new CQLineObj;
- pLine1->m_PtList.AddPoint(&p1);
- pLine1->m_PtList.AddPoint(&p2);
- CQLineObj * pLine2 = new CQLineObj;
- pLine2->m_PtList.AddPoint(&q1);
- pLine2->m_PtList.AddPoint(&q2);
- CBoundaryRect * pRect1 = new CBoundaryRect;
- pLine1->GetBoundingRect(pRect1);
- CBoundaryRect * pRect2 = new CBoundaryRect;
- pLine2->GetBoundingRect(pRect2);
- if(!pRect1->IsCross(pRect2))
- {
- //假如两个边界矩形不相交
- delete pRect2;
- delete pRect1;
- delete pLine2;
- delete pLine1;
- return FALSE; //不相交
- }
- //进行跨立实验
- double p1q1x = p1.GetX() - q1.GetX();
- double p1q1y = p1.GetY() - q1.GetY();
- double q2q1x = q2.GetX() - q1.GetX();
- double q2q1y = q2.GetY() - q1.GetY();
- double p2q1x = p2.GetX() - q1.GetX();
- double p2q1y = p2.GetY() - q1.GetY();
- double fResult; //运算结果
- double fpq = p1q1x*q2q1y - q2q1x*p1q1y;
- double fqp = q2q1x*p2q1y - p2q1x*q2q1y;
- fResult = fpq*fqp;
- if(fResult>=0)
- {
- delete pRect2;
- delete pRect1;
- delete pLine2;
- delete pLine1;
- return TRUE;
- }
- delete pRect2;
- delete pRect1;
- delete pLine2;
- delete pLine1;
- return FALSE;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CalLineCrossPoint
- ///返回类型:无
- ///入口参数:CQPoint p1,CQPoint p2,CQPoint q1,CQPoint q2
- ///出口参数:CQPointArray * pResult
- ///思路说明:判断两条直线是否相交,若相交则返回其交点
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CQGISAlgorithmLib::CalLineCrossPoint(CQPoint pp1,CQPoint pp2,CQPoint qq1,CQPoint qq2,CQPointArray * pResult)
- {
- if(!IsLineCross(pp1,pp2,qq1,qq2))
- {
- pResult = 0;
- return;
- }
- CQPoint * p1 = new CQPoint(pp1);
- CQPoint * p2 = new CQPoint(pp2);
- CQPoint * q1 = new CQPoint(qq1);
- CQPoint * q2 = new CQPoint(qq2);
- //计算两条直线的斜率
- //double k1 = (p2.GetY()-p1.GetY())/(p2.GetX()-p1.GetX());
- //double k2 = (q2.GetY()-q1.GetY())/(q2.GetX()-q1.GetX());
- //L0(p1,p2),L1(q1,q2);
- //情况1 L0平行于Y轴
- //分两种情况 当L1也与Y轴平行时,则两条直线共线
- //若L1与Y轴不平行,则直接代入方程求解
- if(p1->GetX() == p2->GetX())
- {
- if(q1->GetX() == q2->GetX()) //L1也与Y轴平行
- {
- CalSameLineCrossPoint(*p1,*p2,*q1,*q2,pResult);
- return;
- }
- else //L1与Y轴不平行
- {
- double k2 = (q2->GetY()-q1->GetY())/(q2->GetX()-q1->GetX());
- double x2 = p1->GetX();
- //y2 = k2x2-k2x1+y1;x1 = q1->GetX(),y1 = q1->getY();
- double y2 = k2*x2-k2*q1->GetX()+q1->GetY();
- pResult->AddPoint(x2,y2);
- delete q2;
- }
- }
- else if(q1->GetX() == q2->GetX())
- {
- double k1 = (p2->GetY()-p1->GetY())/(p2->GetX()-p1->GetX());
- double x1 = q1->GetX(),y1;
- y1 = k1*x1-k1*p1->GetX()+p1->GetY();
- pResult->AddPoint(x1,y1);
- }
- else if(p1->GetY() == p2->GetY()) //平行与X 轴
- {
- if(q1->GetY() == q2->GetY()) //L1也平行与X轴
- {
- CalSameLineCrossPoint(*p1,*p2,*q1,*q2,pResult);
- }
- else
- {
- double k2 = (q2->GetY()-q1->GetY())/(q2->GetX()-q1->GetX());
- double y2 = p1->GetY();
- double x2 = (y2+k2*q1->GetX()-q1->GetY())/k2;
- pResult->AddPoint(x2,y2);
- }
- }
- else if(q1->GetY() == q2->GetY())
- {
- double k1 = (p2->GetY()-p1->GetY())/(p2->GetX()-p1->GetX());
- double y1 = q1->GetY();
- double x1 = (y1+k1*p1->GetX()-p1->GetY())/k1;
- pResult->AddPoint(x1,y1);
- }
- else
- {
- double k1 = (p2->GetY()-p1->GetY())/(p2->GetX()-p1->GetX());
- double k2 = (q2->GetY()-q1->GetY())/(q2->GetX()-q1->GetX());
- if(k1-k2 == SMALL_NUMBER)
- {
- CalSameLineCrossPoint(*p1,*p2,*q1,*q1,pResult);
- }
- else
- {
- double xResult = k1*p1->GetX()-k2*q1->GetX()+q1->GetY()-p1->GetY();
- xResult = xResult/(k1-k2);
- double yResult = k1*xResult-k1*p1->GetX()+p1->GetY();
- pResult->AddPoint(xResult,yResult);
- }
- }
- delete q2;
- delete q1;
- delete p2;
- delete p1;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CalSameLineCrossPoint
- ///返回类型:无
- ///入口参数:CQPoint p1,CQPoint p2,CQPoint q1,CQPoint q2
- ///出口参数:CQPointArray * pResult
- ///思路说明:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CQGISAlgorithmLib::CalSameLineCrossPoint(CQPoint pp1,CQPoint pp2,CQPoint qq1,CQPoint qq2,CQPointArray * pResult)
- {
- //直接计算 不做条件判断 在调用之前一定要确保两直线是相交的
- //共线计算只考虑垂直于X,Y轴的情形
- double fy1,fy2;
- double fy3,fy4;
- double fx1,fx2;
- double fx3,fx4;
- CQPoint * p1 = new CQPoint(pp1);
- CQPoint * p2 = new CQPoint(pp2);
- CQPoint * q1 = new CQPoint(qq1);
- CQPoint * q2 = new CQPoint(qq2);
- //先找出较长的直线
- double dd1 = p1->Distance(*p2);
- double dd2 = q1->Distance(*q2);
- //始终用较长的线段为基准进行判断
- if(dd1<dd2)
- {
- CQPoint tmPoint = *p1;
- *p1 = *q1;
- *q1 = tmPoint;
- tmPoint = *p2;
- *p2 = *q2;
- *q2 = tmPoint;
- }
- if(p1->GetX() == p2->GetX() && q1->GetX() == q2->GetX()) //两条直线都与Y轴平行
- {
- fy1 = min(p1->GetY(),p2->GetY());
- fy2 = max(p1->GetY(),p2->GetY());
- fy3 = min(q1->GetY(),q2->GetY());
- fy4 = max(q1->GetY(),q2->GetY());
- p1->SetPoint(p1->GetX(),fy1); //Y较小的点
- p2->SetPoint(p2->GetX(),fy2); //Y较大的点
- q1->SetPoint(q1->GetX(),fy3);
- q2->SetPoint(q2->GetX(),fy4);
- if(p1->GetY() > q2->GetY() || p2->GetY() < q1->GetY())
- {
- pResult = 0;
- }
- else
- {
- if(p2->GetY() == q1->GetY())
- {
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(q1->GetY()<p2->GetY() && q1->GetY() >p1->GetY())
- {
- if(p2->GetY()>q1->GetY() && p2->GetY()<q2->GetY())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(p2->GetY()>=q2->GetY())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(q2->GetY(),q2->GetY());
- }
- }
- else if(*p1 == *q1 && *p2 == *q2) //两直线重合
- {
- pResult->AddPoint(p1->GetX(),p1->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- }
- }
- else if(p1->GetY() == p2->GetY() && q1->GetY() == q2->GetY()) //两条直线都与X轴平行
- {
- fx1 = min(p1->GetX(),p2->GetX());
- fx2 = max(p1->GetX(),p2->GetX());
- fx3 = min(q1->GetX(),q2->GetX());
- fx4 = max(q1->GetX(),q2->GetX());
- p1->SetPoint(fx1,p1->GetY());
- p2->SetPoint(fx2,p2->GetY());
- q1->SetPoint(fx3,q1->GetY());
- q2->SetPoint(fx4,q2->GetY());
- if(p2->GetX() < q1->GetX() || p1->GetX() > q2->GetX())
- {
- pResult = 0;
- }
- else if(q1->GetX()>p1->GetX() && q1->GetX()<p2->GetX())
- {
- if(p2->GetX()>q1->GetX() && p2->GetX()<q2->GetX())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(p2->GetX()>=q2->GetX())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(q2->GetX(),q2->GetY());
- }
- }
- else if(p2->GetX() == q1->GetX())
- {
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(*p1 == *q1 && *p2 == *q2)
- {
- pResult->AddPoint(p1->GetX(),p2->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- }
- else //两条直线只是斜率相等
- {
- if(p1->GetX()>p2->GetX())
- {
- CQPoint tmPoint;
- tmPoint = *p1;
- *p1 = *p2;
- *p2 = tmPoint;
- }
- if(q1->GetX()>q2->GetX())
- {
- CQPoint tmPoint;
- tmPoint = *q1;
- *q1 = *q2;
- *q2 = tmPoint;
- }
- if(p2->GetX()<q1->GetX())
- {
- CQPoint tmPoint = *q1;
- *p1 = *q1;
- *q1 = tmPoint;
- tmPoint = *q2;
- *p2 = *q2;
- *q2 = tmPoint;
- }
- if(p2->GetX() < q1->GetX() || p1->GetX() > q2->GetX())
- {
- pResult = 0;
- return;
- }
- else if(q1->GetX()>p1->GetX() && q1->GetX()<p2->GetX())
- {
- if(p2->GetX()>q1->GetX() && p2->GetX()<q2->GetX())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(p2->GetX()>=q2->GetX())
- {
- pResult->AddPoint(q1->GetX(),q1->GetY());
- pResult->AddPoint(q2->GetX(),q2->GetY());
- }
- }
- else if(p2->GetX() == q1->GetX())
- {
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- else if(*p1 == *q1 && *p2 == *q2)
- {
- pResult->AddPoint(p1->GetX(),p1->GetY());
- pResult->AddPoint(p2->GetX(),p2->GetY());
- }
- }
- delete q2;
- delete q1;
- delete p2;
- delete p1;
- }
- //弧度转换为度单位
- double CQGISAlgorithmLib::RadianToDegree(double fRadian)
- {
- return fRadian*PIunder180;
- }
- double CQGISAlgorithmLib::DegreeToRadian(double fDegree)
- {
- return fDegree*PIover180;
- }