GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector2f.hpp
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2 ** This software is in the public domain, furnished "as is", without technical
3 ** support, and with no warranty, express or implied, as to its usefulness for
4 ** any purpose.
5 **
6 ** Vector2f.hpp
7 ** Declares a vector consisting of 2 float values and its helper functions
8 **
9 ** Author: Nim
10 ** -------------------------------------------------------------------------*/
11 
12 #pragma once
13 #ifndef VECTOR2F_HPP
14 #define VECTOR2F_HPP
15 
16 #include <string>
17 
18 namespace radix {
19 
23 struct Vector2f {
24  union {
25  float x, r, s, u;
26  };
27  union {
28  float y, g, t, v;
29  };
30 
31  static const Vector2f ZERO, UP;
32 
33  /* Core */
34  constexpr Vector2f()
35  : x(0), y(0) {}
36  constexpr Vector2f(float x, float y)
37  : x(x), y(y) {}
38  constexpr Vector2f(float v)
39  : x(v), y(v) {}
40 
41  float length() const;
42  std::string str() const;
43 
44  /* Operator overloads */
45  constexpr bool operator==(const Vector2f &v) const {
46  return x == v.x && y == v.y;
47  }
48 
49  constexpr bool operator!=(const Vector2f &v) const {
50  return x != v.x || y != v.y;
51  }
52 
53  constexpr Vector2f operator-() const {
54  return Vector2f(-x, -y);
55  }
56 
57  constexpr Vector2f operator+(const Vector2f& v) const {
58  return Vector2f(x + v.x, y + v.y);
59  }
61  x += v.x;
62  y += v.y;
63  return *this;
64  }
65 
66  constexpr Vector2f operator-(const Vector2f& v) const {
67  return Vector2f(x - v.x, y - v.y);
68  }
70  x -= v.x;
71  y -= v.y;
72  return *this;
73  }
74 
75  constexpr Vector2f operator*(const Vector2f& v) const {
76  return Vector2f(x * v.x, y * v.y);
77  }
79  x *= v.x;
80  y *= v.y;
81  return *this;
82  }
83 
84  constexpr Vector2f operator*(float scale) const {
85  return Vector2f(x * scale, y * scale);
86  }
87  Vector2f& operator*=(float scale) {
88  x *= scale;
89  y *= scale;
90  return *this;
91  }
92 
93  constexpr Vector2f operator/(float divisor) const {
94  return Vector2f(x / divisor, y / divisor);
95  }
96  Vector2f& operator/=(float divisor) {
97  x /= divisor;
98  y /= divisor;
99  return *this;
100  }
101 
102  constexpr Vector2f operator/=(const Vector2f& v) const {
103  return Vector2f(x / v.x, y / v.y);
104  }
106  x /= v.x;
107  y /= v.y;
108  return *this;
109  }
110 
111  bool fuzzyEqual(const Vector2f&, float threshold = .02f) const;
112 };
113 
114 /* Utility functions */
115 constexpr inline float dot(const Vector2f &v1, const Vector2f &v2) {
116  return v1.x * v2.x + v1.y * v2.y;
117 }
118 inline Vector2f normalize(const Vector2f &v) {
119  return v / v.length();
120 }
121 
122 } /* namespace radix */
123 
124 #endif /* VECTOR2F_HPP */
Vector2f & operator/=(const Vector2f &v)
Definition: Vector2f.hpp:105
constexpr Vector2f(float v)
Definition: Vector2f.hpp:38
Definition: GameController.hpp:7
Vector2f & operator*=(float scale)
Definition: Vector2f.hpp:87
float x
Definition: Vector2f.hpp:25
constexpr bool operator!=(const Vector2f &v) const
Definition: Vector2f.hpp:49
float t
Definition: Vector2f.hpp:28
Vector2f & operator-=(const Vector2f &v)
Definition: Vector2f.hpp:69
constexpr Vector2f operator*(const Vector2f &v) const
Definition: Vector2f.hpp:75
constexpr Vector2f operator*(float scale) const
Definition: Vector2f.hpp:84
static const Vector2f ZERO
Definition: Vector2f.hpp:31
constexpr Vector2f operator/=(const Vector2f &v) const
Definition: Vector2f.hpp:102
float y
Definition: Vector2f.hpp:28
bool fuzzyEqual(const Vector2f &, float threshold=.02f) const
Definition: Vector2f.cpp:33
constexpr Vector2f(float x, float y)
Definition: Vector2f.hpp:36
float v
Definition: Vector2f.hpp:28
std::string str() const
Definition: Vector2f.cpp:27
Vector2f & operator/=(float divisor)
Definition: Vector2f.hpp:96
constexpr Vector2f operator-(const Vector2f &v) const
Definition: Vector2f.hpp:66
constexpr float dot(const Vector2f &v1, const Vector2f &v2)
Definition: Vector2f.hpp:115
constexpr Vector2f operator+(const Vector2f &v) const
Definition: Vector2f.hpp:57
float u
Definition: Vector2f.hpp:25
float s
Definition: Vector2f.hpp:25
2-dimensional float-based vector/point storage and manipulation struct
Definition: Vector2f.hpp:23
float g
Definition: Vector2f.hpp:28
float r
Definition: Vector2f.hpp:25
static const Vector2f UP
Definition: Vector2f.hpp:31
Vector2f & operator+=(const Vector2f &v)
Definition: Vector2f.hpp:60
constexpr Vector2f operator-() const
Definition: Vector2f.hpp:53
float length() const
Definition: Vector2f.cpp:23
constexpr Vector2f operator/(float divisor) const
Definition: Vector2f.hpp:93
constexpr Vector2f()
Definition: Vector2f.hpp:34
constexpr bool operator==(const Vector2f &v) const
Definition: Vector2f.hpp:45
Vector2f & operator*=(const Vector2f &v)
Definition: Vector2f.hpp:78
Vector2f normalize(const Vector2f &v)
Definition: Vector2f.hpp:118