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