X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvector.cpp;h=ab1433f9c6a5123e6ca6a8475cbe852210ecb933;hb=11802354d1ddc5bc571d83d8fc9b600618cb4372;hp=e992bbffd53aa5496bffde85c0ff6b543f4891fd;hpb=2af76acd67a2703859c11446be3be4d3ff8ff4b4;p=architektonas diff --git a/src/vector.cpp b/src/vector.cpp index e992bbf..ab1433f 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -11,6 +11,7 @@ // 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 +// JLH 08/04/2013 Added Parameter() function // #include "vector.h" @@ -18,17 +19,18 @@ #include // For sqrt() #include "mathconstants.h" - // Vector implementation Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(yy), z(zz) { } + Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z) { } + Vector Vector::operator=(Vector const v) { x = v.x, y = v.y, z = v.z; @@ -36,16 +38,19 @@ Vector Vector::operator=(Vector const v) return *this; } + Vector Vector::operator+(Vector const v) { return Vector(x + v.x, y + v.y, z + v.z); } + Vector Vector::operator-(Vector const v) { return Vector(x - v.x, y - v.y, z - v.z); } + // Unary negation Vector Vector::operator-(void) @@ -53,6 +58,7 @@ Vector Vector::operator-(void) return Vector(-x, -y, -z); } + // Vector x constant Vector Vector::operator*(double const v) @@ -60,6 +66,7 @@ Vector Vector::operator*(double const v) return Vector(x * v, y * v, z * v); } + // Vector x constant Vector Vector::operator*(float const v) @@ -67,6 +74,7 @@ Vector Vector::operator*(float const v) return Vector(x * v, y * v, z * v); } + // Vector / constant Vector Vector::operator/(double const v) @@ -74,6 +82,7 @@ Vector Vector::operator/(double const v) return Vector(x / v, y / v, z / v); } + // Vector / constant Vector Vector::operator/(float const v) @@ -81,6 +90,7 @@ Vector Vector::operator/(float const v) return Vector(x / v, y / v, z / v); } + // Vector (cross) product Vector Vector::operator*(Vector const v) @@ -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)); } + // Dot product double Vector::Dot(Vector const v) @@ -106,6 +117,7 @@ Vector& Vector::operator*=(double const v) return *this; } + // Vector / constant, self assigned Vector& Vector::operator/=(double const v) @@ -124,6 +136,7 @@ Vector& Vector::operator+=(Vector const v) return *this; } + // Vector + constant, self assigned Vector& Vector::operator+=(double const v) @@ -133,6 +146,7 @@ Vector& Vector::operator+=(double const v) return *this; } + // Vector - vector, self assigned Vector& Vector::operator-=(Vector const v) @@ -142,6 +156,7 @@ Vector& Vector::operator-=(Vector const v) return *this; } + // Vector - constant, self assigned Vector& Vector::operator-=(double const v) @@ -151,18 +166,21 @@ Vector& Vector::operator-=(double const v) return *this; } + // Check for equality bool Vector::operator==(Vector const v) { 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); } + Vector Vector::Unit(void) { double mag = Magnitude(); @@ -174,11 +192,13 @@ Vector Vector::Unit(void) return Vector(x / mag, y / mag, z / mag); } + double Vector::Magnitude(void) { 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 @@ -190,6 +210,7 @@ double Vector::Angle(void) return correctedAngle; } + bool Vector::isZero(double epsilon/*= 1e-6*/) { return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false); @@ -203,6 +224,7 @@ 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) { double xx = v1.x - v2.x; @@ -210,3 +232,23 @@ double Vector::Magnitude(Vector v1, Vector v2) double zz = v1.z - v2.z; return sqrt(xx * xx + yy * yy + zz * zz); } + + +// 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. +double Vector::Parameter(Vector v1, Vector v2, Vector p) +{ + // Geometric interpretation: + // The parameterized point on the vector lineSegment is where the normal of + // the lineSegment to the point intersects lineSegment. If the pp < 0, then + // the perpendicular lies beyond the 1st endpoint. If pp > 1, then the + // perpendicular lies beyond the 2nd endpoint. + + Vector lineSegment = v2 - v1; + double magnitude = lineSegment.Magnitude(); + Vector pointSegment = p - v1; + double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude); + + return t; +} +