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  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef RESOURCES_VECTOR_H
25 #define RESOURCES_VECTOR_H
26 
27 #ifndef USE_SDL2
28 #include <cmath>
29 #endif // USE_SDL2
30 
31 #include <iostream>
32 
33 #include "localconsts.h"
34 
40 {
41  public:
45  Vector() :
46  x(0.0F),
47  y(0.0F),
48  z(0.0F)
49  {}
50 
54  Vector(const float x0,
55  const float y0,
56  const float z0) :
57  x(x0),
58  y(y0),
59  z(z0)
60  {}
61 
65  Vector(const Vector &v) :
66  x(v.x),
67  y(v.y),
68  z(v.z)
69  {}
70 
72 
73 
78  {
79  return x == 0.0F && y == 0.0F && z == 0.0F;
80  }
81 
83  {
84  x = v.x;
85  y = v.y;
86  z = v.z;
87  return *this;
88  }
89 
93  Vector operator*(const float c) const noexcept2 A_WARN_UNUSED
94  {
95  return Vector(x * c,
96  y * c,
97  z * c);
98  }
99 
103  Vector &operator*=(const float c) noexcept2
104  {
105  x *= c;
106  y *= c;
107  z *= c;
108  return *this;
109  }
110 
115  {
116  return Vector(x / c,
117  y / c,
118  z / c);
119  }
120 
125  {
126  x /= c;
127  y /= c;
128  z /= c;
129  return *this;
130  }
131 
136  {
137  return Vector(x + v.x,
138  y + v.y,
139  z + v.z);
140  }
141 
146  {
147  x += v.x;
148  y += v.y;
149  z += v.z;
150  return *this;
151  }
152 
157  {
158  return Vector(x - v.x,
159  y - v.y,
160  z - v.z);
161  }
162 
167  {
168  x -= v.x;
169  y -= v.y;
170  z -= v.z;
171  return *this;
172  }
173 
178  float length() const A_WARN_UNUSED
179  {
180  return sqrtf(x * x + y * y + z * z);
181  }
182 
187  {
188  return x * x + y * y + z * z;
189  }
190 
195  {
196  return fabsf(x) + fabsf(y) + fabsf(z);
197  }
198 
204  {
205  const float len = length();
206  return Vector(x / len, y / len, z / len);
207  }
208 
209  float x, y, z;
210 };
211 
215 std::ostream& operator <<(std::ostream &os, const Vector &v);
216 
217 #endif // RESOURCES_VECTOR_H
Definition: vector.h:40
float squaredLength() const
Definition: vector.h:186
float z
Definition: vector.h:209
Vector & operator+=(const Vector &v)
Definition: vector.h:145
Vector operator/(const float c) const
Definition: vector.h:114
Vector(const float x0, const float y0, const float z0)
Definition: vector.h:54
float manhattanLength() const
Definition: vector.h:194
float length() const
Definition: vector.h:178
Vector operator-(const Vector &v) const
Definition: vector.h:156
Vector operator*(const float c) const
Definition: vector.h:93
Vector & operator/=(const float c)
Definition: vector.h:124
Vector(const Vector &v)
Definition: vector.h:65
Vector()
Definition: vector.h:45
float y
Definition: vector.h:209
Vector & operator=(const Vector &v)
Definition: vector.h:82
Vector & operator*=(const float c)
Definition: vector.h:103
Vector normalized() const
Definition: vector.h:203
float x
Definition: vector.h:209
Vector & operator-=(const Vector &v)
Definition: vector.h:166
Vector operator+(const Vector &v) const
Definition: vector.h:135
bool isNull() const
Definition: vector.h:77
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41
std::ostream & operator<<(std::ostream &os, const Vector &v)
Definition: vector.cpp:28