Unify duplicate Breadth-First-Search traversing of the LayeredPainter and SmoothEleva...
[0ad.git] / source / maths / Vector2D.h
blob55787d81f68acd81b60da182ef14dafb45ab891a
1 /* Copyright (C) 2011 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * Provides an interface for a vector in R2 and allows vector and
20 * scalar operations on it
23 #ifndef INCLUDED_MATHS_VECTOR2D
24 #define INCLUDED_MATHS_VECTOR2D
27 #include <math.h>
29 ///////////////////////////////////////////////////////////////////////////////
30 // CVector2D:
31 class CVector2D
33 public:
34 CVector2D() {}
35 CVector2D(float x, float y) : X(x), Y(y) {}
37 operator float*()
39 return &X;
42 operator const float*() const
44 return &X;
47 CVector2D operator-() const
49 return CVector2D(-X, -Y);
52 CVector2D operator+(const CVector2D& t) const
54 return CVector2D(X + t.X, Y + t.Y);
57 CVector2D operator-(const CVector2D& t) const
59 return CVector2D(X - t.X, Y - t.Y);
62 CVector2D operator*(float f) const
64 return CVector2D(X * f, Y * f);
67 CVector2D operator/(float f) const
69 float inv = 1.0f / f;
70 return CVector2D(X * inv, Y * inv);
73 CVector2D& operator+=(const CVector2D& t)
75 X += t.X;
76 Y += t.Y;
77 return *this;
80 CVector2D& operator-=(const CVector2D& t)
82 X -= t.X;
83 Y -= t.Y;
84 return *this;
87 CVector2D& operator*=(float f)
89 X *= f;
90 Y *= f;
91 return *this;
94 CVector2D& operator/=(float f)
96 float invf = 1.0f / f;
97 X *= invf;
98 Y *= invf;
99 return *this;
102 float Dot(const CVector2D& a) const
104 return X * a.X + Y * a.Y;
107 float LengthSquared() const
109 return Dot(*this);
112 float Length() const
114 return (float)sqrt(LengthSquared());
117 void Normalize()
119 float mag = Length();
120 X /= mag;
121 Y /= mag;
124 CVector2D Normalized() const
126 float mag = Length();
127 return CVector2D(X / mag, Y / mag);
131 * Returns a version of this vector rotated counterclockwise by @p angle radians.
133 CVector2D Rotated(float angle) const
135 float c = cosf(angle);
136 float s = sinf(angle);
137 return CVector2D(
138 c*X - s*Y,
139 s*X + c*Y
144 * Rotates this vector counterclockwise by @p angle radians.
146 void Rotate(float angle)
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;
156 public:
157 float X, Y;
159 //////////////////////////////////////////////////////////////////////////////////
162 #endif