X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvector.cpp;h=20b8ef5f99e5bde36b6020120042d4691a1f222b;hb=0e8f4edf80206248f0ad87a966d69d9ec9da3d2f;hp=a60c1334fd4469897510a350d86c6ccf3d8d37d8;hpb=6c1279871f6bb86bc59e2561b6a7f74ab081f71e;p=architektonas diff --git a/src/vector.cpp b/src/vector.cpp index a60c133..20b8ef5 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -194,7 +194,8 @@ Vector Vector::Unit(void) { double mag = Magnitude(); - // If the magnitude of the vector is zero, then the Unit vector is undefined... + // If the magnitude of the vector is zero, then the Unit vector is + // undefined... if (mag == 0) return Vector(0, 0, 0); @@ -210,11 +211,11 @@ double Vector::Magnitude(void) 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 TAU/2, which means we don't know + // which quadrant the angle is in... However, 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; } @@ -243,6 +244,15 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) } +// +// Convenience function +// +/*static*/ double Vector::Angle(Point p1, Point p2) +{ + return Vector(p1, p2).Angle(); +} + + // Returns the parameter of a point in space to this vector. If the parameter // is between 0 and 1, the normal of the vector to the point is on the vector. // Note: v1 is the tail, v2 is the head of the line (vector). @@ -276,10 +286,10 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) // This is done using the following formula: // (a . b) = ||a|| ||b|| cos(theta) // However, have to check for two degenerate cases, where a = cb: - // 1, if c > 0, theta = 0; 2, if c < 0, theta = 180°. + // 1) if c > 0, theta = 0; 2) if c < 0, theta = 180°. // Also, the vectors a & b have to be non-zero. // Also, have to check using an epsilon because acos will not return an - // exact value if the vectors are orthogonal + // exact value if the vectors are orthogonal. if (a.isZero() || b.isZero()) return 0;