-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2d.cpp
More file actions
141 lines (126 loc) · 3.05 KB
/
Copy pathvector2d.cpp
File metadata and controls
141 lines (126 loc) · 3.05 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "vector2d.h"
/*!
* \details Sets each element to zero.
*/
void Vec2D::setZero()
{
X = 0.0;
Y = 0.0;
}
/*!
* \details Calculates the dot product of two vectors.
* NB: this is a STATIC function!
* \param[in] a the first vector
* \param[in] b the second vector
* \return the resulting scalar
*/
double Vec2D::dot(const Vec2D& a, const Vec2D& b)
{
return a.X * b.X + a.Y * b.Y;
}
void Vec2D::normalise()
{
double length2 = this->getLengthSquared();
if (length2 == 0)
{
std::cerr<<"Normalizing a vector of length 0\n";
}
*this /= std::sqrt(length2);
}
void Vec2D::setLength(double length)
{
this->normalise();
*this *= length;
}
double Vec2D::cross(const Vec2D& a, const Vec2D& b)
{
return a.X * b.Y - a.Y * b.X;
}
double Vec2D::getDistance(const Vec2D& a, const Vec2D& b)
{
return std::sqrt(getDistanceSquared(a, b));
}
double Vec2D::getLengthSquared() const
{
return (X * X + Y * Y);
}
double Vec2D::getLength() const
{
return std::sqrt(getLengthSquared());
}
double Vec2D::getComponent(const int index) const
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
default:
std::cerr<<"[Vector::getComponent] Index is too high for a 2D vector (should be 0-1).\n";
return 0;
}
}
void Vec2D::setComponent(const int index, const double val)
{
switch (index)
{
case 0:
X = val;
break;
case 1:
Y = val;
break;
default:
std::cerr<<"[Vector::setComponent] Index is too high for a 2D vector (should be 0-1).\n";
}
}
bool Vec2D::isEqualTo(const Vec2D& other, const double tol) const
{
if ((Vec2D::getLengthSquared(*this - other)) <= tol * tol)
{
return true;
}
else
{
return false;
}
}
Vec2D Vec2D::getUnitVector(const Vec2D& a)
{
double Length2 = a.getLengthSquared();
if (Length2 != 0.0)
return a / std::sqrt(Length2);
else
return Vec2D(0, 0);
}
double Vec2D::getLength(const Vec2D& a)
{
return a.getLength();
}
/*!
* \details Adds all elements of the vector to an output stream.
* NB: this is a global function and a friend of the Vec3D class!
* \param[in] os the output stream,
* \param[in] a The vector of interest
* \return the output stream with vector elements added
*/
std::ostream& operator<<(std::ostream& os, const Vec2D& a)
{
os << a.X << ' ' << a.Y;
return os;
}
/*!
* \details Reads all elements of a given vector from an input stream.
* NB: this is a global function and a friend of the Vec3D class!
* \param[in,out] is the input stream
* \param[in,out] a the vector to be read in
* \return the input stream from which the vector elements were read
*/
std::istream& operator>>(std::istream& is, Vec2D& a)
{
//TW: clearing the stream avoids the nasty problem that the failbit is set to true if numbers below DBL_MIN=1e-308 are read.
is >> a.X; is.clear();
is >> a.Y;
return is;
}