X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fvector.cpp;h=7458193e23dc9c8bed39cdbe9cfd2b52396e291e;hb=29b571499a38273c6c693334512e44f4162171a5;hp=fc008f3e542d9b39f214f69ca7229b365ce70728;hpb=6d13a5166688e470590692eb91c3915ab332fe36;p=ttedit diff --git a/src/vector.cpp b/src/vector.cpp index fc008f3..7458193 100755 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -1,141 +1,141 @@ -// -// VECTOR.H - vector class definition -// -// by James L. Hammons -// (C) 2004 Underground Software -// -// JLH = James L. Hammons -// -// Who When What -// --- ---------- ------------------------------------------------------------- -// JLH ??/??/2003 Created original implementation -// JLH 05/14/2004 Separated header from implementation, added operator- -// function -// JLH 05/15/2004 Added operator+ function -// - -#include -#include "vector.h" - -vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/, - double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/): - x(a1 - a2), y(b1 - b2), z(c1 - c2) -{ -} - -vector::vector(const vector &v1, const vector &v2): - x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z) -{ -} - -vector& vector::operator=(const vector &v) -{ - x = v.x, y = v.y, z = v.z; - return *this; -} - -bool vector::operator==(const vector &v) -{ - if ((x == v.x) && (y == v.y) && (z == v.z)) - return true; - - return false; -} - -void vector::unitize(void) -{ - double dist = sqrt(x*x + y*y + z*z); - - if (dist != 0.0) - x /= dist, y /= dist, z /= dist; - - if (x == -0.0) - x = +0.0; - - if (y == -0.0) - y = +0.0; - - if (z == -0.0) - z = +0.0; -} - -vector vector::operator*(const vector &v) // Cross product: "this" x "v" -{ - vector r; - - r.x = (y * v.z) - (v.y * z); - r.y = -((x * v.z) - (v.x * z)); - r.z = (x * v.y) - (v.x * y); - - return r; -} - -vector vector::operator+(const vector &v) -{ - return vector(x + v.x, y + v.y, z + v.z); -} - -vector vector::operator-(const vector &v) -{ - return vector(x, y, z, v.x, v.y, v.z); -} - -double vector::dot(const vector &v1, const vector &v2) -{ - return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; -} - -double vector::dot(const vector &v) -{ - return x * v.x + y * v.y + z * v.z; -} - -double vector::distance(const vector &v) // Pythagoras extended to 3 dimensions -{ - double a = x - v.x, b = y - v.y, c = z - v.z; - - return sqrt(a * a + b * b + c * c); -} - -double vector::length(void) -{ - return sqrt(x * x + y * y + z * z); -} - -void vector::operator*=(const double &d) -{ - x *= d, y *= d, z *= d; -} - -void vector::operator/=(const double &d) -{ - if (d != 0.0) - x /= d, y /= d, z /= d; -} - -void vector::operator+=(const vector &v) -{ - x += v.x, y += v.y, z += v.z; -} - -void vector::operator-=(const vector &v) -{ - x -= v.x, y -= v.y, z -= v.z; -} - -vector vector::operator*(const double &d) // Scale vector by amount -{ - return vector(x * d, y * d, z * d); -} - -void vector::zero(const double epsilon) -{ - if (fabs(x) < epsilon) - x = 0.0; - - if (fabs(y) < epsilon) - y = 0.0; - - if (fabs(z) < epsilon) - z = 0.0; -} +// +// VECTOR.H - vector class definition +// +// by James L. Hammons +// (C) 2004 Underground Software +// +// JLH = James L. Hammons +// +// Who When What +// --- ---------- ------------------------------------------------------------- +// JLH ??/??/2003 Created original implementation +// JLH 05/14/2004 Separated header from implementation, added operator- +// function +// JLH 05/15/2004 Added operator+ function +// + +#include +#include "vector.h" + +vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/, + double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/): + x(a1 - a2), y(b1 - b2), z(c1 - c2) +{ +} + +vector::vector(const vector &v1, const vector &v2): + x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z) +{ +} + +vector& vector::operator=(const vector &v) +{ + x = v.x, y = v.y, z = v.z; + return *this; +} + +bool vector::operator==(const vector &v) +{ + if ((x == v.x) && (y == v.y) && (z == v.z)) + return true; + + return false; +} + +void vector::unitize(void) +{ + double dist = sqrt(x*x + y*y + z*z); + + if (dist != 0.0) + x /= dist, y /= dist, z /= dist; + + if (x == -0.0) + x = +0.0; + + if (y == -0.0) + y = +0.0; + + if (z == -0.0) + z = +0.0; +} + +vector vector::operator*(const vector &v) // Cross product: "this" x "v" +{ + vector r; + + r.x = (y * v.z) - (v.y * z); + r.y = -((x * v.z) - (v.x * z)); + r.z = (x * v.y) - (v.x * y); + + return r; +} + +vector vector::operator+(const vector &v) +{ + return vector(x + v.x, y + v.y, z + v.z); +} + +vector vector::operator-(const vector &v) +{ + return vector(x, y, z, v.x, v.y, v.z); +} + +double vector::dot(const vector &v1, const vector &v2) +{ + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; +} + +double vector::dot(const vector &v) +{ + return x * v.x + y * v.y + z * v.z; +} + +double vector::distance(const vector &v) // Pythagoras extended to 3 dimensions +{ + double a = x - v.x, b = y - v.y, c = z - v.z; + + return sqrt(a * a + b * b + c * c); +} + +double vector::length(void) +{ + return sqrt(x * x + y * y + z * z); +} + +void vector::operator*=(const double &d) +{ + x *= d, y *= d, z *= d; +} + +void vector::operator/=(const double &d) +{ + if (d != 0.0) + x /= d, y /= d, z /= d; +} + +void vector::operator+=(const vector &v) +{ + x += v.x, y += v.y, z += v.z; +} + +void vector::operator-=(const vector &v) +{ + x -= v.x, y -= v.y, z -= v.z; +} + +vector vector::operator*(const double &d) // Scale vector by amount +{ + return vector(x * d, y * d, z * d); +} + +void vector::zero(const double epsilon) +{ + if (fabs(x) < epsilon) + x = 0.0; + + if (fabs(y) < epsilon) + y = 0.0; + + if (fabs(z) < epsilon) + z = 0.0; +}