1 | // |
---|
2 | // vector3.cxx |
---|
3 | // |
---|
4 | // C++ class for 3-element vectors |
---|
5 | // |
---|
6 | // Copyright (C) 2000-2001, Mark R. Shinwell. |
---|
7 | // Copyright (C) 2002 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 | #include "vector3.h" |
---|
25 | |
---|
26 | #include <math.h> |
---|
27 | |
---|
28 | Vector3::Vector3() : x(0.0), y(0.0), z(0.0) |
---|
29 | { |
---|
30 | } |
---|
31 | |
---|
32 | Vector3::Vector3(double a, double b, double c) |
---|
33 | { |
---|
34 | x = a; |
---|
35 | y = b; |
---|
36 | z = c; |
---|
37 | } |
---|
38 | |
---|
39 | Vector3::~Vector3() |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | double Vector3::magnitude() const |
---|
44 | { |
---|
45 | return sqrt(x*x + y*y + z*z); |
---|
46 | } |
---|
47 | |
---|
48 | void Vector3::normalise() |
---|
49 | { |
---|
50 | double mag = magnitude(); |
---|
51 | if (mag != 0.0) { |
---|
52 | x /= mag; |
---|
53 | y /= mag; |
---|
54 | z /= mag; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | double dot(const Vector3& left, const Vector3& right) |
---|
59 | { |
---|
60 | return left.x*right.x + left.y*right.y + left.z*right.z; |
---|
61 | } |
---|
62 | |
---|
63 | Vector3& Vector3::operator=(const Vector3& r) |
---|
64 | { |
---|
65 | x = r.x; |
---|
66 | y = r.y; |
---|
67 | z = r.z; |
---|
68 | |
---|
69 | return *this; |
---|
70 | } |
---|
71 | |
---|
72 | void Vector3::set(double nx, double ny, double nz) |
---|
73 | { |
---|
74 | x = nx; |
---|
75 | y = ny; |
---|
76 | z = nz; |
---|
77 | } |
---|
78 | |
---|
79 | Vector3& Vector3::operator*=(const double f) |
---|
80 | { |
---|
81 | x *= f; |
---|
82 | y *= f; |
---|
83 | z *= f; |
---|
84 | |
---|
85 | return *this; |
---|
86 | } |
---|
87 | |
---|
88 | Vector3& Vector3::operator/=(const double f) |
---|
89 | { |
---|
90 | x /= f; |
---|
91 | y /= f; |
---|
92 | z /= f; |
---|
93 | |
---|
94 | return *this; |
---|
95 | } |
---|
96 | |
---|
97 | Vector3 operator*(const double f, const Vector3& v) |
---|
98 | { |
---|
99 | Vector3 o; |
---|
100 | o.x = v.x * f; |
---|
101 | o.y = v.y * f; |
---|
102 | o.z = v.z * f; |
---|
103 | |
---|
104 | return o; |
---|
105 | } |
---|
106 | |
---|
107 | Vector3 operator*(const Vector3& v1, const Vector3& v2) |
---|
108 | { |
---|
109 | // Cross product. |
---|
110 | |
---|
111 | Vector3 o; |
---|
112 | o.x = v1.y*v2.z - v1.z*v2.y; |
---|
113 | o.y = v1.z*v2.x - v1.x*v2.z; |
---|
114 | o.z = v1.x*v2.y - v1.y*v2.x; |
---|
115 | |
---|
116 | return o; |
---|
117 | } |
---|
118 | |
---|
119 | Vector3 operator+(const Vector3& v1, const Vector3& v2) |
---|
120 | { |
---|
121 | Vector3 o; |
---|
122 | o.x = v1.x + v2.x; |
---|
123 | o.y = v1.y + v2.y; |
---|
124 | o.z = v1.z + v2.z; |
---|
125 | |
---|
126 | return o; |
---|
127 | } |
---|
128 | |
---|
129 | Vector3 operator-(const Vector3& v1, const Vector3& v2) |
---|
130 | { |
---|
131 | Vector3 o; |
---|
132 | o.x = v1.x - v2.x; |
---|
133 | o.y = v1.y - v2.y; |
---|
134 | o.z = v1.z - v2.z; |
---|
135 | |
---|
136 | return o; |
---|
137 | } |
---|