]> Shamusworld >> Repos - architektonas/blobdiff - src/vector.cpp
Converted codebase from Qt4 to Qt5.
[architektonas] / src / vector.cpp
index 6147c6d71d4021c0e10744b482e2b7796bb93c72..a60c1334fd4469897510a350d86c6ccf3d8d37d8 100644 (file)
@@ -228,13 +228,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;
@@ -246,7 +246,7 @@ double Vector::Magnitude(Vector v1, Vector v2)
 // 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
@@ -262,13 +262,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.]
+// 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 = (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()));
+}
+