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
29 ///////////////////////////////////////////////////////////////////////////////
35 CVector2D(float x
, float y
) : X(x
), Y(y
) {}
42 operator const float*() const
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
70 return CVector2D(X
* inv
, Y
* inv
);
73 CVector2D
& operator+=(const CVector2D
& t
)
80 CVector2D
& operator-=(const CVector2D
& t
)
87 CVector2D
& operator*=(float f
)
94 CVector2D
& operator/=(float f
)
96 float invf
= 1.0f
/ f
;
102 float Dot(const CVector2D
& a
) const
104 return X
* a
.X
+ Y
* a
.Y
;
107 float LengthSquared() const
114 return (float)sqrt(LengthSquared());
119 float mag
= Length();
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
);
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
;
159 //////////////////////////////////////////////////////////////////////////////////