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