📄 gisvector.cpp
字号:
// Vector.cpp: implementation of the CVector class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GISVector.h"
#include "GISMatrix.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVector::~CVector()
{
x=y=z=0.0;
}
double CVector::AngleTo(const CVector& vec) const
{
return acos(DotProduct(vec)/(Length()*vec.Length()));
}
double CVector::DotProduct(const CVector& vec) const
{
return x*vec.x+y*vec.y+z*vec.z;
}
CVector& CVector::CrossProduct(const CVector& vec)
{
CVector v;
v.x=y*vec.z-z*vec.y;
v.y=z*vec.x-x*vec.z;
v.z=x*vec.y-y*vec.x;
x=v.x;
y=v.y;
z=v.z;
return *this;
}
CVector& CVector::SetToSum(const double a, const CVector &vec)
{
x += a*vec.x;
y += a*vec.y;
z += a*vec.z;
return *this;
}
BOOL CVector::IsUnitLength(const double tolerance) const
{
double len;
len=Length();
if (fabs(len-1.0) <= tolerance)
return TRUE;
else
return FALSE;
}
BOOL CVector::IsZeroLength(const double tolerance) const
{
double len;
len=Length();
if (fabs(len) <= tolerance)
return TRUE;
else
return FALSE;
}
double CVector::Length() const
{
return sqrt(x*x+y*y+z*z);
}
CVector& CVector::RotateAboutVectorbyAng(CVector& vec, double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateVectorAboutVectorbyAng(vec, angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
BOOL CVector::IsParallelTo(const CVector& vec, const double tolerance) const
{
double ang;
ang=AngleTo(vec);
if (fabs(ang) <= tolerance)
return TRUE;
else
return FALSE;
}
BOOL CVector::IsEqualTo(const CVector &vec, double tolerance) const
{
if (fabs(x-vec.x) <= tolerance && fabs(y-vec.y) <= tolerance && fabs(z-vec.z) <= tolerance)
return TRUE;
else
return FALSE;
}
BOOL CVector::IsPerpendicularTo(const CVector& vec, double tolerance) const
{
double cosang;
cosang=DotProduct(vec);
if (fabs(cosang) <= tolerance)
return TRUE;
else
return FALSE;
}
CVector& CVector::Normalization(double tolerance)
{
double len;
len = Length();
if (len > tolerance)
{
x /= len;
y /= len;
z /= len;
}
return *this;
}
CVector& CVector::RotateAboutXbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateVectorAboutXbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
CVector& CVector::RotateAboutYbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateVectorAboutYbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
CVector& CVector::RotateAboutZbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateVectorAboutZbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
CVector& CVector::RotateCoordSysAboutXbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateCoordSysAboutXbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
CVector& CVector::RotateCoordSysAboutYbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateCoordSysAboutYbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
CVector& CVector::RotateCoordSysAboutZbyAng(double angle)
{
CMatrix m;
CVector v;
v.Set(x,y,z);
m.RotateCoordSysAboutZbyAng(angle);
x=m.mat[0][0]*v.x+m.mat[0][1]*v.y+m.mat[0][2]*v.z;
y=m.mat[1][0]*v.x+m.mat[1][1]*v.y+m.mat[1][2]*v.z;
z=m.mat[2][0]*v.x+m.mat[2][1]*v.y+m.mat[2][2]*v.z;
return *this;
}
void CVector::SetZero(void)
{
x=0.0;
y=0.0;
z=0.0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -