[5809313] | 1 | // |
---|
[27b8b59] | 2 | // vector3.cc |
---|
[5809313] | 3 | // |
---|
| 4 | // C++ class for 3-element vectors |
---|
| 5 | // |
---|
| 6 | // Copyright (C) 2000-2001, Mark R. Shinwell. |
---|
[e4ec4b4] | 7 | // Copyright (C) 2002-2003 Olly Betts |
---|
[5809313] | 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 |
---|
[ecbc6c18] | 21 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[5809313] | 22 | // |
---|
| 23 | |
---|
[cbfa50d] | 24 | #include <config.h> |
---|
| 25 | |
---|
[5809313] | 26 | #include "vector3.h" |
---|
| 27 | |
---|
| 28 | #include <math.h> |
---|
| 29 | |
---|
| 30 | void Vector3::normalise() |
---|
| 31 | { |
---|
| 32 | double mag = magnitude(); |
---|
[f8e4c56] | 33 | if (mag != 0.0) { |
---|
[b49ac56] | 34 | x /= mag; |
---|
| 35 | y /= mag; |
---|
| 36 | z /= mag; |
---|
[f8e4c56] | 37 | } |
---|
[5809313] | 38 | } |
---|
| 39 | |
---|
| 40 | double dot(const Vector3& left, const Vector3& right) |
---|
| 41 | { |
---|
| 42 | return left.x*right.x + left.y*right.y + left.z*right.z; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | Vector3& Vector3::operator*=(const double f) |
---|
| 46 | { |
---|
| 47 | x *= f; |
---|
| 48 | y *= f; |
---|
| 49 | z *= f; |
---|
| 50 | |
---|
| 51 | return *this; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | Vector3& Vector3::operator/=(const double f) |
---|
| 55 | { |
---|
| 56 | x /= f; |
---|
| 57 | y /= f; |
---|
| 58 | z /= f; |
---|
| 59 | |
---|
| 60 | return *this; |
---|
| 61 | } |
---|
| 62 | |
---|
[e4ec4b4] | 63 | Vector3& Vector3::operator+=(const Vector3 &v) |
---|
| 64 | { |
---|
| 65 | x += v.x; |
---|
| 66 | y += v.y; |
---|
| 67 | z += v.z; |
---|
| 68 | |
---|
| 69 | return *this; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | Vector3& Vector3::operator-=(const Vector3 &v) |
---|
| 73 | { |
---|
| 74 | x -= v.x; |
---|
| 75 | y -= v.y; |
---|
| 76 | z -= v.z; |
---|
| 77 | |
---|
| 78 | return *this; |
---|
| 79 | } |
---|
| 80 | |
---|
[5809313] | 81 | Vector3 operator*(const double f, const Vector3& v) |
---|
| 82 | { |
---|
| 83 | Vector3 o; |
---|
| 84 | o.x = v.x * f; |
---|
| 85 | o.y = v.y * f; |
---|
| 86 | o.z = v.z * f; |
---|
| 87 | |
---|
| 88 | return o; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Vector3 operator*(const Vector3& v1, const Vector3& v2) |
---|
| 92 | { |
---|
| 93 | // Cross product. |
---|
| 94 | |
---|
| 95 | Vector3 o; |
---|
| 96 | o.x = v1.y*v2.z - v1.z*v2.y; |
---|
| 97 | o.y = v1.z*v2.x - v1.x*v2.z; |
---|
| 98 | o.z = v1.x*v2.y - v1.y*v2.x; |
---|
| 99 | |
---|
| 100 | return o; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | Vector3 operator+(const Vector3& v1, const Vector3& v2) |
---|
| 104 | { |
---|
| 105 | Vector3 o; |
---|
| 106 | o.x = v1.x + v2.x; |
---|
| 107 | o.y = v1.y + v2.y; |
---|
| 108 | o.z = v1.z + v2.z; |
---|
| 109 | |
---|
| 110 | return o; |
---|
| 111 | } |
---|
[f8e4c56] | 112 | |
---|
| 113 | Vector3 operator-(const Vector3& v1, const Vector3& v2) |
---|
| 114 | { |
---|
| 115 | Vector3 o; |
---|
| 116 | o.x = v1.x - v2.x; |
---|
| 117 | o.y = v1.y - v2.y; |
---|
| 118 | o.z = v1.z - v2.z; |
---|
| 119 | |
---|
| 120 | return o; |
---|
| 121 | } |
---|