221 lines
4.2 KiB
C++
221 lines
4.2 KiB
C++
//-----------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TPGeLine2D.cpp
|
|
// = Version : ver2.0
|
|
// = Author : wlw
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: 类的定义
|
|
// = Maintainers:
|
|
//
|
|
//-----------------------------------------------------------------------------+
|
|
#include "StdAfx.h"
|
|
#include "TPGeLine2D.h"
|
|
#include "TPGeCircle.h"
|
|
#include "TGMatrix.h"
|
|
|
|
TPGeLine2D::TPGeLine2D():a(x),b(y),c(z)
|
|
{
|
|
}
|
|
|
|
TPGeLine2D::TPGeLine2D(const TGVector2D&vt,const TGPoint&pt) : TGVector2D(vt),basePoint(pt),a(x),b(y),c(z)
|
|
{
|
|
}
|
|
|
|
TPGeLine2D::TPGeLine2D(const TGPoint&pt0,const TGPoint&pt1) : TGVector2D(pt0,pt1),basePoint(pt0),a(x),b(y),c(z)
|
|
{
|
|
}
|
|
|
|
TPGeLine2D::TPGeLine2D(const TPGeLine2D&line) : TGVector2D(line),basePoint(line.basePoint),a(x),b(y),c(z)
|
|
{
|
|
}
|
|
|
|
TPGeLine2D::~TPGeLine2D()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
TPGeLine2D& TPGeLine2D::operator = (const TPGeLine2D&line)
|
|
{
|
|
a = line.a;
|
|
b = line.b;
|
|
c = line.z;
|
|
basePoint = line.basePoint;
|
|
return *this;
|
|
}
|
|
|
|
void TPGeLine2D::SetVector(const TGPoint&vt)
|
|
{
|
|
a = vt.x;
|
|
b = vt.y;
|
|
c = vt.z;
|
|
}
|
|
|
|
bool TPGeLine2D::IsValid()const
|
|
{
|
|
return !IsNullVector();
|
|
}
|
|
|
|
bool TPGeLine2D::IsSameLine(const TPGeLine2D&line,bool bSameDirection,bool bNormalized)const
|
|
{
|
|
bool r = IsParallel(line,bNormalized) && HitTest(line.basePoint,bNormalized).Is(GR_IN);
|
|
if(bSameDirection)
|
|
{
|
|
return r && IsSameDirection(line,bNormalized);
|
|
}
|
|
else
|
|
{
|
|
return r;
|
|
}
|
|
}
|
|
|
|
double TPGeLine2D::Integration(double pa0,double pa1)const
|
|
{
|
|
return LineIntegration2D(basePoint,*this,pa0,pa1);
|
|
}
|
|
|
|
void TPGeLine2D::SetPoint(const TGPoint&pt0,const TGPoint&pt1)
|
|
{
|
|
a = pt1.x - pt0.x;
|
|
b = pt1.y - pt0.y;
|
|
basePoint = pt0;
|
|
}
|
|
|
|
void TPGeLine2D::Pa2Pt(double pa,TGPoint&pt)const
|
|
{
|
|
pt.x = basePoint.x + a * pa;
|
|
pt.y = basePoint.y + b * pa;
|
|
pt.z = basePoint.z;
|
|
}
|
|
|
|
void TPGeLine2D::Pt2Pa(const TGPoint&pt,double&pa)const
|
|
{
|
|
pa = fabs(a) > fabs(b) ? (pt.x - basePoint.x) / a : (pt.y - basePoint.y) / b;
|
|
}
|
|
|
|
GRelation TPGeLine2D::HitTest(const TGPoint&pt,bool bNormalized)const
|
|
{
|
|
//强制执行距离精度
|
|
TGPoint ptNear;
|
|
GetNearestPoint(pt,ptNear);
|
|
if(DOWNEQUAL_ZEROD(Distance2D(pt,ptNear)))
|
|
{
|
|
return GR_IN;
|
|
}
|
|
|
|
TGVector2D vt1(basePoint,pt);
|
|
vt1.Normalize();
|
|
|
|
double dn;
|
|
if(bNormalized)
|
|
{
|
|
dn = (*this) * vt1;
|
|
}
|
|
else
|
|
{
|
|
TGVector2D vt0(*this);
|
|
vt0.Normalize();
|
|
dn = vt0 * vt1;
|
|
}
|
|
|
|
if(dn > 0)
|
|
{
|
|
return GR_LEFT;
|
|
}
|
|
else if(dn < 0)
|
|
{
|
|
return GR_RIGHT;
|
|
}
|
|
else
|
|
{
|
|
return GR_IN;
|
|
}
|
|
}
|
|
|
|
int TPGeLine2D::Intersect(const TPGeLine2D&line,double&pa,bool bNormalized)const
|
|
{
|
|
if(bNormalized)
|
|
{
|
|
return LineInt2D(basePoint,*this,line.basePoint,line,pa);
|
|
}
|
|
TGVector2D vt1(*this),vt2(line);
|
|
double l1 = vt1.Length();
|
|
vt1 /= l1;
|
|
vt2.Normalize();
|
|
int iRet = LineInt2D(basePoint,vt1,line.basePoint,vt2,pa);
|
|
pa /= l1;
|
|
return iRet;
|
|
}
|
|
|
|
int TPGeLine2D::Intersect(const TPGeLine2D&line,double&pa1,double&pa2,bool bNormalized)const
|
|
{
|
|
if(bNormalized)
|
|
{
|
|
return LineInt2D(basePoint,*this,line.basePoint,line,pa1,pa2);
|
|
}
|
|
|
|
TGVector2D vt1(*this),vt2(line);
|
|
double l1 = vt1.Length(),l2 = vt2.Length();
|
|
vt1 /= l1;
|
|
vt2 /= l2;
|
|
|
|
int iRet = LineInt2D(basePoint,vt1,line.basePoint,vt2,pa1,pa2);
|
|
pa1 /= l1;
|
|
pa2 /= l2;
|
|
return iRet;
|
|
}
|
|
|
|
int TPGeLine2D::Intersect(const TPGeLine2D&line,TGPoint&pt,bool bNormalized)const
|
|
{
|
|
double pa;
|
|
int ret = Intersect(line,pa,bNormalized);
|
|
if(ret==INT_1PT)
|
|
{
|
|
Pa2Pt(pa,pt);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void TPGeLine2D::GetNearestPoint(const TGPoint&pt,TGPoint&res)const
|
|
{
|
|
TPGeLine2D line;
|
|
GetLVertVector(line);
|
|
line.basePoint = pt;
|
|
Intersect(line,res);
|
|
}
|
|
|
|
void TPGeLine2D::Offset(double nDist)
|
|
{
|
|
TGVector2D vt;
|
|
GetRVertVector(vt);
|
|
vt.SetLength(nDist);
|
|
basePoint += vt;
|
|
}
|
|
|
|
int TPGeLine2D::Intersect(const TPGeCircle *pCircle,double&pa1,double&pa2,bool bNormalized)const
|
|
{
|
|
ASSERT(pCircle != NULL);
|
|
|
|
if(bNormalized)
|
|
{
|
|
return LineCirInt2D(pCircle->center,pCircle->r,basePoint,*this,pa1,pa2);
|
|
}
|
|
|
|
TGVector2D vt(*this);
|
|
double len = vt.Length();
|
|
vt /= len;
|
|
|
|
int iRet= LineCirInt2D(pCircle->center,pCircle->r,basePoint,vt,pa1,pa2);
|
|
pa1 /= len;
|
|
pa2 /= len;
|
|
|
|
return iRet;
|
|
}
|
|
|
|
void TPGeLine2D::TransformBy(const TGMatrix* pMatrix)
|
|
{
|
|
ASSERT(pMatrix != NULL);
|
|
|
|
pMatrix->Transform(*this);
|
|
pMatrix->Transform2D(basePoint);
|
|
} |