Fix coding style
[survex.git] / src / vector3.cc
blob5e10e675adef7e520ae34cc5a473016ac93598a9
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include "vector3.h"
30 #include <math.h>
32 void Vector3::normalise()
34 double mag = magnitude();
35 if (mag != 0.0) {
36 x /= mag;
37 y /= mag;
38 z /= mag;
42 double dot(const Vector3& left, const Vector3& right)
44 return left.x*right.x + left.y*right.y + left.z*right.z;
47 Vector3& Vector3::operator*=(const double f)
49 x *= f;
50 y *= f;
51 z *= f;
53 return *this;
56 Vector3& Vector3::operator/=(const double f)
58 x /= f;
59 y /= f;
60 z /= f;
62 return *this;
65 Vector3& Vector3::operator+=(const Vector3 &v)
67 x += v.x;
68 y += v.y;
69 z += v.z;
71 return *this;
74 Vector3& Vector3::operator-=(const Vector3 &v)
76 x -= v.x;
77 y -= v.y;
78 z -= v.z;
80 return *this;
83 Vector3 operator*(const double f, const Vector3& v)
85 Vector3 o;
86 o.x = v.x * f;
87 o.y = v.y * f;
88 o.z = v.z * f;
90 return o;
93 Vector3 operator*(const Vector3& v1, const Vector3& v2)
95 // Cross product.
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;
102 return o;
105 Vector3 operator+(const Vector3& v1, const Vector3& v2)
107 Vector3 o;
108 o.x = v1.x + v2.x;
109 o.y = v1.y + v2.y;
110 o.z = v1.z + v2.z;
112 return o;
115 Vector3 operator-(const Vector3& v1, const Vector3& v2)
117 Vector3 o;
118 o.x = v1.x - v2.x;
119 o.y = v1.y - v2.y;
120 o.z = v1.z - v2.z;
122 return o;