TrinityCore
Loading...
Searching...
No Matches
vec3d.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef VEC3D_H
19#define VEC3D_H
20
21#include <iostream>
22#include <cmath>
23
24class Vec3D
25{
26public:
27 float x, y, z;
28
29 Vec3D(float x0 = 0.0f, float y0 = 0.0f, float z0 = 0.0f) : x(x0), y(y0), z(z0) { }
30
31 Vec3D(Vec3D const& v) = default;
32
33 Vec3D& operator=(Vec3D const& v) = default;
34
35 Vec3D operator+(Vec3D const& v) const
36 {
37 Vec3D r(x + v.x, y + v.y, z + v.z);
38 return r;
39 }
40
41 Vec3D operator-(Vec3D const& v) const
42 {
43 Vec3D r(x - v.x, y - v.y, z - v.z);
44 return r;
45 }
46
47 float operator*(Vec3D const& v) const
48 {
49 return x * v.x + y * v.y + z * v.z;
50 }
51
52 Vec3D operator*(float d) const
53 {
54 Vec3D r(x * d, y * d, z * d);
55 return r;
56 }
57
58 friend Vec3D operator*(float d, Vec3D const& v)
59 {
60 return v * d;
61 }
62
63 Vec3D operator%(Vec3D const& v) const
64 {
65 Vec3D r(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
66 return r;
67 }
68
70 {
71 x += v.x;
72 y += v.y;
73 z += v.z;
74 return *this;
75 }
76
78 {
79 x -= v.x;
80 y -= v.y;
81 z -= v.z;
82 return *this;
83 }
84
85 Vec3D& operator*=(float d)
86 {
87 x *= d;
88 y *= d;
89 z *= d;
90 return *this;
91 }
92
93 float lengthSquared() const
94 {
95 return x * x + y * y + z * z;
96 }
97
98 float length() const
99 {
100 return std::sqrt(lengthSquared());
101 }
102
104 {
105 *this *= (1.0f / length());
106 return *this;
107 }
108
110 {
111 Vec3D r(*this);
112 r.normalize();
113 return r;
114 }
115
116 friend std::istream& operator>>(std::istream& in, Vec3D& v)
117 {
118 in >> v.x >> v.y >> v.z;
119 return in;
120 }
121
122 friend std::ostream& operator<<(std::ostream& out, Vec3D const& v)
123 {
124 out << v.x << " " << v.y << " " << v.z;
125 return out;
126 }
127
128 operator float*()
129 {
130 return (float*)this;
131 }
132};
133
135{
136public:
139};
140
141class Vec2D
142{
143public:
144 float x, y;
145
146 Vec2D(float x0 = 0.0f, float y0 = 0.0f) : x(x0), y(y0) { }
147
148 Vec2D(Vec2D const& v) = default;
149
150 Vec2D& operator=(Vec2D const& v) = default;
151
152 Vec2D operator+(Vec2D const& v) const
153 {
154 Vec2D r(x + v.x, y + v.y);
155 return r;
156 }
157
158 Vec2D operator-(Vec2D const& v) const
159 {
160 Vec2D r(x - v.x, y - v.y);
161 return r;
162 }
163
164 float operator*(Vec2D const& v) const
165 {
166 return x * v.x + y * v.y;
167 }
168
169 Vec2D operator*(float d) const
170 {
171 Vec2D r(x * d, y * d);
172 return r;
173 }
174
175 friend Vec2D operator*(float d, Vec2D const& v)
176 {
177 return v * d;
178 }
179
181 {
182 x += v.x;
183 y += v.y;
184 return *this;
185 }
186
188 {
189 x -= v.x;
190 y -= v.y;
191 return *this;
192 }
193
195 {
196 x *= d;
197 y *= d;
198 return *this;
199 }
200
201 float lengthSquared() const
202 {
203 return x * x + y * y;
204 }
205
206 float length() const
207 {
208 return std::sqrt(lengthSquared());
209 }
210
212 {
213 *this *= (1.0f / length());
214 return *this;
215 }
216
218 {
219 Vec2D r(*this);
220 r.normalize();
221 return r;
222 }
223
224 friend std::istream& operator>>(std::istream& in, Vec2D& v)
225 {
226 in >> v.x >> v.y;
227 return in;
228 }
229
230 operator float*()
231 {
232 return (float*)this;
233 }
234};
235
236inline void rotate(float x0, float y0, float* x, float* y, float angle)
237{
238 float xa = *x - x0;
239 float ya = *y - y0;
240 *x = xa*cosf(angle) - ya*sinf(angle) + x0;
241 *y = xa*sinf(angle) + ya*cosf(angle) + y0;
242}
243
245{
246 float X, Y, Z, W;
247};
248
249#endif
Vec3D max
Definition vec3d.h:138
Vec3D min
Definition vec3d.h:137
Definition vec3d.h:142
Vec2D(float x0=0.0f, float y0=0.0f)
Definition vec3d.h:146
Vec2D & operator=(Vec2D const &v)=default
float length() const
Definition vec3d.h:206
friend std::istream & operator>>(std::istream &in, Vec2D &v)
Definition vec3d.h:224
float operator*(Vec2D const &v) const
Definition vec3d.h:164
Vec2D(Vec2D const &v)=default
Vec2D operator~() const
Definition vec3d.h:217
Vec2D & normalize()
Definition vec3d.h:211
float x
Definition vec3d.h:144
Vec2D & operator*=(float d)
Definition vec3d.h:194
Vec2D operator+(Vec2D const &v) const
Definition vec3d.h:152
Vec2D & operator-=(Vec2D const &v)
Definition vec3d.h:187
float lengthSquared() const
Definition vec3d.h:201
friend Vec2D operator*(float d, Vec2D const &v)
Definition vec3d.h:175
Vec2D operator-(Vec2D const &v) const
Definition vec3d.h:158
float y
Definition vec3d.h:144
Vec2D & operator+=(Vec2D const &v)
Definition vec3d.h:180
Vec2D operator*(float d) const
Definition vec3d.h:169
Definition vec3d.h:25
Vec3D(Vec3D const &v)=default
float x
Definition vec3d.h:27
Vec3D operator+(Vec3D const &v) const
Definition vec3d.h:35
float operator*(Vec3D const &v) const
Definition vec3d.h:47
Vec3D operator-(Vec3D const &v) const
Definition vec3d.h:41
float y
Definition vec3d.h:27
Vec3D operator*(float d) const
Definition vec3d.h:52
float z
Definition vec3d.h:27
Vec3D(float x0=0.0f, float y0=0.0f, float z0=0.0f)
Definition vec3d.h:29
float lengthSquared() const
Definition vec3d.h:93
Vec3D & operator=(Vec3D const &v)=default
friend std::istream & operator>>(std::istream &in, Vec3D &v)
Definition vec3d.h:116
Vec3D & operator-=(Vec3D const &v)
Definition vec3d.h:77
Vec3D & operator*=(float d)
Definition vec3d.h:85
float length() const
Definition vec3d.h:98
friend Vec3D operator*(float d, Vec3D const &v)
Definition vec3d.h:58
Vec3D & normalize()
Definition vec3d.h:103
Vec3D operator~() const
Definition vec3d.h:109
friend std::ostream & operator<<(std::ostream &out, Vec3D const &v)
Definition vec3d.h:122
Vec3D & operator+=(Vec3D const &v)
Definition vec3d.h:69
Vec3D operator%(Vec3D const &v) const
Definition vec3d.h:63
float X
Definition vec3d.h:246
float Z
Definition vec3d.h:246
float Y
Definition vec3d.h:246
float W
Definition vec3d.h:246
void rotate(float x0, float y0, float *x, float *y, float angle)
Definition vec3d.h:236