C++求点与直线,直线与直线的位置关系

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
x = -x;
y = -y;
}
CVecter2D CVecter2D::GetVertical(bool bClockwise) //获取正交向量
{
if(bClockwise)
{
return CVecter2D(y,-x);
}
return CVecter2D(-y,x);
}
//-----------属性-----------------------
void operator /= (const double& fScalar);
//-----------操作------------------------
public:
voidNormalized(); //归一化
doubleDot(const CVecter2D& vecter); //点积
{
CVecter2D v = line_pt2 - line_pt1;
CVecter2D c = pt - line_pt1;
double K = c.Dot(v) / v.Dot(v);
CVecter2D Kv = v*K; //v与line平行的分量
int nRet = -1; //cout<<"垂足在line_pt1,line_pt2的反向延长线上"<<endl;
//-----------操作------------------------
void CPoint2D::operator = (const CPoint2D& pt)
{
x = pt.x;
y = pt.y;
}
CPoint2D CPoint2D::operator + (const CVecter2D& vecter)
{
return CPoint2D(x+vecter.x, y+vecter.y);
}
CPoint2D CPoint2D::operator - (const CVecter2D& vecter)
{
return CPoint2D(x-vecter.x, y-vecter.y);
}
CVecter2D CPoint2D::operator - (const CPoint2D& vecter)
{
return CVecter2D(x-vecter.x, y-vecter.y);
}
//求点到直线的距离,垂足,垂足与线段位置关系
int GetPointToLineMSG(CPoint2D pt,CPoint2D line_pt1,CPoint2D line_pt2,CPoint2D& ptResult,double& fLength)
double GetAngle(const CVecter2D& vecter); //夹角
intIsVertical(const CVecter2D& vecter); //0--垂直,1--大于90度,-1小于90度
boolIsParallel(const CVecter2D& vecter); //是否平行
{
return CVecter2D(x-vecter.x, y-vecter.y);
}
CVecter2D CVecter2D::operator * (const double& fScalar)
{
return CVecter2D(x*fScalar, y*fScalar);
}
CVecter2D CVecter2D::operator / (const double& fScalar)
public:
CPoint2D(){ x = y = 0.0;}
CPoint2D(double fx,double fy){x = fx; y = fy;}
CPoint2D(const CPoint2D& pt){x = pt.x; y=pt.y;}
public:
double x,y;
//-----------操作------------------------
CVecter2D(){ x = y = 0.0;}
CVecter2D(double fx,double fy){x = fx; y = fy;}
public:
double x,y;
//-----------操作符---------------------
public:
CVecter2D operator + (const CVecter2D& vecter);
return false;
}
intCVecter2D::IsVertical(const CVecter2D& vecter)
{
double fDot = Dot(vecter);
if(fDot == 0)return 0;
if(fDot < 0)return -1;
return 1;
}
void CVecter2D::Inverted() //求反
//----------------------------------
//类名:Vecter2D
//功能:2维矢量类
//----------------------------------
class CVecter2D
{
//---------构造函数---------------------
public:
bool operator == (const CVecter2D& vecter);
void operator += (const CVecter2D& vecter);
void operator -= (const CVecter2D& vecter);
void operator *= (const double& fScalar);
public:
void operator = (const CPoint2D& pt);
CPoint2D operator + (const CVecter2D& vecter);
CPoint2D operator - (const CVecter2D& vecter);
CVecter2D operator - (const CPoint2D& pt);
}
double CVecter2D::GetAngle(const CVecter2D& vecter) //夹角
{
CVecter2D vec1 = vecter;
CVecter2D vec2(x,y);
if(vec1.GetLength() && vec2.GetLength())
{
vec1.Normalized();
void CVecter2D::Normalized() //归一化
{
double fLength = GetLength();
x = x/fLength;
y = y/fLength;
}
double CVecter2D::Dot(const CVecter2D& vecter) //点积
{
return (x*vecter.x + y*vecter.y);
{
x *= fScalar;
y *= fScalar;
}
void CVecter2D::operator /= (const double& fScalar)
{
if(fScalar)
{
x /= fScalar;
y /= fScalar;
}
else
{
x = y = 0;
}
}
//-----------操作------------------------
}
}
CVecter2D vT = v.GetVertical();
double M = c.Dot(vT) / vT.Dot(vT);
doubleGetLength(); //长度
};
//---------------------------------------
//类名:CPoint2D
//功能:2维点
//---------------------------------------
class CPoint2D
{
//---------构造函数----------------------
}
bool CVecter2D::operator == (const CVecter2D& vecter)
{
if(x==vecter百度文库x && y==vecter.y)return true;
return false;
}
void CVecter2D::operator += (const CVecter2D& vecter)
double fAngle = Kv.Dot(v);
if(fAngle>0)
{
nRet = 1; //cout<<"垂足在line_pt1 line_pt2的延长线上"<<endl;
if(Kv.GetLength() < v.GetLength())
{
nRet = 0; //cout<<"垂足在line上"<<endl;
CVecter2D CVecter2D::operator + (const CVecter2D& vecter)
{
return CVecter2D(x+vecter.x, y+vecter.y);
}
CVecter2D CVecter2D::operator - (const CVecter2D& vecter)
}
}
return false;
}
double CVecter2D::GetLength() //长度
{
return sqrt(x*x + y*y);
}
//---------------------------------------
//类名:CPoint2D
//---------------------------------------
bool CVecter2D::IsAffine() //是否为仿射组合
{
if((x+y) == 1)return true;
return false;
}
bool CVecter2D::IsConvex() //是否为凸组合
{
if(IsAffine())
{
if(x>=0 && y>=0)
{
return true;
voidInverted(); //求反
CVecter2D GetVertical(bool bClockwise = false); //获取正交向量
//-----------属性------------------------
public:
boolIsAffine(); //是否为仿射组合
boolIsConvex(); //是否为凸组合
CVecter2D operator - (const CVecter2D& vecter);
CVecter2D operator * (const double& fScalar);
CVecter2D operator / (const double& fScalar);
void operator = (const CVecter2D& vecter);
};
#include "iostream.h"
#include "math.h"
#include "TestVecter.h"
//----------------------------------
//类名:Vecter2D
//----------------------------------
//-----------操作---------------------
vec2.Normalized();
return acos(vec1.Dot(vec2));
}
else
{
return 0;
}
}
bool CVecter2D::IsParallel(const CVecter2D& vecter)
{
if((x*vecter.y - y*vecter.x) == 0)return true;
{
if(fScalar)
{
return CVecter2D(x/fScalar, y/fScalar);
}
else
{
return CVecter2D(0,0);
}
}
void CVecter2D::operator=(const CVecter2D& vecter)
{
x = vecter.x;
y = vecter.y;
{
x += vecter.x;
y += vecter.y;
}
void CVecter2D::operator -= (const CVecter2D& vecter)
{
x -= vecter.x;
y -= vecter.y;
}
void CVecter2D::operator *= (const double& fScalar)
相关文档
最新文档