2 // vector.cpp: Various structures used for 3 dimensional imaging
\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(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
\r
32 Vector Vector::operator=(Vector const v)
\r
34 x = v.x, y = v.y, z = v.z;
\r
39 Vector Vector::operator+(Vector const v)
\r
41 return Vector(x + v.x, y + v.y, z + v.z);
\r
44 Vector Vector::operator-(Vector const v)
\r
46 return Vector(x - v.x, y - v.y, z - v.z);
\r
51 Vector Vector::operator-(void)
\r
53 return Vector(-x, -y, -z);
\r
56 // Vector x constant
\r
58 Vector Vector::operator*(double const v)
\r
60 return Vector(x * v, y * v, z * v);
\r
63 // Vector x constant
\r
65 Vector Vector::operator*(float const v)
\r
67 return Vector(x * v, y * v, z * v);
\r
70 // Vector / constant
\r
72 Vector Vector::operator/(double const v)
\r
74 return Vector(x / v, y / v, z / v);
\r
77 // Vector / constant
\r
79 Vector Vector::operator/(float const v)
\r
81 return Vector(x / v, y / v, z / v);
\r
84 // Vector (cross) product
\r
86 Vector Vector::operator*(Vector const v)
\r
88 // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
\r
89 return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
\r
94 double Vector::Dot(Vector const v)
\r
96 return (x * v.x) + (y * v.y) + (z * v.z);
\r
100 // Vector x constant, self assigned
\r
102 Vector& Vector::operator*=(double const v)
\r
104 x *= v, y *= v, z *= v;
\r
109 // Vector / constant, self assigned
\r
111 Vector& Vector::operator/=(double const v)
\r
113 x /= v, y /= v, z /= v;
\r
118 // Vector + vector, self assigned
\r
120 Vector& Vector::operator+=(Vector const v)
\r
122 x += v.x, y += v.y, z += v.z;
\r
127 // Vector + constant, self assigned
\r
129 Vector& Vector::operator+=(double const v)
\r
131 x += v, y += v, z += v;
\r
136 // Vector - vector, self assigned
\r
138 Vector& Vector::operator-=(Vector const v)
\r
140 x -= v.x, y -= v.y, z -= v.z;
\r
145 // Vector - constant, self assigned
\r
147 Vector& Vector::operator-=(double const v)
\r
149 x -= v, y -= v, z -= v;
\r
154 // Check for equality
\r
155 bool Vector::operator==(Vector const v)
\r
157 return (x == v.x && y == v.y && z == v.z ? true : false);
\r
160 // Check for inequality
\r
161 bool Vector::operator!=(Vector const v)
\r
163 return (x != v.x || y != v.y || z != v.z ? true : false);
\r
166 Vector Vector::Unit(void)
\r
168 double mag = Magnitude();
\r
170 // If the magnitude of the vector is zero, then the Unit vector is undefined...
\r
172 return Vector(0, 0, 0);
\r
174 return Vector(x / mag, y / mag, z / mag);
\r
177 double Vector::Magnitude(void)
\r
179 return sqrt(x * x + y * y + z * z);
\r
182 double Vector::Angle(void)
\r
184 // acos returns a value between zero and PI, which means we don't know which
\r
185 // quadrant the angle is in... Though, if the y-coordinate of the vector is
\r
186 // negative, that means that the angle is in quadrants III - IV.
\r
187 double rawAngle = acos(Unit().x);
\r
188 double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
\r
190 return correctedAngle;
\r
193 bool Vector::isZero(double epsilon/*= 1e-6*/)
\r
195 return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
\r
201 double Vector::Dot(Vector v1, Vector v2)
\r
203 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
\r
206 double Vector::Magnitude(Vector v1, Vector v2)
\r
208 double xx = v1.x - v2.x;
\r
209 double yy = v1.y - v2.y;
\r
210 double zz = v1.z - v2.z;
\r
211 return sqrt(xx * xx + yy * yy + zz * zz);
\r