Game Engine 2D Platformer
TinyMath.hpp
Go to the documentation of this file.
1 /*
2  * @file TinyMath.hpp
3  * @brief TinyMath class interface
4  * @date 2021-03-12
5  ***********************************************/
6 #ifndef TINYMATH_H
7 #define TINYMATH_H
8 
9 #include <cmath>
10 #include <iostream>
11 // Forward references of each of the structs
12 struct Vector2D;
13 
14 // Vector3D performs vector operations with 3-dimensions
15 // The purpose of this class is primarily for 3D graphics
16 // applications.
17 struct Vector2D
18 {
19  // Note: x,y are a convention
20  // x,y could be position, but also any 3-component value.
21  float x, y;
22 
23  // Default conostrutcor
24  // 'why default?' https://stackoverflow.com/questions/20828907/the-new-keyword-default-in-c11
25  Vector2D() = default;
26 
27  // The "Real" constructor we want to use.
28  // This initializes the values x,y,z
29  Vector2D(float a, float b)
30  {
31  // TODO:
32  x = a;
33  y = b;
34  }
35 
36  // Index operator, allowing us to access the individual
37  // x,y,z components of our vector.
38  float &operator[](int i)
39  {
40  // TODO: Discuss with your partner why this works.
41  // There is no code to change here.
42  return ((&x)[i]);
43  }
44 
45  // Index operator, allowing us to access the individual
46  // x,y,z components of our vector.
47  const float &operator[](int i) const
48  {
49  // TODO: Discuss with your partner why this works.
50  // There is no code to change here.
51  return ((&x)[i]);
52  }
53 
54  // Multiplication Operator
55  // Multiply vector by a uniform-scalar.
56  Vector2D &operator*=(float s)
57  {
58  // TODO:
59  x *= s;
60  y *= s;
61  return (*this);
62  }
63 
64  // Division Operator
65  Vector2D &operator/=(float s)
66  {
67  // TODO:
68  x = x / s;
69  y = y / s;
70 
71  return (*this);
72  }
73 
74  // Addition operator
76  {
77  // TODO:
78  x += v.x;
79  y += v.y;
80  return (*this);
81  }
82 
83  // Subtraction operator
85  {
86  // TODO:
87  x -= v.x;
88  y -= v.y;
89 
90  return (*this);
91  }
92 };
93 
94 // Compute the dot product of a Vector3D
95 inline float Dot(const Vector2D &a, const Vector2D &b)
96 {
97  // TODO:
98  float value = (a.x * b.x) + (a.y * b.y);
99  return value;
100 }
101 
102 // Multiplication of a vector by a scalar values
103 inline Vector2D operator*(const Vector2D &v, float s)
104 {
105  // TODO:
106  Vector2D vec(v.x * s, v.y * s);
107  return vec;
108 }
109 
110 // Division of a vector by a scalar value.
111 inline Vector2D operator/(const Vector2D &v, float s)
112 {
113  // TODO:
114  Vector2D vec(v.x / s, v.y / s);
115  return vec;
116 }
117 
118 // Negation of a vector
119 // Use Case: Sometimes it is handy to apply a force in an opposite direction
120 inline Vector2D operator-(const Vector2D &v)
121 {
122  // TODO:
123  Vector2D vec(-v.x, -v.y);
124  return vec;
125 }
126 
127 // Return the magnitude of a vector
128 inline float Magnitude(const Vector2D &v)
129 {
130  return (sqrt(v.x * v.x + v.y * v.y));
131 }
132 
133 // Add two vectors together
134 inline Vector2D operator+(const Vector2D &a, const Vector2D &b)
135 {
136  // TODO:
137  Vector2D vec((a.x + b.x), (a.y + b.y));
138  return vec;
139 }
140 
141 // Subtract two vectors
142 inline Vector2D operator-(const Vector2D &a, const Vector2D &b)
143 {
144  // TODO:
145  Vector2D vec((a.x - b.x), (a.y - b.y));
146  return vec;
147 }
148 
149 // Vector Projection
150 inline Vector2D Project(const Vector2D &a, const Vector2D &b)
151 {
152  // TODO:
153  float b_len = Magnitude(b);
154  b_len *= b_len;
155  Vector2D vec = b * (Dot(b, a) / b_len);
156  return vec;
157 }
158 
159 // Set a vectors magnitude to 1
160 // Note: This is NOT generating a normal vector
161 inline Vector2D Normalize(const Vector2D &v)
162 {
163  // TODO:
164  assert(Magnitude(v) != 0);
165  Vector2D vec(v / Magnitude(v));
166  return vec;
167 }
168 
169 #endif
Vector2D Project(const Vector2D &a, const Vector2D &b)
Definition: TinyMath.hpp:150
float Dot(const Vector2D &a, const Vector2D &b)
Definition: TinyMath.hpp:95
float Magnitude(const Vector2D &v)
Definition: TinyMath.hpp:128
Vector2D operator*(const Vector2D &v, float s)
Definition: TinyMath.hpp:103
Vector2D operator/(const Vector2D &v, float s)
Definition: TinyMath.hpp:111
Vector2D operator+(const Vector2D &a, const Vector2D &b)
Definition: TinyMath.hpp:134
Vector2D Normalize(const Vector2D &v)
Definition: TinyMath.hpp:161
Vector2D operator-(const Vector2D &v)
Definition: TinyMath.hpp:120
Definition: TinyMath.hpp:18
Vector2D & operator*=(float s)
Definition: TinyMath.hpp:56
Vector2D & operator/=(float s)
Definition: TinyMath.hpp:65
float y
Definition: TinyMath.hpp:21
float & operator[](int i)
Definition: TinyMath.hpp:38
Vector2D()=default
Vector2D(float a, float b)
Definition: TinyMath.hpp:29
const float & operator[](int i) const
Definition: TinyMath.hpp:47
Vector2D & operator-=(const Vector2D &v)
Definition: TinyMath.hpp:84
float x
Definition: TinyMath.hpp:21
Vector2D & operator+=(const Vector2D &v)
Definition: TinyMath.hpp:75