Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Vector2.cpp
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#include "KatanaEngine.h"
16
17namespace KatanaEngine
18{
19 const Vector2 Vector2::ZERO = Vector2(0, 0);
20 const Vector2 Vector2::ONE = Vector2(1, 1);
21 const Vector2 Vector2::UNIT_X = Vector2(1, 0);
22 const Vector2 Vector2::UNIT_Y = Vector2(0, 1);
23
24 Vector2::Vector2(const float x, const float y)
25 {
26 X = x;
27 Y = y;
28 }
29
31 {
32 return ((X * X) + (Y * Y));
33 }
34
35 float Vector2::Length() const
36 {
37 return sqrtf(X * X + Y * Y);
38 }
39
41 {
42 if (!IsZero())
43 {
44 float len = Length();
45 X /= len;
46 Y /= len;
47 }
48 }
49
50 float Vector2::DotProduct(const Vector2 &vector) const
51 {
52 return (X * vector.X) + (Y * vector.Y);
53 }
54
55 float Vector2::CrossProduct(const Vector2 &vector) const
56 {
57 return (X * vector.Y) - (Y * vector.X);
58 }
59
60 float Vector2::Distance(const Vector2 &vector1, const Vector2 &vector2)
61 {
62 return sqrtf(pow((vector2.X - vector1.X), 2) + pow((vector2.Y - vector1.Y), 2));
63 }
64
65 float Vector2::DistanceSquared(const Vector2 &vector1, const Vector2 &vector2)
66 {
67 return pow((vector2.X - vector1.X), 2) + pow((vector2.Y - vector1.Y), 2);
68 }
69
70 Vector2 Vector2::Lerp(const Vector2 &start, const Vector2 &end, const float value)
71 {
72 if (value < 0) return start;
73 if (value > 1) return end;
74
75 return start + (end - start) * value;
76 }
77
79 {
80 float x = Math::GetRandomFloat() * 2 - 1;
81 float y = Math::GetRandomFloat() * 2 - 1;
82
83 Vector2 result(x, y);
84 if (normalize) result.Normalize();
85
86 return result;
87 }
88
89 std::string Vector2::ToString() const
90 {
91 std::ostringstream ss;
92 ss << "{ " << X << ", " << Y << " }";
93 return ss.str();
94 }
95
97 {
98 if (this != &vector)
99 {
100 X = vector.X;
101 Y = vector.Y;
102 }
103
104 return *this;
105 }
106
108 {
109 X += vector.X;
110 Y += vector.Y;
111
112 return *this;
113 }
114
116 {
117 X -= vector.X;
118 Y -= vector.Y;
119
120 return *this;
121 }
122
123 Vector2 &Vector2::operator*=(const float scalar)
124 {
125 X *= scalar;
126 Y *= scalar;
127
128 return *this;
129 }
130
131 Vector2 &Vector2::operator/=(const float scalar)
132 {
133 X /= scalar;
134 Y /= scalar;
135
136 return *this;
137 }
138
139 const Vector2 Vector2::operator+(const Vector2 &vector) const
140 {
141 return Vector2(*this) += vector;
142 }
143
144 const Vector2 Vector2::operator-(const Vector2 &vector) const
145 {
146 return Vector2(*this) -= vector;
147 }
148
149 const Vector2 Vector2::operator*(const float scalar) const
150 {
151 return Vector2(*this) *= scalar;
152 }
153
154 const Vector2 Vector2::operator/(const float scalar) const
155 {
156 return Vector2(*this) /= scalar;
157 }
158
159 bool Vector2::operator==(const Vector2 &vector) const
160 {
161 return ((X == vector.X) && (Y == vector.Y));
162 }
163
164 bool Vector2::operator!=(const Vector2 &vector) const
165 {
166 return !((X == vector.X) && (Y == vector.Y));
167 }
168
170 {
171 return Point((int)X, (int)Y);
172 }
173}
static float GetRandomFloat()
Get a random number between zero and one.
Definition MathUtil.h:48
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
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
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 & 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
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