3 // C++ class for 3-element vectors
5 // Copyright (C) 2000-2002, Mark R. Shinwell.
6 // Copyright (C) 2002-2004,2005,2006,2015 Olly Betts
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 Vector3() : x(0.0), y(0.0), z(0.0) { }
34 Vector3(double a
, double b
, double c
) : x(a
), y(b
), z(c
) { }
36 double GetX() const { return x
; }
37 double GetY() const { return y
; }
38 double GetZ() const { return z
; }
40 double magnitude() const {
41 return sqrt(x
*x
+ y
*y
+ z
*z
);
44 // Returns a value in *radians*.
45 double gradient() const {
46 return atan2(z
, sqrt(x
*x
+ y
*y
));
51 void assign(double a
, double b
, double c
) {
55 void assign(const Vector3
&v
) {
59 friend Vector3
operator-(const Vector3
& o
) {
60 return Vector3(-o
.x
, -o
.y
, -o
.z
);
62 Vector3
& operator*=(double);
63 Vector3
& operator/=(double);
64 Vector3
& operator+=(const Vector3
&);
65 Vector3
& operator-=(const Vector3
&);
67 friend Vector3
operator*(double, const Vector3
&);
68 friend Vector3
operator*(const Vector3
& v
, double f
) {
71 friend Vector3
operator*(const Vector3
&, const Vector3
&); // cross product
72 friend Vector3
operator+(const Vector3
&, const Vector3
&);
73 friend Vector3
operator-(const Vector3
&, const Vector3
&);
74 friend bool operator==(const Vector3
&, const Vector3
&);
75 friend bool operator<(const Vector3
&, const Vector3
&);
76 friend double dot(const Vector3
&, const Vector3
&);
79 inline bool operator==(const Vector3
& a
, const Vector3
& b
) {
80 return a
.x
== b
.x
&& a
.y
== b
.y
&& a
.z
== b
.z
;
83 // So we can use Vector3 as a key in a map...
84 inline bool operator<(const Vector3
& a
, const Vector3
& b
) {
85 if (a
.x
!= b
.x
) return a
.x
< b
.x
;
86 if (a
.y
!= b
.y
) return a
.y
< b
.y
;