Pyrogenesis  trunk
Vector2D.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 /*
19  * Provides an interface for a vector in R2 and allows vector and
20  * scalar operations on it
21  */
22 
23 #ifndef INCLUDED_MATHS_VECTOR2D
24 #define INCLUDED_MATHS_VECTOR2D
25 
26 
27 #include <math.h>
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 // CVector2D:
31 class CVector2D
32 {
33 public:
34  CVector2D() {}
35  CVector2D(float x, float y) : X(x), Y(y) {}
36 
37  operator float*()
38  {
39  return &X;
40  }
41 
42  operator const float*() const
43  {
44  return &X;
45  }
46 
48  {
49  return CVector2D(-X, -Y);
50  }
51 
52  CVector2D operator+(const CVector2D& t) const
53  {
54  return CVector2D(X + t.X, Y + t.Y);
55  }
56 
57  CVector2D operator-(const CVector2D& t) const
58  {
59  return CVector2D(X - t.X, Y - t.Y);
60  }
61 
62  CVector2D operator*(float f) const
63  {
64  return CVector2D(X * f, Y * f);
65  }
66 
67  CVector2D operator/(float f) const
68  {
69  float inv = 1.0f / f;
70  return CVector2D(X * inv, Y * inv);
71  }
72 
74  {
75  X += t.X;
76  Y += t.Y;
77  return *this;
78  }
79 
81  {
82  X -= t.X;
83  Y -= t.Y;
84  return *this;
85  }
86 
88  {
89  X *= f;
90  Y *= f;
91  return *this;
92  }
93 
95  {
96  float invf = 1.0f / f;
97  X *= invf;
98  Y *= invf;
99  return *this;
100  }
101 
102  float Dot(const CVector2D& a) const
103  {
104  return X * a.X + Y * a.Y;
105  }
106 
107  float LengthSquared() const
108  {
109  return Dot(*this);
110  }
111 
112  float Length() const
113  {
114  return (float)sqrt(LengthSquared());
115  }
116 
117  void Normalize()
118  {
119  float mag = Length();
120  X /= mag;
121  Y /= mag;
122  }
123 
125  {
126  float mag = Length();
127  return CVector2D(X / mag, Y / mag);
128  }
129 
130  /**
131  * Returns a version of this vector rotated counterclockwise by @p angle radians.
132  */
133  CVector2D Rotated(float angle) const
134  {
135  float c = cosf(angle);
136  float s = sinf(angle);
137  return CVector2D(
138  c*X - s*Y,
139  s*X + c*Y
140  );
141  }
142 
143  /**
144  * Rotates this vector counterclockwise by @p angle radians.
145  */
146  void Rotate(float angle)
147  {
148  float c = cosf(angle);
149  float s = sinf(angle);
150  float newX = c*X - s*Y;
151  float newY = s*X + c*Y;
152  X = newX;
153  Y = newY;
154  }
155 
156 public:
157  float X, Y;
158 };
159 //////////////////////////////////////////////////////////////////////////////////
160 
161 
162 #endif
CVector2D()
Definition: Vector2D.h:34
void Normalize()
Definition: Vector2D.h:117
float X
Definition: Vector2D.h:157
CVector2D & operator-=(const CVector2D &t)
Definition: Vector2D.h:80
CVector2D operator-() const
Definition: Vector2D.h:47
float LengthSquared() const
Definition: Vector2D.h:107
void Rotate(float angle)
Rotates this vector counterclockwise by angle radians.
Definition: Vector2D.h:146
CVector2D Rotated(float angle) const
Returns a version of this vector rotated counterclockwise by angle radians.
Definition: Vector2D.h:133
CVector2D(float x, float y)
Definition: Vector2D.h:35
CVector2D operator/(float f) const
Definition: Vector2D.h:67
CVector2D Normalized() const
Definition: Vector2D.h:124
CVector2D operator*(float f) const
Definition: Vector2D.h:62
float Length() const
Definition: Vector2D.h:112
float Y
Definition: Vector2D.h:157
Definition: Vector2D.h:31
CVector2D & operator+=(const CVector2D &t)
Definition: Vector2D.h:73
CVector2D & operator*=(float f)
Definition: Vector2D.h:87
CVector2D operator+(const CVector2D &t) const
Definition: Vector2D.h:52
CVector2D & operator/=(float f)
Definition: Vector2D.h:94
float Dot(const CVector2D &a) const
Definition: Vector2D.h:102
CVector2D operator-(const CVector2D &t) const
Definition: Vector2D.h:57