]> Shamusworld >> Repos - ttedit/blobdiff - src/vector.cpp
Added rectangle point selection, canvas zooming.
[ttedit] / src / vector.cpp
index fc008f3e542d9b39f214f69ca7229b365ce70728..fe627cd329adb397f28c3447512dfcad265c6f02 100755 (executable)
-//\r
-// VECTOR.H - vector class definition\r
-//\r
-// by James L. Hammons\r
-// (C) 2004 Underground Software\r
-//\r
-// JLH = James L. Hammons <jlhamm@acm.org>\r
-//\r
-// Who  When        What\r
-// ---  ----------  -------------------------------------------------------------\r
-// JLH  ??/??/2003  Created original implementation\r
-// JLH  05/14/2004  Separated header from implementation, added operator-\r
-//                  function\r
-// JLH  05/15/2004  Added operator+ function\r
-//\r
-\r
-#include <math.h>\r
-#include "vector.h"\r
-\r
-vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,\r
-       double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):\r
-       x(a1 - a2), y(b1 - b2), z(c1 - c2)\r
-{\r
-}\r
-\r
-vector::vector(const vector &v1, const vector &v2):\r
-       x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)\r
-{\r
-}\r
-\r
-vector& vector::operator=(const vector &v)\r
-{\r
-       x = v.x, y = v.y, z = v.z;\r
-       return *this;\r
-}\r
-\r
-bool vector::operator==(const vector &v)\r
-{\r
-       if ((x == v.x) && (y == v.y) && (z == v.z))\r
-               return true;\r
-\r
-       return false;\r
-}\r
-\r
-void vector::unitize(void)\r
-{\r
-       double dist = sqrt(x*x + y*y + z*z);\r
-\r
-       if (dist != 0.0)\r
-               x /= dist, y /= dist, z /= dist;\r
-\r
-       if (x == -0.0)\r
-               x = +0.0;\r
-\r
-       if (y == -0.0)\r
-               y = +0.0;\r
-\r
-       if (z == -0.0)\r
-               z = +0.0;\r
-}\r
-\r
-vector vector::operator*(const vector &v)              // Cross product: "this" x "v"\r
-{\r
-       vector r;\r
-\r
-       r.x = (y * v.z) - (v.y * z);\r
-       r.y = -((x * v.z) - (v.x * z));\r
-       r.z = (x * v.y) - (v.x * y);\r
-\r
-       return r;\r
-}\r
-\r
-vector vector::operator+(const vector &v)\r
-{\r
-       return vector(x + v.x, y + v.y, z + v.z);\r
-}\r
-\r
-vector vector::operator-(const vector &v)\r
-{\r
-       return vector(x, y, z, v.x, v.y, v.z);\r
-}\r
-\r
-double vector::dot(const vector &v1, const vector &v2)\r
-{\r
-       return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;\r
-}\r
-\r
-double vector::dot(const vector &v)\r
-{\r
-       return x * v.x + y * v.y + z * v.z;\r
-}\r
-\r
-double vector::distance(const vector &v)               // Pythagoras extended to 3 dimensions\r
-{\r
-       double a = x - v.x, b = y - v.y, c = z - v.z;\r
-\r
-       return sqrt(a * a + b * b + c * c);\r
-}\r
-\r
-double vector::length(void)\r
-{\r
-       return sqrt(x * x + y * y + z * z);\r
-}\r
-\r
-void vector::operator*=(const double &d)\r
-{\r
-       x *= d, y *= d, z *= d;\r
-}\r
-\r
-void vector::operator/=(const double &d)\r
-{\r
-       if (d != 0.0)\r
-               x /= d, y /= d, z /= d;\r
-}\r
-\r
-void vector::operator+=(const vector &v)\r
-{\r
-       x += v.x, y += v.y, z += v.z;\r
-}\r
-\r
-void vector::operator-=(const vector &v)\r
-{\r
-       x -= v.x, y -= v.y, z -= v.z;\r
-}\r
-\r
-vector vector::operator*(const double &d)              // Scale vector by amount\r
-{\r
-       return vector(x * d, y * d, z * d);\r
-}\r
-\r
-void vector::zero(const double epsilon)\r
-{\r
-       if (fabs(x) < epsilon)\r
-               x = 0.0;\r
-\r
-       if (fabs(y) < epsilon)\r
-               y = 0.0;\r
-\r
-       if (fabs(z) < epsilon)\r
-               z = 0.0;\r
-}\r
+#if 0
+
+//
+// VECTOR.H - vector class definition
+//
+// by James L. Hammons
+// (C) 2004 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -----------------------------------------------------------
+// JLH  ??/??/2003  Created original implementation
+// JLH  05/14/2004  Separated header from implementation, added operator-
+//                  function
+// JLH  05/15/2004  Added operator+ function
+//
+
+#include "vector.h"
+#include <math.h>
+
+vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,
+       double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):
+       x(a1 - a2), y(b1 - b2), z(c1 - c2)
+{
+}
+
+vector::vector(const vector &v1, const vector &v2):
+       x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)
+{
+}
+
+vector& vector::operator=(const vector &v)
+{
+       x = v.x, y = v.y, z = v.z;
+       return *this;
+}
+
+bool vector::operator==(const vector &v)
+{
+       if ((x == v.x) && (y == v.y) && (z == v.z))
+               return true;
+
+       return false;
+}
+
+void vector::unitize(void)
+{
+       double dist = sqrt(x*x + y*y + z*z);
+
+       if (dist != 0.0)
+               x /= dist, y /= dist, z /= dist;
+
+       if (x == -0.0)
+               x = +0.0;
+
+       if (y == -0.0)
+               y = +0.0;
+
+       if (z == -0.0)
+               z = +0.0;
+}
+
+vector vector::operator*(const vector &v)              // Cross product: "this" x "v"
+{
+       vector r;
+
+       r.x = (y * v.z) - (v.y * z);
+       r.y = -((x * v.z) - (v.x * z));
+       r.z = (x * v.y) - (v.x * y);
+
+       return r;
+}
+
+vector vector::operator+(const vector &v)
+{
+       return vector(x + v.x, y + v.y, z + v.z);
+}
+
+vector vector::operator-(const vector &v)
+{
+       return vector(x, y, z, v.x, v.y, v.z);
+}
+
+double vector::dot(const vector &v1, const vector &v2)
+{
+       return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
+}
+
+double vector::dot(const vector &v)
+{
+       return x * v.x + y * v.y + z * v.z;
+}
+
+double vector::distance(const vector &v)               // Pythagoras extended to 3 dimensions
+{
+       double a = x - v.x, b = y - v.y, c = z - v.z;
+
+       return sqrt(a * a + b * b + c * c);
+}
+
+double vector::length(void)
+{
+       return sqrt(x * x + y * y + z * z);
+}
+
+void vector::operator*=(const double &d)
+{
+       x *= d, y *= d, z *= d;
+}
+
+void vector::operator/=(const double &d)
+{
+       if (d != 0.0)
+               x /= d, y /= d, z /= d;
+}
+
+void vector::operator+=(const vector &v)
+{
+       x += v.x, y += v.y, z += v.z;
+}
+
+void vector::operator-=(const vector &v)
+{
+       x -= v.x, y -= v.y, z -= v.z;
+}
+
+vector vector::operator*(const double &d)              // Scale vector by amount
+{
+       return vector(x * d, y * d, z * d);
+}
+
+void vector::zero(const double epsilon/*= 1.0e-6*/)
+{
+       if (fabs(x) < epsilon)
+               x = 0.0;
+
+       if (fabs(y) < epsilon)
+               y = 0.0;
+
+       if (fabs(z) < epsilon)
+               z = 0.0;
+}
+
+#else
+
+//
+// vector.cpp: Various structures used for 3 dimensional imaging
+//
+// by James Hammons
+// (C) 2006 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  ----------------------------------------------------------
+// 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
+//
+
+#include "vector.h"
+
+#include <math.h>                                                              // For sqrt()
+//#include "mathconstants.h"
+
+#define PI 3.14159265358979323846264338327
+#define RADIANS_TO_DEGREES (180.0 / PI)
+#define DEGREES_TO_RADIANS (PI / 180.0)
+
+
+// Vector implementation
+
+Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/,
+       double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/):
+       x(x1 - x2), y(y1 - y2), z(z1 - z2)
+{
+}
+
+
+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;
+
+       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)
+{
+       return Vector(-x, -y, -z);
+}
+
+
+// Vector x constant
+
+Vector Vector::operator*(double const v)
+{
+       return Vector(x * v, y * v, z * v);
+}
+
+
+// Vector x constant
+
+Vector Vector::operator*(float const v)
+{
+       return Vector(x * v, y * v, z * v);
+}
+
+
+// Vector / constant
+
+Vector Vector::operator/(double const v)
+{
+       return Vector(x / v, y / v, z / v);
+}
+
+
+// Vector / constant
+
+Vector Vector::operator/(float const v)
+{
+       return Vector(x / v, y / v, z / v);
+}
+
+
+// Vector (cross) product
+
+Vector Vector::operator*(Vector const v)
+{
+       // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
+       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)
+{
+       return (x * v.x) + (y * v.y) + (z * v.z);
+}
+
+
+// Vector x constant, self assigned
+
+Vector& Vector::operator*=(double const v)
+{
+       x *= v, y *= v, z *= v;
+
+       return *this;
+}
+
+
+// Vector / constant, self assigned
+
+Vector& Vector::operator/=(double const v)
+{
+       x /= v, y /= v, z /= v;
+
+       return *this;
+}
+
+
+// Vector + vector, self assigned
+
+Vector& Vector::operator+=(Vector const v)
+{
+       x += v.x, y += v.y, z += v.z;
+
+       return *this;
+}
+
+
+// Vector + constant, self assigned
+
+Vector& Vector::operator+=(double const v)
+{
+       x += v, y += v, z += v;
+
+       return *this;
+}
+
+
+// Vector - vector, self assigned
+
+Vector& Vector::operator-=(Vector const v)
+{
+       x -= v.x, y -= v.y, z -= v.z;
+
+       return *this;
+}
+
+
+// Vector - constant, self assigned
+
+Vector& Vector::operator-=(double const v)
+{
+       x -= v, y -= v, z -= 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();
+
+       // If the magnitude of the vector is zero, then the Unit vector is undefined...
+       if (mag == 0)
+               return Vector(0, 0, 0);
+
+       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
+       // 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.
+       double rawAngle = acos(Unit().x);
+       double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
+
+       return correctedAngle;
+}
+
+
+//
+// Returns the smallest angle between these two vectors
+//
+double Vector::Angle(Vector v)
+{
+// seems that something relies on this bad behavior... :-P
+#if 0
+       // Discard the sign from the subtraction
+       double angle = fabs(Angle() - v.Angle());
+
+       // Return the complementary angle if greater than 180⁰
+       return (angle <= 180.0 ? angle : 360.0 - angle);
+#else
+       return Angle() - v.Angle();
+#endif
+}
+
+
+bool Vector::isZero(double epsilon/*= 1e-6*/)
+{
+       return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false);
+}
+
+
+// Class methods
+
+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;
+       double yy = v1.y - v2.y;
+       double zz = v1.z - v2.z;
+       return sqrt((xx * xx) + (yy * yy) + (zz * zz));
+}
+
+#endif