2 // vector.cpp: Various structures used for 3 dimensional imaging
\r
4 // by James L. Hammons
\r
5 // (C) 2006 Underground Software
\r
7 // JLH = James L. Hammons <jlhamm@acm.org>
\r
10 // --- ---------- ------------------------------------------------------------
\r
11 // JLH 09/19/2006 Created this file
\r
12 // JLH 03/22/2011 Moved implementation of constructor from header to here
\r
13 // JLH 04/02/2011 Fixed divide-by-zero bug in Unit(), added Angle() function
\r
18 #include <math.h> // For sqrt()
\r
19 #include "mathconstants.h"
\r
22 // Vector implementation
\r
24 Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)
\r
28 Vector Vector::operator=(Vector const v)
\r
30 x = v.x, y = v.y, z = v.z;
\r
35 Vector Vector::operator+(Vector const v)
\r
37 return Vector(x + v.x, y + v.y, z + v.z);
\r
40 Vector Vector::operator-(Vector const v)
\r
42 return Vector(x - v.x, y - v.y, z - v.z);
\r
47 Vector Vector::operator-(void)
\r
49 return Vector(-x, -y, -z);
\r
52 // Vector x constant
\r
54 Vector Vector::operator*(double const v)
\r
56 return Vector(x * v, y * v, z * v);
\r
59 // Vector x constant
\r
61 Vector Vector::operator*(float const v)
\r
63 return Vector(x * v, y * v, z * v);
\r
66 // Vector / constant
\r
68 Vector Vector::operator/(double const v)
\r
70 return Vector(x / v, y / v, z / v);
\r
73 // Vector / constant
\r
75 Vector Vector::operator/(float const v)
\r
77 return Vector(x / v, y / v, z / v);
\r
80 // Vector (cross) product
\r
82 Vector Vector::operator*(Vector const v)
\r
84 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
\r
85 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
\r
90 double Vector::Dot(Vector const v)
\r
92 return (x * v.x) + (y * v.y) + (z * v.z);
\r
96 // Vector x constant, self assigned
\r
98 Vector& Vector::operator*=(double const v)
\r
100 x *= v, y *= v, z *= v;
\r
105 // Vector / constant, self assigned
\r
107 Vector& Vector::operator/=(double const v)
\r
109 x /= v, y /= v, z /= v;
\r
114 // Vector + vector, self assigned
\r
116 Vector& Vector::operator+=(Vector const v)
\r
118 x += v.x, y += v.y, z += v.z;
\r
123 // Vector + constant, self assigned
\r
125 Vector& Vector::operator+=(double const v)
\r
127 x += v, y += v, z += v;
\r
133 Vector Vector::Unit(void)
\r
135 double mag = Magnitude();
\r
137 // If the magnitude of the vector is zero, then the Unit vector is undefined...
\r
139 return Vector(0, 0, 0);
\r
141 return Vector(x / mag, y / mag, z / mag);
\r
144 double Vector::Magnitude(void)
\r
146 return sqrt(x * x + y * y + z * z);
\r
149 double Vector::Angle(void)
\r
151 // acos returns a value between zero and PI, which means we don't know which
\r
152 // quadrant the angle is in... Though, if the y-coordinate of the vector is
\r
153 // negative, that means that the angle is in quadrants III - IV.
\r
154 double rawAngle = acos(Unit().x);
\r
155 double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
\r
157 return correctedAngle;
\r
160 bool Vector::isZero(double epsilon/*= 1e-6*/)
\r
162 return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
\r
168 double Vector::Dot(Vector v1, Vector v2)
\r
170 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
\r