-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanvector.cpp
96 lines (73 loc) · 2.4 KB
/
tanvector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "tanvector.h"
#include <cmath>
double TanVector::_TOLERANCE_ = 0.001;
TanVector::TanVector()
: x(0), y(0), z(0) { }
TanVector::TanVector(double x, double y, double z)
: x(x), y(y), z(z) { }
void TanVector::setTolerance(double tolerance) { _TOLERANCE_ = tolerance; }
double TanVector::getX() const { return x; }
double TanVector::getY() const { return y; }
double TanVector::getZ() const { return z; }
void TanVector::setX(double x) { this->x = x; }
void TanVector::setY(double y) { this->y = y; }
void TanVector::setZ(double z) { this->z = z; }
double TanVector::length() const
{
return std::sqrt(x * x + y * y + z * z);
}
double TanVector::distanceTo(const TanVector & vec) const
{
return std::sqrt((x - vec.x) * (x - vec.x) +
(y - vec.y) * (y - vec.y) +
(z - vec.z) * (z - vec.z));
}
TanVector TanVector::crossProduct(const TanVector & vec1, const TanVector & vec2)
{
TanVector vecRe;
vecRe.x = vec1.y * vec2.z - vec1.z * vec2.y;
vecRe.y = vec1.z * vec2.x - vec1.x * vec2.z;
vecRe.z = vec1.x * vec2.y - vec1.y * vec2.x;
return vecRe;
}
bool TanVector::opposite(const TanVector & vec1, const TanVector & vec2)
{
// 如果有零向量,则不应该认为是反向向量
if (vec1.length() < _TOLERANCE_ || vec2.length() < _TOLERANCE_) return false;
TanVector vec = vec1.normalize() + vec2.normalize();
return vec.length() < _TOLERANCE_;
}
TanVector TanVector::normalize() const
{
TanVector vec;
double len = length();
if (len > _TOLERANCE_)
{
vec.x = x / len;
vec.y = y / len;
vec.z = z / len;
}
return vec;
}
bool operator < (const TanVector & vec1, const TanVector & vec2)
{
if (vec1.x < vec2.x) return true;
if ((std::abs(vec1.x - vec2.x) < TanVector::_TOLERANCE_) && (vec1.y < vec2.y)) return true;
if ((std::abs(vec1.y - vec2.y) < TanVector::_TOLERANCE_) && (vec1.z < vec2.z)) return true;
return false;
}
TanVector operator + (const TanVector & vec1, const TanVector & vec2)
{
TanVector vec(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z + vec2.z);
return vec;
}
TanVector operator - (const TanVector & vec1, const TanVector & vec2)
{
TanVector vec(vec1.x - vec2.x, vec1.y - vec2.y, vec1.z - vec2.z);
return vec;
}
TanVector operator * (double scale, const TanVector & vec)
{
TanVector v(scale * vec.x, scale * vec.y, scale * vec.z);
return v;
}