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"
168 // Vector implementation
170 Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/,
171 double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/):
172 x(x1 - x2), y(y1 - y2), z(z1 - z2)
177 Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
182 Vector Vector::operator=(Vector const v)
184 x = v.x, y = v.y, z = v.z;
190 Vector Vector::operator+(Vector const v)
192 return Vector(x + v.x, y + v.y, z + v.z);
196 Vector Vector::operator-(Vector const v)
198 return Vector(x - v.x, y - v.y, z - v.z);
204 Vector Vector::operator-(void)
206 return Vector(-x, -y, -z);
212 Vector Vector::operator*(double const v)
214 return Vector(x * v, y * v, z * v);
220 Vector Vector::operator*(float const v)
222 return Vector(x * v, y * v, z * v);
228 Vector Vector::operator/(double const v)
230 return Vector(x / v, y / v, z / v);
236 Vector Vector::operator/(float const v)
238 return Vector(x / v, y / v, z / v);
242 // Vector (cross) product
244 Vector Vector::operator*(Vector const v)
246 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
247 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
253 double Vector::Dot(Vector const v)
255 return (x * v.x) + (y * v.y) + (z * v.z);
259 // Vector x constant, self assigned
261 Vector& Vector::operator*=(double const v)
263 x *= v, y *= v, z *= v;
269 // Vector / constant, self assigned
271 Vector& Vector::operator/=(double const v)
273 x /= v, y /= v, z /= v;
279 // Vector + vector, self assigned
281 Vector& Vector::operator+=(Vector const v)
283 x += v.x, y += v.y, z += v.z;
289 // Vector + constant, self assigned
291 Vector& Vector::operator+=(double const v)
293 x += v, y += v, z += v;
299 // Vector - vector, self assigned
301 Vector& Vector::operator-=(Vector const v)
303 x -= v.x, y -= v.y, z -= v.z;
309 // Vector - constant, self assigned
311 Vector& Vector::operator-=(double const v)
313 x -= v, y -= v, z -= v;
319 // Check for equality
320 bool Vector::operator==(Vector const v)
322 return ((x == v.x) && (y == v.y) && (z == v.z) ? true : false);
326 // Check for inequality
327 bool Vector::operator!=(Vector const v)
329 return ((x != v.x) || (y != v.y) || (z != v.z) ? true : false);
333 Vector Vector::Unit(void)
335 double mag = Magnitude();
337 // If the magnitude of the vector is zero, then the Unit vector is undefined...
339 return Vector(0, 0, 0);
341 return Vector(x / mag, y / mag, z / mag);
345 double Vector::Magnitude(void)
347 return sqrt((x * x) + (y * y) + (z * z));
351 double Vector::Angle(void)
353 // acos returns a value between zero and PI, which means we don't know
354 // which quadrant the angle is in... Though, if the y-coordinate of the
355 // vector is negative, that means that the angle is in quadrants III - IV.
356 double rawAngle = acos(Unit().x);
357 double correctedAngle = (y < 0 ? TAU - rawAngle : rawAngle);
359 return correctedAngle;
364 // Returns the smallest angle between these two vectors
366 double Vector::Angle(Vector v)
368 // seems that something relies on this bad behavior... :-P
370 // Discard the sign from the subtraction
371 double angle = fabs(Angle() - v.Angle());
373 // Return the complementary angle if greater than 180⁰
374 return (angle <= 180.0 ? angle : 360.0 - angle);
376 return Angle() - v.Angle();
381 bool Vector::isZero(double epsilon/*= 1e-6*/)
383 return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false);
389 double Vector::Dot(Vector v1, Vector v2)
391 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
395 double Vector::Magnitude(Vector v1, Vector v2)
397 double xx = v1.x - v2.x;
398 double yy = v1.y - v2.y;
399 double zz = v1.z - v2.z;
400 return sqrt((xx * xx) + (yy * yy) + (zz * zz));