X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvector.cpp;h=baa0422869b125aa24dca41e8d1c32ed016b58cd;hb=HEAD;hp=1f4c29cc5ae5b69eedd639efaa5fa0a6bc56883c;hpb=cf3ec188764cdf34ff3472ee9806aba3a772d2df;p=ttedit diff --git a/src/vector.cpp b/src/vector.cpp old mode 100755 new mode 100644 index 1f4c29c..baa0422 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -9,7 +9,7 @@ // JLH = James L. Hammons // // Who When What -// --- ---------- ------------------------------------------------------------- +// --- ---------- ----------------------------------------------------------- // JLH ??/??/2003 Created original implementation // JLH 05/14/2004 Separated header from implementation, added operator- // function @@ -153,7 +153,7 @@ void vector::zero(const double epsilon/*= 1.0e-6*/) // JLH = James L. Hammons // // WHO WHEN WHAT -// --- ---------- ------------------------------------------------------------ +// --- ---------- ---------------------------------------------------------- // JLH 09/19/2006 Created this file // JLH 03/22/2011 Moved implementation of constructor from header to here // JLH 04/02/2011 Fixed divide-by-zero bug in Unit(), added Angle() function @@ -162,11 +162,7 @@ void vector::zero(const double epsilon/*= 1.0e-6*/) #include "vector.h" #include // For sqrt() -//#include "mathconstants.h" - -#define PI 3.14159265358979323846264338327 -#define RADIANS_TO_DEGREES (180.0 / PI) -#define DEGREES_TO_RADIANS (PI / 180.0) +#include "mathconstants.h" // Vector implementation @@ -323,14 +319,14 @@ Vector& Vector::operator-=(double const v) // Check for equality bool Vector::operator==(Vector const v) { - return (x == v.x && y == v.y && z == v.z ? true : false); + return ((x == v.x) && (y == v.y) && (z == v.z) ? true : false); } // Check for inequality bool Vector::operator!=(Vector const v) { - return (x != v.x || y != v.y || z != v.z ? true : false); + return ((x != v.x) || (y != v.y) || (z != v.z) ? true : false); } @@ -348,34 +344,43 @@ Vector Vector::Unit(void) double Vector::Magnitude(void) { - return sqrt(x * x + y * y + z * z); + return sqrt((x * x) + (y * y) + (z * z)); } double Vector::Angle(void) { - // acos returns a value between zero and PI, which means we don't know which - // quadrant the angle is in... Though, if the y-coordinate of the vector is - // negative, that means that the angle is in quadrants III - IV. + // acos returns a value between zero and PI, which means we don't know + // which quadrant the angle is in... Though, if the y-coordinate of the + // vector is negative, that means that the angle is in quadrants III - IV. double rawAngle = acos(Unit().x); - double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle); + double correctedAngle = (y < 0 ? TAU - rawAngle : rawAngle); return correctedAngle; } // -// Angle between these two vectors +// Returns the smallest angle between these two vectors // double Vector::Angle(Vector v) { +// seems that something relies on this bad behavior... :-P +#if 0 + // Discard the sign from the subtraction + double angle = fabs(Angle() - v.Angle()); + + // Return the complementary angle if greater than 180⁰ + return (angle <= 180.0 ? angle : 360.0 - angle); +#else return Angle() - v.Angle(); +#endif } bool Vector::isZero(double epsilon/*= 1e-6*/) { - return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false); + return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false); } @@ -392,7 +397,7 @@ double Vector::Magnitude(Vector v1, Vector v2) double xx = v1.x - v2.x; double yy = v1.y - v2.y; double zz = v1.z - v2.z; - return sqrt(xx * xx + yy * yy + zz * zz); + return sqrt((xx * xx) + (yy * yy) + (zz * zz)); } #endif