ManaPlus
vector.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2007-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
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  * 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, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef RESOURCES_VECTOR_H
24 #define RESOURCES_VECTOR_H
25 
26 #ifndef USE_SDL2
27 #include <cmath>
28 #endif // USE_SDL2
29 
30 #include <iostream>
31 
32 #include "localconsts.h"
33 
39 {
40  public:
44  Vector() :
45  x(0.0F),
46  y(0.0F),
47  z(0.0F)
48  {}
49 
53  Vector(const float x0,
54  const float y0,
55  const float z0) :
56  x(x0),
57  y(y0),
58  z(z0)
59  {}
60 
64  Vector(const Vector &v) :
65  x(v.x),
66  y(v.y),
67  z(v.z)
68  {}
69 
71 
72 
77  {
78  return x == 0.0F && y == 0.0F && z == 0.0F;
79  }
80 
82  {
83  x = v.x;
84  y = v.y;
85  z = v.z;
86  return *this;
87  }
88 
92  Vector operator*(const float c) const noexcept2 A_WARN_UNUSED
93  {
94  return Vector(x * c,
95  y * c,
96  z * c);
97  }
98 
102  Vector &operator*=(const float c) noexcept2
103  {
104  x *= c;
105  y *= c;
106  z *= c;
107  return *this;
108  }
109 
114  {
115  return Vector(x / c,
116  y / c,
117  z / c);
118  }
119 
124  {
125  x /= c;
126  y /= c;
127  z /= c;
128  return *this;
129  }
130 
135  {
136  return Vector(x + v.x,
137  y + v.y,
138  z + v.z);
139  }
140 
145  {
146  x += v.x;
147  y += v.y;
148  z += v.z;
149  return *this;
150  }
151 
156  {
157  return Vector(x - v.x,
158  y - v.y,
159  z - v.z);
160  }
161 
166  {
167  x -= v.x;
168  y -= v.y;
169  z -= v.z;
170  return *this;
171  }
172 
177  float length() const A_WARN_UNUSED
178  {
179  return sqrtf(x * x + y * y + z * z);
180  }
181 
186  {
187  return x * x + y * y + z * z;
188  }
189 
194  {
195  return fabsf(x) + fabsf(y) + fabsf(z);
196  }
197 
203  {
204  const float len = length();
205  return Vector(x / len, y / len, z / len);
206  }
207 
208  float x, y, z;
209 };
210 
214 std::ostream& operator <<(std::ostream &os, const Vector &v);
215 
216 #endif // RESOURCES_VECTOR_H
Vector::operator=
Vector & operator=(const Vector &v)
Definition: vector.h:81
Vector::Vector
Vector()
Definition: vector.h:44
Vector::manhattanLength
float manhattanLength() const
Definition: vector.h:193
Vector::x
float x
Definition: vector.h:208
Vector::operator+=
Vector & operator+=(const Vector &v)
Definition: vector.h:144
Vector::normalized
Vector normalized() const
Definition: vector.h:202
operator<<
std::ostream & operator<<(std::ostream &os, const Vector &v)
Definition: vector.cpp:27
localconsts.h
Vector::operator/
Vector operator/(const float c) const
Definition: vector.h:113
A_DEFAULT_COPY
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:40
bool
Vector::z
float z
Definition: vector.h:208
noexcept2
#define noexcept2
Definition: localconsts.h:49
Vector::operator*=
Vector & operator*=(const float c)
Definition: vector.h:102
Vector::operator-=
Vector & operator-=(const Vector &v)
Definition: vector.h:165
final
#define final
Definition: localconsts.h:45
Vector::operator/=
Vector & operator/=(const float c)
Definition: vector.h:123
Vector::isNull
bool isNull() const
Definition: vector.h:76
Vector::Vector
Vector(const float x0, const float y0, const float z0)
Definition: vector.h:53
Vector::operator*
Vector operator*(const float c) const
Definition: vector.h:92
Vector::operator-
Vector operator-(const Vector &v) const
Definition: vector.h:155
Vector::length
float length() const
Definition: vector.h:177
Vector::squaredLength
float squaredLength() const
Definition: vector.h:185
Vector::operator+
Vector operator+(const Vector &v) const
Definition: vector.h:134
Vector::y
float y
Definition: vector.h:208
A_WARN_UNUSED
#define A_WARN_UNUSED
Definition: localconsts.h:160
Vector
Definition: vector.h:38
Vector::Vector
Vector(const Vector &v)
Definition: vector.h:64