4 // VECTOR.H - vector class definition
7 // (C) 2004 Underground Software
9 // JLH = James L. Hammons <jlhamm@acm.org>
12 // --- ---------- -------------------------------------------------------------
13 // JLH ??/??/2003 Created original implementation
14 // JLH 05/14/2004 Separated header from implementation, added operator-
16 // JLH 05/15/2004 Added operator+ function
22 vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,
23 double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):
24 x(a1 - a2), y(b1 - b2), z(c1 - c2)
28 vector::vector(const vector &v1, const vector &v2):
29 x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)
33 vector& vector::operator=(const vector &v)
35 x = v.x, y = v.y, z = v.z;
39 bool vector::operator==(const vector &v)
41 if ((x == v.x) && (y == v.y) && (z == v.z))
47 void vector::unitize(void)
49 double dist = sqrt(x*x + y*y + z*z);
52 x /= dist, y /= dist, z /= dist;
64 vector vector::operator*(const vector &v) // Cross product: "this" x "v"
68 r.x = (y * v.z) - (v.y * z);
69 r.y = -((x * v.z) - (v.x * z));
70 r.z = (x * v.y) - (v.x * y);
75 vector vector::operator+(const vector &v)
77 return vector(x + v.x, y + v.y, z + v.z);
80 vector vector::operator-(const vector &v)
82 return vector(x, y, z, v.x, v.y, v.z);
85 double vector::dot(const vector &v1, const vector &v2)
87 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
90 double vector::dot(const vector &v)
92 return x * v.x + y * v.y + z * v.z;
95 double vector::distance(const vector &v) // Pythagoras extended to 3 dimensions
97 double a = x - v.x, b = y - v.y, c = z - v.z;
99 return sqrt(a * a + b * b + c * c);
102 double vector::length(void)
104 return sqrt(x * x + y * y + z * z);
107 void vector::operator*=(const double &d)
109 x *= d, y *= d, z *= d;
112 void vector::operator/=(const double &d)
115 x /= d, y /= d, z /= d;
118 void vector::operator+=(const vector &v)
120 x += v.x, y += v.y, z += v.z;
123 void vector::operator-=(const vector &v)
125 x -= v.x, y -= v.y, z -= v.z;
128 vector vector::operator*(const double &d) // Scale vector by amount
130 return vector(x * d, y * d, z * d);
133 void vector::zero(const double epsilon/*= 1.0e-6*/)
135 if (fabs(x) < epsilon)
138 if (fabs(y) < epsilon)
141 if (fabs(z) < epsilon)
148 // vector.cpp: Various structures used for 3 dimensional imaging
151 // (C) 2006 Underground Software
153 // JLH = James L. Hammons <jlhamm@acm.org>
156 // --- ---------- ------------------------------------------------------------
157 // JLH 09/19/2006 Created this file
158 // JLH 03/22/2011 Moved implementation of constructor from header to here
159 // JLH 04/02/2011 Fixed divide-by-zero bug in Unit(), added Angle() function
164 #include <math.h> // For sqrt()
165 //#include "mathconstants.h"
167 #define PI 3.14159265358979323846264338327
168 #define RADIANS_TO_DEGREES (180.0 / PI)
169 #define DEGREES_TO_RADIANS (PI / 180.0)
172 // Vector implementation
174 Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/,
175 double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/):
176 x(x1 - x2), y(y1 - y2), z(z1 - z2)
181 Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
186 Vector Vector::operator=(Vector const v)
188 x = v.x, y = v.y, z = v.z;
194 Vector Vector::operator+(Vector const v)
196 return Vector(x + v.x, y + v.y, z + v.z);
200 Vector Vector::operator-(Vector const v)
202 return Vector(x - v.x, y - v.y, z - v.z);
208 Vector Vector::operator-(void)
210 return Vector(-x, -y, -z);
216 Vector Vector::operator*(double const v)
218 return Vector(x * v, y * v, z * v);
224 Vector Vector::operator*(float const v)
226 return Vector(x * v, y * v, z * v);
232 Vector Vector::operator/(double const v)
234 return Vector(x / v, y / v, z / v);
240 Vector Vector::operator/(float const v)
242 return Vector(x / v, y / v, z / v);
246 // Vector (cross) product
248 Vector Vector::operator*(Vector const v)
250 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
251 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
257 double Vector::Dot(Vector const v)
259 return (x * v.x) + (y * v.y) + (z * v.z);
263 // Vector x constant, self assigned
265 Vector& Vector::operator*=(double const v)
267 x *= v, y *= v, z *= v;
273 // Vector / constant, self assigned
275 Vector& Vector::operator/=(double const v)
277 x /= v, y /= v, z /= v;
283 // Vector + vector, self assigned
285 Vector& Vector::operator+=(Vector const v)
287 x += v.x, y += v.y, z += v.z;
293 // Vector + constant, self assigned
295 Vector& Vector::operator+=(double const v)
297 x += v, y += v, z += v;
303 // Vector - vector, self assigned
305 Vector& Vector::operator-=(Vector const v)
307 x -= v.x, y -= v.y, z -= v.z;
313 // Vector - constant, self assigned
315 Vector& Vector::operator-=(double const v)
317 x -= v, y -= v, z -= v;
323 // Check for equality
324 bool Vector::operator==(Vector const v)
326 return ((x == v.x) && (y == v.y) && (z == v.z) ? true : false);
330 // Check for inequality
331 bool Vector::operator!=(Vector const v)
333 return ((x != v.x) || (y != v.y) || (z != v.z) ? true : false);
337 Vector Vector::Unit(void)
339 double mag = Magnitude();
341 // If the magnitude of the vector is zero, then the Unit vector is undefined...
343 return Vector(0, 0, 0);
345 return Vector(x / mag, y / mag, z / mag);
349 double Vector::Magnitude(void)
351 return sqrt((x * x) + (y * y) + (z * z));
355 double Vector::Angle(void)
357 // acos returns a value between zero and PI, which means we don't know which
358 // quadrant the angle is in... Though, if the y-coordinate of the vector is
359 // negative, that means that the angle is in quadrants III - IV.
360 double rawAngle = acos(Unit().x);
361 double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
363 return correctedAngle;
368 // Returns the smallest angle between these two vectors
370 double Vector::Angle(Vector v)
372 // seems that something relies on this bad behavior... :-P
374 // Discard the sign from the subtraction
375 double angle = fabs(Angle() - v.Angle());
377 // Return the complementary angle if greater than 180⁰
378 return (angle <= 180.0 ? angle : 360.0 - angle);
380 return Angle() - v.Angle();
385 bool Vector::isZero(double epsilon/*= 1e-6*/)
387 return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false);
393 double Vector::Dot(Vector v1, Vector v2)
395 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
399 double Vector::Magnitude(Vector v1, Vector v2)
401 double xx = v1.x - v2.x;
402 double yy = v1.y - v2.y;
403 double zz = v1.z - v2.z;
404 return sqrt((xx * xx) + (yy * yy) + (zz * zz));