Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Vector2.h
Go to the documentation of this file.
1
2/*
3 ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
4 ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
5 █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
6 ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
7 ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
8 ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
9 /vvvvvvvvvvvvvvvvvvv \=========================================,
10 `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
11 Katana Engine \/ © 2012 - Shuriken Studios LLC
12 http://www.shurikenstudios.com
13*/
14
15#pragma once
16
17namespace KatanaEngine
18{
20 class Vector2
21 {
22
23 public:
24
28 Vector2(const float x = 0, const float y = 0);
29
30
31 static const Vector2 ZERO;
32 static const Vector2 ONE;
33 static const Vector2 UNIT_X;
34 static const Vector2 UNIT_Y;
39 float LengthSquared() const;
40
43 float Length() const;
44
45
49 void Set(const float x, const float y) { X = x; Y = y; }
50
55 void Set(const Vector2 vector) { Set(vector.X, vector.Y); }
56
59 void Normalize();
60
63 bool IsZero() const { return (X == 0 && Y == 0); }
64
72 float DotProduct(const Vector2 &vector) const;
73
77 float CrossProduct(const Vector2 &vector) const;
78
83 static float Distance(const Vector2 &vector1, const Vector2 &vector2);
84
89 static float DistanceSquared(const Vector2 &vector1, const Vector2 &vector2);
90
97 static Vector2 Lerp(const Vector2 &start, const Vector2 &end, const float value);
98
103 static Vector2 GetRandom(bool normalize = false);
104
107 Vector2 Left() { return Vector2(-Y, X); }
108
111 Vector2 Right() { return Vector2(Y, -X); }
112
115 const Point ToPoint() const;
116
119 std::string ToString() const;
120
122 void Display() const { std::cout << ToString() << std::endl; }
123
124
128 Vector2 &operator= (const Vector2 &vector);
129
133 Vector2 &operator+=(const Vector2 &vector);
134
138 Vector2 &operator-=(const Vector2 &vector);
139
143 Vector2 &operator*=(const float scalar);
144
148 Vector2 &operator/=(const float scalar);
149
152 const Vector2 operator-() const { return Vector2(-X, -Y); }
153
157 const Vector2 operator+(const Vector2 &vector) const;
158
162 const Vector2 operator-(const Vector2 &vector) const;
163
167 const Vector2 operator*(const float scalar) const;
168
172 const Vector2 operator/(const float scalar) const;
173
174
178 bool operator==(const Vector2 &vector) const;
179
183 bool operator!=(const Vector2 &vector) const;
184
185
186 float X = 0;
187 float Y = 0;
189 };
190}
Defines a point in 2D space.
Definition Point.h:23
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
float Y
The y-coordinate of the vector.
Definition Vector2.h:187
const Vector2 operator*(const float scalar) const
Multiplies a vector by a scalar.
Definition Vector2.cpp:149
static const Vector2 UNIT_X
A unit vector on the x-axis.
Definition Vector2.h:33
const Point ToPoint() const
Converts the vector into a point.
Definition Vector2.cpp:169
float DotProduct(const Vector2 &vector) const
Calculates the dot product of two vectors.
Definition Vector2.cpp:50
Vector2 & operator/=(const float scalar)
Divides by a scalar.
Definition Vector2.cpp:131
void Set(const Vector2 vector)
Sets the components of the vector.
Definition Vector2.h:55
static float Distance(const Vector2 &vector1, const Vector2 &vector2)
Calculates the distance between two vectors.
Definition Vector2.cpp:60
float CrossProduct(const Vector2 &vector) const
Calculates the cross product between two vectors.
Definition Vector2.cpp:55
const Vector2 operator-() const
Negates the vector.
Definition Vector2.h:152
void Normalize()
Resize a vector to a length of one unit. If the starting vector is the zero vector,...
Definition Vector2.cpp:40
static const Vector2 UNIT_Y
A unit vector on the y-axis.
Definition Vector2.h:34
static Vector2 GetRandom(bool normalize=false)
Creates a random vector.
Definition Vector2.cpp:78
const Vector2 operator+(const Vector2 &vector) const
Adds two vectors.
Definition Vector2.cpp:139
Vector2 Right()
Calculates the right-hand orthogonal vector.
Definition Vector2.h:111
float LengthSquared() const
Calculates the length of the vector squared.
Definition Vector2.cpp:30
static const Vector2 ZERO
A vector with both of its components set to zero.
Definition Vector2.h:31
Vector2 & operator+=(const Vector2 &vector)
Adds a vector.
Definition Vector2.cpp:107
bool IsZero() const
Determines if the vector is the zero vector.
Definition Vector2.h:63
const Vector2 operator/(const float scalar) const
Divides a vector by a scalar.
Definition Vector2.cpp:154
static Vector2 Lerp(const Vector2 &start, const Vector2 &end, const float value)
Linearly interpolate between two vectors.
Definition Vector2.cpp:70
Vector2(const float x=0, const float y=0)
Instantiates a new Vector2 object.
Definition Vector2.cpp:24
static float DistanceSquared(const Vector2 &vector1, const Vector2 &vector2)
Calculates the distance squared between two vectors.
Definition Vector2.cpp:65
Vector2 Left()
Calculates the left-hand orthogonal vector.
Definition Vector2.h:107
void Display() const
Prints the vector to the console.
Definition Vector2.h:122
Vector2 & operator*=(const float scalar)
Multiplies by a scalar.
Definition Vector2.cpp:123
bool operator==(const Vector2 &vector) const
Determines if two vectors are equal.
Definition Vector2.cpp:159
void Set(const float x, const float y)
Sets the components of the vector.
Definition Vector2.h:49
Vector2 & operator-=(const Vector2 &vector)
Subtracts a vector.
Definition Vector2.cpp:115
std::string ToString() const
Gets a string representation of the vector.
Definition Vector2.cpp:89
float Length() const
Calculates the length of the vector.
Definition Vector2.cpp:35
bool operator!=(const Vector2 &vector) const
Determines if two vectors are not equal.
Definition Vector2.cpp:164
static const Vector2 ONE
A vector with both of its components set to one.
Definition Vector2.h:32
Vector2 & operator=(const Vector2 &vector)
Assigns the reference of a vector.
Definition Vector2.cpp:96
float X
The x-coordinate of the vector.
Definition Vector2.h:186
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18