X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvector.cpp;h=89b9148a44343b6aa7f3c00bbf8210099731f97f;hb=5d8c9e52606315fbfe857f2715b8f051b4f97491;hp=57f49fbb8b1d4bc7ae1566c91cdb58586691895f;hpb=742d2aa9bb46bce4f690474fa22f5980e175e55e;p=architektonas diff --git a/src/vector.cpp b/src/vector.cpp index 57f49fb..89b9148 100644 --- a/src/vector.cpp +++ b/src/vector.cpp @@ -25,11 +25,13 @@ Vector::Vector(double xx/*= 0*/, double yy/*= 0*/, double zz/*= 0*/): x(xx), y(y { } - Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z) { } +Vector::Vector(const Vector &v): x(v.x), y(v.y), z(v.z) +{ +} // Create vector from angle + length (2D; z is set to zero) void Vector::SetAngleAndLength(double angle, double length) @@ -39,7 +41,6 @@ void Vector::SetAngleAndLength(double angle, double length) z = 0; } - Vector Vector::operator=(Vector const v) { x = v.x, y = v.y, z = v.z; @@ -47,19 +48,16 @@ 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) @@ -67,7 +65,6 @@ Vector Vector::operator-(void) return Vector(-x, -y, -z); } - // Vector x constant Vector Vector::operator*(double const v) @@ -75,7 +72,6 @@ Vector Vector::operator*(double const v) return Vector(x * v, y * v, z * v); } - // Vector x constant Vector Vector::operator*(float const v) @@ -83,7 +79,6 @@ Vector Vector::operator*(float const v) return Vector(x * v, y * v, z * v); } - // Vector / constant Vector Vector::operator/(double const v) @@ -91,7 +86,6 @@ Vector Vector::operator/(double const v) return Vector(x / v, y / v, z / v); } - // Vector / constant Vector Vector::operator/(float const v) @@ -99,7 +93,6 @@ Vector Vector::operator/(float const v) return Vector(x / v, y / v, z / v); } - // Vector (cross) product Vector Vector::operator*(Vector const v) @@ -108,7 +101,6 @@ 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) @@ -116,7 +108,6 @@ double Vector::Dot(Vector const v) return (x * v.x) + (y * v.y) + (z * v.z); } - // Vector x constant, self assigned Vector& Vector::operator*=(double const v) @@ -126,7 +117,6 @@ Vector& Vector::operator*=(double const v) return *this; } - // Vector / constant, self assigned Vector& Vector::operator/=(double const v) @@ -145,7 +135,6 @@ Vector& Vector::operator+=(Vector const v) return *this; } - // Vector + constant, self assigned Vector& Vector::operator+=(double const v) @@ -155,7 +144,6 @@ Vector& Vector::operator+=(double const v) return *this; } - // Vector - vector, self assigned Vector& Vector::operator-=(Vector const v) @@ -165,7 +153,6 @@ Vector& Vector::operator-=(Vector const v) return *this; } - // Vector - constant, self assigned Vector& Vector::operator-=(double const v) @@ -175,21 +162,18 @@ 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(); @@ -202,13 +186,11 @@ 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 TAU/2, which means we don't know @@ -220,13 +202,11 @@ 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); } - // Class methods /*static*/ double Vector::Dot(Vector v1, Vector v2) @@ -234,16 +214,15 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); } - /*static*/ double Vector::Magnitude(Vector v1, Vector v2) { double xx = v1.x - v2.x; double yy = v1.y - v2.y; double zz = v1.z - v2.z; + return sqrt((xx * xx) + (yy * yy) + (zz * zz)); } - // // Convenience function // @@ -252,7 +231,6 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) 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). @@ -268,19 +246,19 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) double magnitude = lineSegment.Magnitude(); Vector pointSegment = p - tail; double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude); + return t; } - // 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: @@ -295,4 +273,3 @@ bool Vector::isZero(double epsilon/*= 1e-6*/) return acos(a.Dot(b) / (a.Magnitude() * b.Magnitude())); } -