[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 |
---|
| 21 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 22 | // |
---|
| 23 | |
---|
[cbfa50d] | 24 | #ifdef HAVE_CONFIG_H |
---|
| 25 | #include <config.h> |
---|
| 26 | #endif |
---|
| 27 | |
---|
[5809313] | 28 | #include "vector3.h" |
---|
| 29 | |
---|
| 30 | #include <math.h> |
---|
| 31 | |
---|
| 32 | void Vector3::normalise() |
---|
| 33 | { |
---|
| 34 | double mag = magnitude(); |
---|
[f8e4c56] | 35 | if (mag != 0.0) { |
---|
| 36 | x /= mag; |
---|
| 37 | y /= mag; |
---|
| 38 | z /= mag; |
---|
| 39 | } |
---|
[5809313] | 40 | } |
---|
| 41 | |
---|
| 42 | double dot(const Vector3& left, const Vector3& right) |
---|
| 43 | { |
---|
| 44 | return left.x*right.x + left.y*right.y + left.z*right.z; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | Vector3& Vector3::operator*=(const double f) |
---|
| 48 | { |
---|
| 49 | x *= f; |
---|
| 50 | y *= f; |
---|
| 51 | z *= f; |
---|
| 52 | |
---|
| 53 | return *this; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | Vector3& Vector3::operator/=(const double f) |
---|
| 57 | { |
---|
| 58 | x /= f; |
---|
| 59 | y /= f; |
---|
| 60 | z /= f; |
---|
| 61 | |
---|
| 62 | return *this; |
---|
| 63 | } |
---|
| 64 | |
---|
[e4ec4b4] | 65 | Vector3& Vector3::operator+=(const Vector3 &v) |
---|
| 66 | { |
---|
| 67 | x += v.x; |
---|
| 68 | y += v.y; |
---|
| 69 | z += v.z; |
---|
| 70 | |
---|
| 71 | return *this; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | Vector3& Vector3::operator-=(const Vector3 &v) |
---|
| 75 | { |
---|
| 76 | x -= v.x; |
---|
| 77 | y -= v.y; |
---|
| 78 | z -= v.z; |
---|
| 79 | |
---|
| 80 | return *this; |
---|
| 81 | } |
---|
| 82 | |
---|
[5809313] | 83 | Vector3 operator*(const double f, const Vector3& v) |
---|
| 84 | { |
---|
| 85 | Vector3 o; |
---|
| 86 | o.x = v.x * f; |
---|
| 87 | o.y = v.y * f; |
---|
| 88 | o.z = v.z * f; |
---|
| 89 | |
---|
| 90 | return o; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | Vector3 operator*(const Vector3& v1, const Vector3& v2) |
---|
| 94 | { |
---|
| 95 | // Cross product. |
---|
| 96 | |
---|
| 97 | Vector3 o; |
---|
| 98 | o.x = v1.y*v2.z - v1.z*v2.y; |
---|
| 99 | o.y = v1.z*v2.x - v1.x*v2.z; |
---|
| 100 | o.z = v1.x*v2.y - v1.y*v2.x; |
---|
| 101 | |
---|
| 102 | return o; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Vector3 operator+(const Vector3& v1, const Vector3& v2) |
---|
| 106 | { |
---|
| 107 | Vector3 o; |
---|
| 108 | o.x = v1.x + v2.x; |
---|
| 109 | o.y = v1.y + v2.y; |
---|
| 110 | o.z = v1.z + v2.z; |
---|
| 111 | |
---|
| 112 | return o; |
---|
| 113 | } |
---|
[f8e4c56] | 114 | |
---|
| 115 | Vector3 operator-(const Vector3& v1, const Vector3& v2) |
---|
| 116 | { |
---|
| 117 | Vector3 o; |
---|
| 118 | o.x = v1.x - v2.x; |
---|
| 119 | o.y = v1.y - v2.y; |
---|
| 120 | o.z = v1.z - v2.z; |
---|
| 121 | |
---|
| 122 | return o; |
---|
| 123 | } |
---|