Mana
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2007-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
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 * any later version.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cmath>
25
26#include <iostream>
27
32class Vector
33{
34 public:
35 Vector() = default;
36
37 Vector(float x, float y, float z = 0.0f):
38 x(x),
39 y(y),
40 z(z)
41 {}
42
47 bool isNull() const
48 {
49 return x == 0.0f && y == 0.0f && z == 0.0f;
50 }
51
55 Vector operator*(float c) const
56 {
57 return Vector(x * c,
58 y * c,
59 z * c);
60 }
61
66 {
67 x *= c;
68 y *= c;
69 z *= c;
70 return *this;
71 }
72
76 Vector operator/(float c) const
77 {
78 return Vector(x / c,
79 y / c,
80 z / c);
81 }
82
87 {
88 x /= c;
89 y /= c;
90 z /= c;
91 return *this;
92 }
93
97 Vector operator+(const Vector &v) const
98 {
99 return Vector(x + v.x,
100 y + v.y,
101 z + v.z);
102 }
103
108 {
109 x += v.x;
110 y += v.y;
111 z += v.z;
112 return *this;
113 }
114
118 Vector operator-(const Vector &v) const
119 {
120 return Vector(x - v.x,
121 y - v.y,
122 z - v.z);
123 }
124
129 {
130 x -= v.x;
131 y -= v.y;
132 z -= v.z;
133 return *this;
134 }
135
140 float length() const
141 {
142 return std::sqrt(x * x + y * y + z * z);
143 }
144
148 float squaredLength() const
149 {
150 return x * x + y * y + z * z;
151 }
152
156 float manhattanLength() const
157 {
158 return std::abs(x) + std::abs(y) + std::abs(z);
159 }
160
166 {
167 const float l = length();
168 return Vector(x / l, y / l, z / l);
169 }
170
171 float x = 0.0f;
172 float y = 0.0f;
173 float z = 0.0f;
174};
175
176inline bool operator == (const Vector &a, const Vector &b)
177{
178 return a.x == b.x && a.y == b.y && a.z == b.z;
179}
180
184std::ostream& operator <<(std::ostream &os, const Vector &v);
Vector class.
Definition vector.h:33
float squaredLength() const
Returns the squared length of this vector.
Definition vector.h:148
float z
Definition vector.h:173
Vector & operator*=(float c)
In-place scale vector operator.
Definition vector.h:65
float manhattanLength() const
Returns the manhattan length of this vector.
Definition vector.h:156
float length() const
Returns the length of this vector.
Definition vector.h:140
Vector operator-(const Vector &v) const
Subtract vector operator.
Definition vector.h:118
float y
Definition vector.h:172
Vector operator/(float c) const
Scale vector operator.
Definition vector.h:76
Vector()=default
Vector(float x, float y, float z=0.0f)
Definition vector.h:37
Vector & operator/=(float c)
In-place scale vector operator.
Definition vector.h:86
Vector operator*(float c) const
Scale vector operator.
Definition vector.h:55
Vector & operator-=(const Vector &v)
In-place subtract vector operator.
Definition vector.h:128
Vector normalized() const
Returns a normalized version of this vector.
Definition vector.h:165
Vector & operator+=(const Vector &v)
In-place add vector operator.
Definition vector.h:107
float x
Definition vector.h:171
Vector operator+(const Vector &v) const
Add vector operator.
Definition vector.h:97
bool isNull() const
Returns true if all coordinates are set to 0, otherwise returns false.
Definition vector.h:47
bool operator==(const Vector &a, const Vector &b)
Definition vector.h:176
std::ostream & operator<<(std::ostream &os, const Vector &v)
Appends a string representation of a vector to the output stream.
Definition vector.cpp:24