Arcade Shooter
Vector2.h
1 
2 /* ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
3  ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
4  █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
5  ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
6  ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
7  ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
8  /vvvvvvvvvvvvvvvvvvv \=========================================,
9  `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
10  Katana Engine \/ © 2012 - Shuriken Studios LLC */
11 
12 #pragma once
13 
14 namespace KatanaEngine
15 {
18  class Vector2
19  {
20 
21  public:
22 
26  Vector2(const float x = 0, const float y = 0);
27 
28 
29  static const Vector2 Zero;
30  static const Vector2 One;
31  static const Vector2 UnitX;
32  static const Vector2 UnitY;
37  float LengthSquared() const;
38 
41  float Length() const;
42 
43 
47  void Set(const float x, const float y) { X = x; Y = y; }
48 
53  void Set(const Vector2 vector) { Set(vector.X, vector.Y); }
54 
57  void Normalize();
58 
61  bool IsZero() const { return (X == 0 && Y == 0); }
62 
70  float DotProduct(const Vector2 &vector) const;
71 
75  float CrossProduct(const Vector2 &vector) const;
76 
81  static float Distance(const Vector2 &vector1, const Vector2 &vector2);
82 
87  static float DistanceSquared(const Vector2 &vector1, const Vector2 &vector2);
88 
95  static Vector2 Lerp(const Vector2 &start, const Vector2 &end, const float value);
96 
101  static Vector2 GetRandom(bool normalize = false);
102 
105  Vector2 Left() { return Vector2(-Y, X); }
106 
109  Vector2 Right() { return Vector2(Y, -X); }
110 
113  const Point ToPoint() const;
114 
117  std::string ToString() const;
118 
120  void Display() const { std::cout << ToString() << std::endl; }
121 
122 
126  Vector2 &operator= (const Vector2 &vector);
127 
131  Vector2 &operator+=(const Vector2 &vector);
132 
136  Vector2 &operator-=(const Vector2 &vector);
137 
141  Vector2 &operator*=(const float scalar);
142 
146  Vector2 &operator/=(const float scalar);
147 
150  const Vector2 operator-() const { return Vector2(-X, -Y); }
151 
155  const Vector2 operator+(const Vector2 &vector) const;
156 
160  const Vector2 operator-(const Vector2 &vector) const;
161 
165  const Vector2 operator*(const float scalar) const;
166 
170  const Vector2 operator/(const float scalar) const;
171 
172 
176  bool operator==(const Vector2 &vector) const;
177 
181  bool operator!=(const Vector2 &vector) const;
182 
183 
184  float X;
185  float Y;
187  };
188 }
Vector2 & operator=(const Vector2 &vector)
Assigns the reference of a vector.
Definition: Vector2.cpp:93
Vector2 Left()
Calculates the left-hand orthogonal vector.
Definition: Vector2.h:105
float DotProduct(const Vector2 &vector) const
Calculates the dot product of two vectors.
Definition: Vector2.cpp:47
static Vector2 GetRandom(bool normalize=false)
Creates a random vector.
Definition: Vector2.cpp:75
bool operator==(const Vector2 &vector) const
Determines if two vectors are equal.
Definition: Vector2.cpp:156
static const Vector2 UnitY
A unit vector on the y-axis.
Definition: Vector2.h:32
void Display() const
Prints the vector to the console.
Definition: Vector2.h:120
void Set(const float x, const float y)
Sets the components of the vector.
Definition: Vector2.h:47
bool operator!=(const Vector2 &vector) const
Determines if two vectors are not equal.
Definition: Vector2.cpp:161
bool IsZero() const
Determines if the vector is the zero vector.
Definition: Vector2.h:61
const Vector2 operator/(const float scalar) const
Divides a vector by a scalar.
Definition: Vector2.cpp:151
static float Distance(const Vector2 &vector1, const Vector2 &vector2)
Calculates the distance between two vectors.
Definition: Vector2.cpp:57
Vector2 & operator-=(const Vector2 &vector)
Subtracts a vector.
Definition: Vector2.cpp:112
Vector2 & operator*=(const float scalar)
Multiplies by a scalar.
Definition: Vector2.cpp:120
static const Vector2 Zero
A vector with both of its components set to zero.
Definition: Vector2.h:29
float X
The x-coordinate of the vector.
Definition: Vector2.h:184
Defines a vector with 2 components (x and y). This is additional information...
Definition: Vector2.h:18
Vector2(const float x=0, const float y=0)
Instantiates a new Vector2 object.
Definition: Vector2.cpp:21
static float DistanceSquared(const Vector2 &vector1, const Vector2 &vector2)
Calculates the distance squared between two vectors.
Definition: Vector2.cpp:62
Vector2 & operator+=(const Vector2 &vector)
Adds a vector.
Definition: Vector2.cpp:104
float Length() const
Calculates the length of the vector.
Definition: Vector2.cpp:32
std::string ToString() const
Gets a string representation of the vector.
Definition: Vector2.cpp:86
static Vector2 Lerp(const Vector2 &start, const Vector2 &end, const float value)
Linearly interpolate between two vectors.
Definition: Vector2.cpp:67
const Point ToPoint() const
Converts the vector into a point.
Definition: Vector2.cpp:166
float Y
The y-coordinate of the vector.
Definition: Vector2.h:185
static const Vector2 UnitX
A unit vector on the x-axis.
Definition: Vector2.h:31
Vector2 Right()
Calculates the right-hand orthogonal vector.
Definition: Vector2.h:109
void Set(const Vector2 vector)
Sets the components of the vector.
Definition: Vector2.h:53
float CrossProduct(const Vector2 &vector) const
Calculates the cross product between two vectors.
Definition: Vector2.cpp:52
Defines a point in 2D space.
Definition: Point.h:19
float LengthSquared() const
Calculates the length of the vector squared.
Definition: Vector2.cpp:27
const Vector2 operator+(const Vector2 &vector) const
Adds two vectors.
Definition: Vector2.cpp:136
static const Vector2 One
A vector with both of its components set to one.
Definition: Vector2.h:30
Vector2 & operator/=(const float scalar)
Divides by a scalar.
Definition: Vector2.cpp:128
const Vector2 operator*(const float scalar) const
Multiplies a vector by a scalar.
Definition: Vector2.cpp:146
const Vector2 operator-() const
Negates the vector.
Definition: Vector2.h:150
void Normalize()
Resize a vector to a length of one unit. If the starting vector is the zero vector, the call will be ignored.
Definition: Vector2.cpp:37
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition: Animation.cpp:14