LinearAlgebra
Loading...
Searching...
No Matches
Vector2.h
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0.If a copy of the MPL was not distributed with this
3// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
4
5#ifndef VECTOR2_H
6#define VECTOR2_H
7
8#include "Angle.h"
9
10extern "C" {
16typedef struct Vec2 {
20 float x;
24 float y;
25
26} Vec2;
27}
28
29namespace Passer {
30namespace LinearAlgebra {
31
32struct Vector3;
33template <typename T> class PolarOf;
34
38struct Vector2 : Vec2 {
39 friend struct Vec2;
40
41public:
43 Vector2();
47 Vector2(float right, float forward);
55
57 ~Vector2();
58
60 const static Vector2 zero;
62 const static Vector2 one;
64 const static Vector2 forward;
66 const static Vector2 back;
68 const static Vector2 right;
70 const static Vector2 left;
73 const static Vector2 up;
76 const static Vector2 down;
77
83 bool operator==(const Vector2 &v);
84
88 static float Magnitude(const Vector2 &v);
91 float magnitude() const;
98 static float SqrMagnitude(const Vector2 &v);
104 float sqrMagnitude() const;
105
109 static Vector2 Normalize(const Vector2 &v);
112 Vector2 normalized() const;
113
117
121 Vector2 operator-(const Vector2 &v) const;
122 Vector2 operator-=(const Vector2 &v);
126 Vector2 operator+(const Vector2 &v) const;
127 Vector2 operator+=(const Vector2 &v);
128
135 static Vector2 Scale(const Vector2 &v1, const Vector2 &v2);
141 friend Vector2 operator*(const Vector2 &v, float f) {
142 return Vector2(v.x * f, v.y * f);
143 }
144 friend Vector2 operator*(float f, const Vector2 &v) {
145 return Vector2(v.x * f, v.y * f);
146 // return Vector2(f * v.x, f * v.y);
147 }
148 Vector2 operator*=(float f);
153 friend Vector2 operator/(const Vector2 &v, float f) {
154 return Vector2(v.x / f, v.y / f);
155 }
156 friend Vector2 operator/(float f, const Vector2 &v) {
157 return Vector2(f / v.x, f / v.y);
158 }
159 Vector2 operator/=(float f);
160
165 static float Dot(const Vector2 &v1, const Vector2 &v2);
166
171 static float Distance(const Vector2 &v1, const Vector2 &v2);
172
180 static float Angle(const Vector2 &v1, const Vector2 &v2);
185 static float SignedAngle(const Vector2 &v1, const Vector2 &v2);
186
192
201 static Vector2 Lerp(const Vector2 &v1, const Vector2 &v2, float f);
202};
203
204} // namespace LinearAlgebra
205} // namespace Passer
206using namespace Passer::LinearAlgebra;
207
208#include "Polar.h"
209
210#endif
An angle in various representations.
Definition Angle.h:21
A polar vector using an angle in various representations.
Definition Polar.h:18
A 2-dimensional vector.
Definition Vector2.h:38
static const Vector2 up
A normalized up-oriented vector.
Definition Vector2.h:73
Vector2 operator+(const Vector2 &v) const
Add a vector to this vector.
Definition Vector2.cpp:90
Vector2 normalized() const
Convert the vector to a length 1.
Definition Vector2.cpp:71
Vector2()
A new 2-dimensional zero vector.
Definition Vector2.cpp:16
static Vector2 Normalize(const Vector2 &v)
Convert the vector to a length of 1.
Definition Vector2.cpp:63
static const Vector2 zero
A vector with zero for all axis.
Definition Vector2.h:60
static Vector2 Rotate(const Vector2 &v, Passer::LinearAlgebra::AngleSingle a)
Rotate the vector.
Definition Vector2.cpp:154
static const Vector2 one
A vector with one for all axis.
Definition Vector2.h:62
static float SignedAngle(const Vector2 &v1, const Vector2 &v2)
The signed angle between two vectors.
Definition Vector2.cpp:136
Vector2 operator-()
Negate the vector such that it points in the opposite direction.
Definition Vector2.cpp:80
static float Dot(const Vector2 &v1, const Vector2 &v2)
The dot product of two vectors.
Definition Vector2.cpp:125
static const Vector2 down
A normalized down-oriented vector.
Definition Vector2.h:76
static float Angle(const Vector2 &v1, const Vector2 &v2)
The angle between two vectors.
Definition Vector2.cpp:133
static Vector2 Scale(const Vector2 &v1, const Vector2 &v2)
Scale the vector using another vector.
Definition Vector2.cpp:99
Vector2(float right, float forward)
A new 2-dimensional vector.
Vector2(PolarOf< float > v)
Convert a Polar vector to a 2-dimensional vector.
static Vector2 Lerp(const Vector2 &v1, const Vector2 &v2, float f)
Lerp (linear interpolation) between two vectors.
Definition Vector2.cpp:172
static const Vector2 right
A normalized right-oriented vector.
Definition Vector2.h:68
static const Vector2 left
A normalized left-oriented vector.
Definition Vector2.h:70
float magnitude() const
The vector length.
Definition Vector2.cpp:59
friend Vector2 operator*(const Vector2 &v, float f)
Scale the vector uniformly up.
Definition Vector2.h:141
float sqrMagnitude() const
The squared vector length.
Definition Vector2.cpp:61
static float Distance(const Vector2 &v1, const Vector2 &v2)
The distance between two vectors.
Definition Vector2.cpp:129
friend Vector2 operator/(const Vector2 &v, float f)
Scale the vector uniformly down.
Definition Vector2.h:153
~Vector2()
Vector2 destructor.
Definition Vector2.cpp:41
static float Magnitude(const Vector2 &v)
The vector length.
Definition Vector2.cpp:56
bool operator==(const Vector2 &v)
Check if this vector to the given vector.
Definition Vector2.cpp:52
static const Vector2 back
A normalized back-oriented vector.
Definition Vector2.h:66
static const Vector2 forward
A normalized forward-oriented vector.
Definition Vector2.h:64
static float SqrMagnitude(const Vector2 &v)
The squared vector length.
Definition Vector2.cpp:60
A 3-dimensional vector.
Definition Vector3.h:42
2-dimensional Vector representation (C-style)
Definition Vector2.h:16
float y
The upward/forward axis of the vector.
Definition Vector2.h:24
float x
The right axis of the vector.
Definition Vector2.h:20