X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvector.cpp;h=57f49fbb8b1d4bc7ae1566c91cdb58586691895f;hb=742d2aa9bb46bce4f690474fa22f5980e175e55e;hp=11ddb6494962b724e8166b05502baa27612c0295;hpb=fd5a80446b2abfdfb9d8951fcc03fb1b55ad707c;p=architektonas diff --git a/src/vector.cpp b/src/vector.cpp index 11ddb64..57f49fb 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -31,6 +31,15 @@ Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y) } +// Create vector from angle + length (2D; z is set to zero) +void Vector::SetAngleAndLength(double angle, double length) +{ + x = cos(angle) * length; + y = sin(angle) * length; + z = 0; +} + + Vector Vector::operator=(Vector const v) { x = v.x, y = v.y, z = v.z; @@ -185,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); @@ -195,17 +205,17 @@ 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 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; } @@ -219,13 +229,13 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) // Class methods -double Vector::Dot(Vector v1, Vector v2) +/*static*/ double Vector::Dot(Vector v1, Vector v2) { return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); } -double Vector::Magnitude(Vector v1, Vector v2) +/*static*/ double Vector::Magnitude(Vector v1, Vector v2) { double xx = v1.x - v2.x; double yy = v1.y - v2.y; @@ -234,10 +244,19 @@ double Vector::Magnitude(Vector v1, Vector v2) } +// +// 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). -double Vector::Parameter(Vector tail, Vector head, Vector p) +/*static*/ double Vector::Parameter(Vector tail, Vector head, Vector p) { // Geometric interpretation: // The parameterized point on the vector lineSegment is where the normal of @@ -253,15 +272,27 @@ double Vector::Parameter(Vector tail, Vector head, Vector p) } -// Return the normal to the linesegment formed by the passed in points. -// (Not sure which is head or tail, or which hand the normal lies) -// [v1 should be the tail, v2 should be the head, in which case the normal should -// rotate anti-clockwise.] -///*static*/ Vector Vector::Normal(Vector v1, Vector v2) +// Return the 2D normal to the linesegment formed by the passed in points. +// The normal thus calculated should rotate anti-clockwise. /*static*/ Vector Vector::Normal(Vector tail, Vector head) { -// Vector v = (v1 - v2).Unit(); Vector v = (head - tail).Unit(); return Vector(-v.y, v.x); } + +/*static*/ double Vector::AngleBetween(Vector a, Vector b) +{ + // 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°. + // 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. + if (a.isZero() || b.isZero()) + return 0; + + return acos(a.Dot(b) / (a.Magnitude() * b.Magnitude())); +} +