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