]> Shamusworld >> Repos - ttedit/blob - src/vector.cpp
Move main repo to trunk.
[ttedit] / src / vector.cpp
1 //\r
2 // VECTOR.H - vector class definition\r
3 //\r
4 // by James L. Hammons\r
5 // (C) 2004 Underground Software\r
6 //\r
7 // JLH = James L. Hammons <jlhamm@acm.org>\r
8 //\r
9 // Who  When        What\r
10 // ---  ----------  -------------------------------------------------------------\r
11 // JLH  ??/??/2003  Created original implementation\r
12 // JLH  05/14/2004  Separated header from implementation, added operator-\r
13 //                  function\r
14 // JLH  05/15/2004  Added operator+ function\r
15 //\r
16 \r
17 #include <math.h>\r
18 #include "vector.h"\r
19 \r
20 vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,\r
21         double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):\r
22         x(a1 - a2), y(b1 - b2), z(c1 - c2)\r
23 {\r
24 }\r
25 \r
26 vector::vector(const vector &v1, const vector &v2):\r
27         x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)\r
28 {\r
29 }\r
30 \r
31 vector& vector::operator=(const vector &v)\r
32 {\r
33         x = v.x, y = v.y, z = v.z;\r
34         return *this;\r
35 }\r
36 \r
37 bool vector::operator==(const vector &v)\r
38 {\r
39         if ((x == v.x) && (y == v.y) && (z == v.z))\r
40                 return true;\r
41 \r
42         return false;\r
43 }\r
44 \r
45 void vector::unitize(void)\r
46 {\r
47         double dist = sqrt(x*x + y*y + z*z);\r
48 \r
49         if (dist != 0.0)\r
50                 x /= dist, y /= dist, z /= dist;\r
51 \r
52         if (x == -0.0)\r
53                 x = +0.0;\r
54 \r
55         if (y == -0.0)\r
56                 y = +0.0;\r
57 \r
58         if (z == -0.0)\r
59                 z = +0.0;\r
60 }\r
61 \r
62 vector vector::operator*(const vector &v)               // Cross product: "this" x "v"\r
63 {\r
64         vector r;\r
65 \r
66         r.x = (y * v.z) - (v.y * z);\r
67         r.y = -((x * v.z) - (v.x * z));\r
68         r.z = (x * v.y) - (v.x * y);\r
69 \r
70         return r;\r
71 }\r
72 \r
73 vector vector::operator+(const vector &v)\r
74 {\r
75         return vector(x + v.x, y + v.y, z + v.z);\r
76 }\r
77 \r
78 vector vector::operator-(const vector &v)\r
79 {\r
80         return vector(x, y, z, v.x, v.y, v.z);\r
81 }\r
82 \r
83 double vector::dot(const vector &v1, const vector &v2)\r
84 {\r
85         return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;\r
86 }\r
87 \r
88 double vector::dot(const vector &v)\r
89 {\r
90         return x * v.x + y * v.y + z * v.z;\r
91 }\r
92 \r
93 double vector::distance(const vector &v)                // Pythagoras extended to 3 dimensions\r
94 {\r
95         double a = x - v.x, b = y - v.y, c = z - v.z;\r
96 \r
97         return sqrt(a * a + b * b + c * c);\r
98 }\r
99 \r
100 double vector::length(void)\r
101 {\r
102         return sqrt(x * x + y * y + z * z);\r
103 }\r
104 \r
105 void vector::operator*=(const double &d)\r
106 {\r
107         x *= d, y *= d, z *= d;\r
108 }\r
109 \r
110 void vector::operator/=(const double &d)\r
111 {\r
112         if (d != 0.0)\r
113                 x /= d, y /= d, z /= d;\r
114 }\r
115 \r
116 void vector::operator+=(const vector &v)\r
117 {\r
118         x += v.x, y += v.y, z += v.z;\r
119 }\r
120 \r
121 void vector::operator-=(const vector &v)\r
122 {\r
123         x -= v.x, y -= v.y, z -= v.z;\r
124 }\r
125 \r
126 vector vector::operator*(const double &d)               // Scale vector by amount\r
127 {\r
128         return vector(x * d, y * d, z * d);\r
129 }\r
130 \r
131 void vector::zero(const double epsilon)\r
132 {\r
133         if (fabs(x) < epsilon)\r
134                 x = 0.0;\r
135 \r
136         if (fabs(y) < epsilon)\r
137                 y = 0.0;\r
138 \r
139         if (fabs(z) < epsilon)\r
140                 z = 0.0;\r
141 }\r