]> Shamusworld >> Repos - architektonas/blobdiff - src/vector.cpp
Added ability to edit grid unit in base units, work on Dimension.
[architektonas] / src / vector.cpp
index e992bbffd53aa5496bffde85c0ff6b543f4891fd..ab1433f9c6a5123e6ca6a8475cbe852210ecb933 100644 (file)
@@ -11,6 +11,7 @@
 // JLH  09/19/2006  Created this file\r
 // JLH  03/22/2011  Moved implementation of constructor from header to here\r
 // JLH  04/02/2011  Fixed divide-by-zero bug in Unit(), added Angle() function\r
+// JLH  08/04/2013  Added Parameter() function\r
 //\r
 \r
 #include "vector.h"\r
 #include <math.h>                                                              // For sqrt()\r
 #include "mathconstants.h"\r
 \r
-\r
 // Vector implementation\r
 \r
 Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz)\r
 {\r
 }\r
 \r
+\r
 Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)\r
 {\r
 }\r
 \r
+\r
 Vector Vector::operator=(Vector const v)\r
 {\r
        x = v.x, y = v.y, z = v.z;\r
@@ -36,16 +38,19 @@ Vector Vector::operator=(Vector const v)
        return *this;\r
 }\r
 \r
+\r
 Vector Vector::operator+(Vector const v)\r
 {\r
        return Vector(x + v.x, y + v.y, z + v.z);\r
 }\r
 \r
+\r
 Vector Vector::operator-(Vector const v)\r
 {\r
        return Vector(x - v.x, y - v.y, z - v.z);\r
 }\r
 \r
+\r
 // Unary negation\r
 \r
 Vector Vector::operator-(void)\r
@@ -53,6 +58,7 @@ Vector Vector::operator-(void)
        return Vector(-x, -y, -z);\r
 }\r
 \r
+\r
 // Vector x constant\r
 \r
 Vector Vector::operator*(double const v)\r
@@ -60,6 +66,7 @@ Vector Vector::operator*(double const v)
        return Vector(x * v, y * v, z * v);\r
 }\r
 \r
+\r
 // Vector x constant\r
 \r
 Vector Vector::operator*(float const v)\r
@@ -67,6 +74,7 @@ Vector Vector::operator*(float const v)
        return Vector(x * v, y * v, z * v);\r
 }\r
 \r
+\r
 // Vector / constant\r
 \r
 Vector Vector::operator/(double const v)\r
@@ -74,6 +82,7 @@ Vector Vector::operator/(double const v)
        return Vector(x / v, y / v, z / v);\r
 }\r
 \r
+\r
 // Vector / constant\r
 \r
 Vector Vector::operator/(float const v)\r
@@ -81,6 +90,7 @@ Vector Vector::operator/(float const v)
        return Vector(x / v, y / v, z / v);\r
 }\r
 \r
+\r
 // Vector (cross) product\r
 \r
 Vector Vector::operator*(Vector const v)\r
@@ -89,6 +99,7 @@ Vector Vector::operator*(Vector const v)
        return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));\r
 }\r
 \r
+\r
 // Dot product\r
 \r
 double Vector::Dot(Vector const v)\r
@@ -106,6 +117,7 @@ Vector& Vector::operator*=(double const v)
        return *this;\r
 }\r
 \r
+\r
 // Vector / constant, self assigned\r
 \r
 Vector& Vector::operator/=(double const v)\r
@@ -124,6 +136,7 @@ Vector& Vector::operator+=(Vector const v)
        return *this;\r
 }\r
 \r
+\r
 // Vector + constant, self assigned\r
 \r
 Vector& Vector::operator+=(double const v)\r
@@ -133,6 +146,7 @@ Vector& Vector::operator+=(double const v)
        return *this;\r
 }\r
 \r
+\r
 // Vector - vector, self assigned\r
 \r
 Vector& Vector::operator-=(Vector const v)\r
@@ -142,6 +156,7 @@ Vector& Vector::operator-=(Vector const v)
        return *this;\r
 }\r
 \r
+\r
 // Vector - constant, self assigned\r
 \r
 Vector& Vector::operator-=(double const v)\r
@@ -151,18 +166,21 @@ Vector& Vector::operator-=(double const v)
        return *this;\r
 }\r
 \r
+\r
 // Check for equality\r
 bool Vector::operator==(Vector const v)\r
 {\r
        return (x == v.x && y == v.y && z == v.z ? true : false);\r
 }\r
 \r
+\r
 // Check for inequality\r
 bool Vector::operator!=(Vector const v)\r
 {\r
        return (x != v.x || y != v.y || z != v.z ? true : false);\r
 }\r
 \r
+\r
 Vector Vector::Unit(void)\r
 {\r
        double mag = Magnitude();\r
@@ -174,11 +192,13 @@ Vector Vector::Unit(void)
        return Vector(x / mag, y / mag, z / mag);\r
 }\r
 \r
+\r
 double Vector::Magnitude(void)\r
 {\r
        return sqrt(x * x + y * y + z * z);\r
 }\r
 \r
+\r
 double Vector::Angle(void)\r
 {\r
        // acos returns a value between zero and PI, which means we don't know which\r
@@ -190,6 +210,7 @@ double Vector::Angle(void)
        return correctedAngle;\r
 }\r
 \r
+\r
 bool Vector::isZero(double epsilon/*= 1e-6*/)\r
 {\r
        return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);\r
@@ -203,6 +224,7 @@ double Vector::Dot(Vector v1, Vector v2)
        return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);\r
 }\r
 \r
+\r
 double Vector::Magnitude(Vector v1, Vector v2)\r
 {\r
        double xx = v1.x - v2.x;\r
@@ -210,3 +232,23 @@ double Vector::Magnitude(Vector v1, Vector v2)
        double zz = v1.z - v2.z;\r
        return sqrt(xx * xx + yy * yy + zz * zz);\r
 }\r
+\r
+\r
+// Returns the parameter of a point in space to this vector. If the parameter\r
+// is between 0 and 1, the normal of the vector to the point is on the vector.\r
+double Vector::Parameter(Vector v1, Vector v2, Vector p)\r
+{\r
+       // Geometric interpretation:\r
+       // The parameterized point on the vector lineSegment is where the normal of\r
+       // the lineSegment to the point intersects lineSegment. If the pp < 0, then\r
+       // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the\r
+       // perpendicular lies beyond the 2nd endpoint.\r
+\r
+       Vector lineSegment = v2 - v1;\r
+       double magnitude = lineSegment.Magnitude();\r
+       Vector pointSegment = p - v1;\r
+       double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);\r
+\r
+       return t;\r
+}\r
+\r