]> Shamusworld >> Repos - ttedit/blob - src/vector.cpp
Add ability to move points with the arrow keys.
[ttedit] / src / vector.cpp
1 #if 0
2
3 //
4 // VECTOR.H - vector class definition
5 //
6 // by James L. Hammons
7 // (C) 2004 Underground Software
8 //
9 // JLH = James L. Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  ??/??/2003  Created original implementation
14 // JLH  05/14/2004  Separated header from implementation, added operator-
15 //                  function
16 // JLH  05/15/2004  Added operator+ function
17 //
18
19 #include "vector.h"
20 #include <math.h>
21
22 vector::vector(double a1/*= 0.0*/, double b1/*= 0.0*/, double c1/*= 0.0*/,
23         double a2/*= 0.0*/, double b2/*= 0.0*/, double c2/*= 0.0*/):
24         x(a1 - a2), y(b1 - b2), z(c1 - c2)
25 {
26 }
27
28 vector::vector(const vector &v1, const vector &v2):
29         x(v1.x - v2.x), y(v1.y - v2.y), z(v1.z - v2.z)
30 {
31 }
32
33 vector& vector::operator=(const vector &v)
34 {
35         x = v.x, y = v.y, z = v.z;
36         return *this;
37 }
38
39 bool vector::operator==(const vector &v)
40 {
41         if ((x == v.x) && (y == v.y) && (z == v.z))
42                 return true;
43
44         return false;
45 }
46
47 void vector::unitize(void)
48 {
49         double dist = sqrt(x*x + y*y + z*z);
50
51         if (dist != 0.0)
52                 x /= dist, y /= dist, z /= dist;
53
54         if (x == -0.0)
55                 x = +0.0;
56
57         if (y == -0.0)
58                 y = +0.0;
59
60         if (z == -0.0)
61                 z = +0.0;
62 }
63
64 vector vector::operator*(const vector &v)               // Cross product: "this" x "v"
65 {
66         vector r;
67
68         r.x = (y * v.z) - (v.y * z);
69         r.y = -((x * v.z) - (v.x * z));
70         r.z = (x * v.y) - (v.x * y);
71
72         return r;
73 }
74
75 vector vector::operator+(const vector &v)
76 {
77         return vector(x + v.x, y + v.y, z + v.z);
78 }
79
80 vector vector::operator-(const vector &v)
81 {
82         return vector(x, y, z, v.x, v.y, v.z);
83 }
84
85 double vector::dot(const vector &v1, const vector &v2)
86 {
87         return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
88 }
89
90 double vector::dot(const vector &v)
91 {
92         return x * v.x + y * v.y + z * v.z;
93 }
94
95 double vector::distance(const vector &v)                // Pythagoras extended to 3 dimensions
96 {
97         double a = x - v.x, b = y - v.y, c = z - v.z;
98
99         return sqrt(a * a + b * b + c * c);
100 }
101
102 double vector::length(void)
103 {
104         return sqrt(x * x + y * y + z * z);
105 }
106
107 void vector::operator*=(const double &d)
108 {
109         x *= d, y *= d, z *= d;
110 }
111
112 void vector::operator/=(const double &d)
113 {
114         if (d != 0.0)
115                 x /= d, y /= d, z /= d;
116 }
117
118 void vector::operator+=(const vector &v)
119 {
120         x += v.x, y += v.y, z += v.z;
121 }
122
123 void vector::operator-=(const vector &v)
124 {
125         x -= v.x, y -= v.y, z -= v.z;
126 }
127
128 vector vector::operator*(const double &d)               // Scale vector by amount
129 {
130         return vector(x * d, y * d, z * d);
131 }
132
133 void vector::zero(const double epsilon/*= 1.0e-6*/)
134 {
135         if (fabs(x) < epsilon)
136                 x = 0.0;
137
138         if (fabs(y) < epsilon)
139                 y = 0.0;
140
141         if (fabs(z) < epsilon)
142                 z = 0.0;
143 }
144
145 #else
146
147 //
148 // vector.cpp: Various structures used for 3 dimensional imaging
149 //
150 // by James Hammons
151 // (C) 2006 Underground Software
152 //
153 // JLH = James L. Hammons <jlhamm@acm.org>
154 //
155 // WHO  WHEN        WHAT
156 // ---  ----------  ------------------------------------------------------------
157 // JLH  09/19/2006  Created this file
158 // JLH  03/22/2011  Moved implementation of constructor from header to here
159 // JLH  04/02/2011  Fixed divide-by-zero bug in Unit(), added Angle() function
160 //
161
162 #include "vector.h"
163
164 #include <math.h>                                                               // For sqrt()
165 //#include "mathconstants.h"
166
167 #define PI 3.14159265358979323846264338327
168 #define RADIANS_TO_DEGREES (180.0 / PI)
169 #define DEGREES_TO_RADIANS (PI / 180.0)
170
171
172 // Vector implementation
173
174 Vector::Vector(double x1/*= 0*/, double y1/*= 0*/, double z1/*= 0*/,
175         double x2/*= 0*/, double y2/*= 0*/, double z2/*= 0*/):
176         x(x1 - x2), y(y1 - y2), z(z1 - z2)
177 {
178 }
179
180
181 Vector::Vector(Vector head, Vector tail): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
182 {
183 }
184
185
186 Vector Vector::operator=(Vector const v)
187 {
188         x = v.x, y = v.y, z = v.z;
189
190         return *this;
191 }
192
193
194 Vector Vector::operator+(Vector const v)
195 {
196         return Vector(x + v.x, y + v.y, z + v.z);
197 }
198
199
200 Vector Vector::operator-(Vector const v)
201 {
202         return Vector(x - v.x, y - v.y, z - v.z);
203 }
204
205
206 // Unary negation
207
208 Vector Vector::operator-(void)
209 {
210         return Vector(-x, -y, -z);
211 }
212
213
214 // Vector x constant
215
216 Vector Vector::operator*(double const v)
217 {
218         return Vector(x * v, y * v, z * v);
219 }
220
221
222 // Vector x constant
223
224 Vector Vector::operator*(float const v)
225 {
226         return Vector(x * v, y * v, z * v);
227 }
228
229
230 // Vector / constant
231
232 Vector Vector::operator/(double const v)
233 {
234         return Vector(x / v, y / v, z / v);
235 }
236
237
238 // Vector / constant
239
240 Vector Vector::operator/(float const v)
241 {
242         return Vector(x / v, y / v, z / v);
243 }
244
245
246 // Vector (cross) product
247
248 Vector Vector::operator*(Vector const v)
249 {
250         // a x b = [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1]
251         return Vector((y * v.z) - (z * v.y), (z * v.x) - (x * v.z), (x * v.y) - (y * v.x));
252 }
253
254
255 // Dot product
256
257 double Vector::Dot(Vector const v)
258 {
259         return (x * v.x) + (y * v.y) + (z * v.z);
260 }
261
262
263 // Vector x constant, self assigned
264
265 Vector& Vector::operator*=(double const v)
266 {
267         x *= v, y *= v, z *= v;
268
269         return *this;
270 }
271
272
273 // Vector / constant, self assigned
274
275 Vector& Vector::operator/=(double const v)
276 {
277         x /= v, y /= v, z /= v;
278
279         return *this;
280 }
281
282
283 // Vector + vector, self assigned
284
285 Vector& Vector::operator+=(Vector const v)
286 {
287         x += v.x, y += v.y, z += v.z;
288
289         return *this;
290 }
291
292
293 // Vector + constant, self assigned
294
295 Vector& Vector::operator+=(double const v)
296 {
297         x += v, y += v, z += v;
298
299         return *this;
300 }
301
302
303 // Vector - vector, self assigned
304
305 Vector& Vector::operator-=(Vector const v)
306 {
307         x -= v.x, y -= v.y, z -= v.z;
308
309         return *this;
310 }
311
312
313 // Vector - constant, self assigned
314
315 Vector& Vector::operator-=(double const v)
316 {
317         x -= v, y -= v, z -= v;
318
319         return *this;
320 }
321
322
323 // Check for equality
324 bool Vector::operator==(Vector const v)
325 {
326         return ((x == v.x) && (y == v.y) && (z == v.z) ? true : false);
327 }
328
329
330 // Check for inequality
331 bool Vector::operator!=(Vector const v)
332 {
333         return ((x != v.x) || (y != v.y) || (z != v.z) ? true : false);
334 }
335
336
337 Vector Vector::Unit(void)
338 {
339         double mag = Magnitude();
340
341         // If the magnitude of the vector is zero, then the Unit vector is undefined...
342         if (mag == 0)
343                 return Vector(0, 0, 0);
344
345         return Vector(x / mag, y / mag, z / mag);
346 }
347
348
349 double Vector::Magnitude(void)
350 {
351         return sqrt((x * x) + (y * y) + (z * z));
352 }
353
354
355 double Vector::Angle(void)
356 {
357         // acos returns a value between zero and PI, which means we don't know which
358         // quadrant the angle is in... Though, if the y-coordinate of the vector is
359         // negative, that means that the angle is in quadrants III - IV.
360         double rawAngle = acos(Unit().x);
361         double correctedAngle = (y < 0 ? (2.0 * PI) - rawAngle : rawAngle);
362
363         return correctedAngle;
364 }
365
366
367 //
368 // Returns the smallest angle between these two vectors
369 //
370 double Vector::Angle(Vector v)
371 {
372 // seems that something relies on this bad behavior... :-P
373 #if 0
374         // Discard the sign from the subtraction
375         double angle = fabs(Angle() - v.Angle());
376
377         // Return the complementary angle if greater than 180⁰
378         return (angle <= 180.0 ? angle : 360.0 - angle);
379 #else
380         return Angle() - v.Angle();
381 #endif
382 }
383
384
385 bool Vector::isZero(double epsilon/*= 1e-6*/)
386 {
387         return ((fabs(x) < epsilon) && (fabs(y) < epsilon) && (fabs(z) < epsilon) ? true : false);
388 }
389
390
391 // Class methods
392
393 double Vector::Dot(Vector v1, Vector v2)
394 {
395         return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
396 }
397
398
399 double Vector::Magnitude(Vector v1, Vector v2)
400 {
401         double xx = v1.x - v2.x;
402         double yy = v1.y - v2.y;
403         double zz = v1.z - v2.z;
404         return sqrt((xx * xx) + (yy * yy) + (zz * zz));
405 }
406
407 #endif