Pyrogenesis  trunk
Vector2D.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_VECTOR2D
19 #define INCLUDED_VECTOR2D
20 
21 #include "ps/containers/Span.h"
22 
23 #include <math.h>
24 
25 class CSize2D;
26 
27 /*
28  * Provides an interface for a vector in R2 and allows vector and
29  * scalar operations on it.
30  */
31 class CVector2D
32 {
33 public:
34  CVector2D() : X(0.0f), Y(0.0f) {}
35  CVector2D(float x, float y) : X(x), Y(y) {}
36  CVector2D(const CSize2D& size);
37 
38  operator float*()
39  {
40  return &X;
41  }
42 
43  operator const float*() const
44  {
45  return &X;
46  }
47 
48  bool operator==(const CVector2D& v) const;
49  bool operator!=(const CVector2D& v) const;
50 
52  {
53  return CVector2D(-X, -Y);
54  }
55 
56  CVector2D operator+(const CVector2D& t) const
57  {
58  return CVector2D(X + t.X, Y + t.Y);
59  }
60 
61  CVector2D operator-(const CVector2D& t) const
62  {
63  return CVector2D(X - t.X, Y - t.Y);
64  }
65 
66  CVector2D operator*(float f) const
67  {
68  return CVector2D(X * f, Y * f);
69  }
70 
71  CVector2D operator/(float f) const
72  {
73  float inv = 1.0f / f;
74  return CVector2D(X * inv, Y * inv);
75  }
76 
78  {
79  X += t.X;
80  Y += t.Y;
81  return *this;
82  }
83 
85  {
86  X -= t.X;
87  Y -= t.Y;
88  return *this;
89  }
90 
92  {
93  X *= f;
94  Y *= f;
95  return *this;
96  }
97 
99  {
100  float invf = 1.0f / f;
101  X *= invf;
102  Y *= invf;
103  return *this;
104  }
105 
106  float Dot(const CVector2D& a) const
107  {
108  return X * a.X + Y * a.Y;
109  }
110 
111  float LengthSquared() const
112  {
113  return Dot(*this);
114  }
115 
116  float Length() const
117  {
118  return (float)sqrt(LengthSquared());
119  }
120 
121  void Normalize()
122  {
123  float mag = Length();
124  X /= mag;
125  Y /= mag;
126  }
127 
129  {
130  float mag = Length();
131  return CVector2D(X / mag, Y / mag);
132  }
133 
134  /**
135  * Returns a version of this vector rotated counterclockwise by @p angle radians.
136  */
137  CVector2D Rotated(float angle) const
138  {
139  float c = cosf(angle);
140  float s = sinf(angle);
141  return CVector2D(
142  c*X - s*Y,
143  s*X + c*Y
144  );
145  }
146 
147  /**
148  * Rotates this vector counterclockwise by @p angle radians.
149  */
150  void Rotate(float angle)
151  {
152  float c = cosf(angle);
153  float s = sinf(angle);
154  float newX = c*X - s*Y;
155  float newY = s*X + c*Y;
156  X = newX;
157  Y = newY;
158  }
159 
160  CVector2D operator+(const CSize2D& size) const;
161  CVector2D operator-(const CSize2D& size) const;
162 
163  void operator+=(const CSize2D& size);
164  void operator-=(const CSize2D& size);
165 
166  // Returns 2 element array of floats, e.g. for vec2 uniforms.
168  {
169  // Additional check to prevent a weird compiler having a different
170  // alignement for an array and a class members.
171  static_assert(
172  sizeof(CVector2D) == sizeof(float) * 2u &&
173  offsetof(CVector2D, X) == 0 &&
174  offsetof(CVector2D, Y) == sizeof(float),
175  "Vector2D should be properly layouted to use AsFloatArray");
176  return PS::span<const float>(&X, 2);
177  }
178 
179 public:
180  float X, Y;
181 };
182 
183 #endif // INCLUDED_VECTOR2D
CVector2D()
Definition: Vector2D.h:34
bool operator!=(const CVector2D &v) const
Definition: Vector2D.cpp:33
void Normalize()
Definition: Vector2D.h:121
float X
Definition: Vector2D.h:180
CVector2D & operator-=(const CVector2D &t)
Definition: Vector2D.h:84
void Rotate(float angle)
Rotates this vector counterclockwise by angle radians.
Definition: Vector2D.h:150
CVector2D(float x, float y)
Definition: Vector2D.h:35
float LengthSquared() const
Definition: Vector2D.h:111
float Length() const
Definition: Vector2D.h:116
Definition: Size2D.h:24
CVector2D operator-() const
Definition: Vector2D.h:51
CVector2D operator-(const CVector2D &t) const
Definition: Vector2D.h:61
bool operator==(const CVector2D &v) const
Definition: Vector2D.cpp:28
PS::span< const float > AsFloatArray() const
Definition: Vector2D.h:167
CVector2D Rotated(float angle) const
Returns a version of this vector rotated counterclockwise by angle radians.
Definition: Vector2D.h:137
float Y
Definition: Vector2D.h:180
Definition: Vector2D.h:31
CVector2D & operator+=(const CVector2D &t)
Definition: Vector2D.h:77
CVector2D & operator*=(float f)
Definition: Vector2D.h:91
CVector2D & operator/=(float f)
Definition: Vector2D.h:98
CVector2D operator*(float f) const
Definition: Vector2D.h:66
float Dot(const CVector2D &a) const
Definition: Vector2D.h:106
CVector2D operator/(float f) const
Definition: Vector2D.h:71
CVector2D operator+(const CVector2D &t) const
Definition: Vector2D.h:56
CVector2D Normalized() const
Definition: Vector2D.h:128
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36