1 | // |
---|
2 | // vector3.cc |
---|
3 | // |
---|
4 | // C++ class for 3-element vectors |
---|
5 | // |
---|
6 | // Copyright (C) 2000-2001, Mark R. Shinwell. |
---|
7 | // Copyright (C) 2002-2003 Olly Betts |
---|
8 | // |
---|
9 | // This program is free software; you can redistribute it and/or modify |
---|
10 | // it under the terms of the GNU General Public License as published by |
---|
11 | // the Free Software Foundation; either version 2 of the License, or |
---|
12 | // (at your option) any later version. |
---|
13 | // |
---|
14 | // This program is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | // GNU General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU General Public License |
---|
20 | // along with this program; if not, write to the Free Software |
---|
21 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
22 | // |
---|
23 | |
---|
24 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "vector3.h" |
---|
29 | |
---|
30 | #include <math.h> |
---|
31 | |
---|
32 | Vector3::Vector3() : x(0.0), y(0.0), z(0.0) |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | Vector3::Vector3(double a, double b, double c) |
---|
37 | { |
---|
38 | x = a; |
---|
39 | y = b; |
---|
40 | z = c; |
---|
41 | } |
---|
42 | |
---|
43 | Vector3::~Vector3() |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | void Vector3::normalise() |
---|
48 | { |
---|
49 | double mag = magnitude(); |
---|
50 | if (mag != 0.0) { |
---|
51 | x /= mag; |
---|
52 | y /= mag; |
---|
53 | z /= mag; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | double dot(const Vector3& left, const Vector3& right) |
---|
58 | { |
---|
59 | return left.x*right.x + left.y*right.y + left.z*right.z; |
---|
60 | } |
---|
61 | |
---|
62 | Vector3& Vector3::operator=(const Vector3& r) |
---|
63 | { |
---|
64 | x = r.x; |
---|
65 | y = r.y; |
---|
66 | z = r.z; |
---|
67 | |
---|
68 | return *this; |
---|
69 | } |
---|
70 | |
---|
71 | void Vector3::set(double nx, double ny, double nz) |
---|
72 | { |
---|
73 | x = nx; |
---|
74 | y = ny; |
---|
75 | z = nz; |
---|
76 | } |
---|
77 | |
---|
78 | Vector3& Vector3::operator*=(const double f) |
---|
79 | { |
---|
80 | x *= f; |
---|
81 | y *= f; |
---|
82 | z *= f; |
---|
83 | |
---|
84 | return *this; |
---|
85 | } |
---|
86 | |
---|
87 | Vector3& Vector3::operator/=(const double f) |
---|
88 | { |
---|
89 | x /= f; |
---|
90 | y /= f; |
---|
91 | z /= f; |
---|
92 | |
---|
93 | return *this; |
---|
94 | } |
---|
95 | |
---|
96 | Vector3& Vector3::operator+=(const Vector3 &v) |
---|
97 | { |
---|
98 | x += v.x; |
---|
99 | y += v.y; |
---|
100 | z += v.z; |
---|
101 | |
---|
102 | return *this; |
---|
103 | } |
---|
104 | |
---|
105 | Vector3& Vector3::operator-=(const Vector3 &v) |
---|
106 | { |
---|
107 | x -= v.x; |
---|
108 | y -= v.y; |
---|
109 | z -= v.z; |
---|
110 | |
---|
111 | return *this; |
---|
112 | } |
---|
113 | |
---|
114 | Vector3 operator*(const double f, const Vector3& v) |
---|
115 | { |
---|
116 | Vector3 o; |
---|
117 | o.x = v.x * f; |
---|
118 | o.y = v.y * f; |
---|
119 | o.z = v.z * f; |
---|
120 | |
---|
121 | return o; |
---|
122 | } |
---|
123 | |
---|
124 | Vector3 operator*(const Vector3& v1, const Vector3& v2) |
---|
125 | { |
---|
126 | // Cross product. |
---|
127 | |
---|
128 | Vector3 o; |
---|
129 | o.x = v1.y*v2.z - v1.z*v2.y; |
---|
130 | o.y = v1.z*v2.x - v1.x*v2.z; |
---|
131 | o.z = v1.x*v2.y - v1.y*v2.x; |
---|
132 | |
---|
133 | return o; |
---|
134 | } |
---|
135 | |
---|
136 | Vector3 operator+(const Vector3& v1, const Vector3& v2) |
---|
137 | { |
---|
138 | Vector3 o; |
---|
139 | o.x = v1.x + v2.x; |
---|
140 | o.y = v1.y + v2.y; |
---|
141 | o.z = v1.z + v2.z; |
---|
142 | |
---|
143 | return o; |
---|
144 | } |
---|
145 | |
---|
146 | Vector3 operator-(const Vector3& v1, const Vector3& v2) |
---|
147 | { |
---|
148 | Vector3 o; |
---|
149 | o.x = v1.x - v2.x; |
---|
150 | o.y = v1.y - v2.y; |
---|
151 | o.z = v1.z - v2.z; |
---|
152 | |
---|
153 | return o; |
---|
154 | } |
---|