2 // VECTOR.H - vector class definition
\r
4 // by James L. Hammons
\r
5 // (C) 2004 Underground Software
\r
7 // JLH = James L. Hammons <jlhamm@acm.org>
\r
10 // --- ---------- -------------------------------------------------------------
\r
11 // JLH ??/??/2003 Created original implementation
\r
12 // JLH 05/14/2004 Separated header from implementation, added operator-
\r
14 // JLH 05/15/2004 Added operator+ function
\r
20 vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,
\r
21 double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):
\r
22 x(a1 - a2), y(b1 - b2), z(c1 - c2)
\r
26 vector::vector(const vector &v1, const vector &v2):
\r
27 x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)
\r
31 vector& vector::operator=(const vector &v)
\r
33 x = v.x, y = v.y, z = v.z;
\r
37 bool vector::operator==(const vector &v)
\r
39 if ((x == v.x) && (y == v.y) && (z == v.z))
\r
45 void vector::unitize(void)
\r
47 double dist = sqrt(x*x + y*y + z*z);
\r
50 x /= dist, y /= dist, z /= dist;
\r
62 vector vector::operator*(const vector &v) // Cross product: "this" x "v"
\r
66 r.x = (y * v.z) - (v.y * z);
\r
67 r.y = -((x * v.z) - (v.x * z));
\r
68 r.z = (x * v.y) - (v.x * y);
\r
73 vector vector::operator+(const vector &v)
\r
75 return vector(x + v.x, y + v.y, z + v.z);
\r
78 vector vector::operator-(const vector &v)
\r
80 return vector(x, y, z, v.x, v.y, v.z);
\r
83 double vector::dot(const vector &v1, const vector &v2)
\r
85 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
\r
88 double vector::dot(const vector &v)
\r
90 return x * v.x + y * v.y + z * v.z;
\r
93 double vector::distance(const vector &v) // Pythagoras extended to 3 dimensions
\r
95 double a = x - v.x, b = y - v.y, c = z - v.z;
\r
97 return sqrt(a * a + b * b + c * c);
\r
100 double vector::length(void)
\r
102 return sqrt(x * x + y * y + z * z);
\r
105 void vector::operator*=(const double &d)
\r
107 x *= d, y *= d, z *= d;
\r
110 void vector::operator/=(const double &d)
\r
113 x /= d, y /= d, z /= d;
\r
116 void vector::operator+=(const vector &v)
\r
118 x += v.x, y += v.y, z += v.z;
\r
121 void vector::operator-=(const vector &v)
\r
123 x -= v.x, y -= v.y, z -= v.z;
\r
126 vector vector::operator*(const double &d) // Scale vector by amount
\r
128 return vector(x * d, y * d, z * d);
\r
131 void vector::zero(const double epsilon)
\r
133 if (fabs(x) < epsilon)
\r
136 if (fabs(y) < epsilon)
\r
139 if (fabs(z) < epsilon)
\r