cpp-sdl2
C++ header-only SDL2 wrapper
vec2.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL_rect.h>
4 #include <algorithm>
5 #include <cmath>
6 #include <ostream>
7 
9 namespace sdl::details
10 {
12 template<typename T>
13 struct Vec2Base
14 {
15  T x, y;
16 };
17 
18 } // namespace sdl::details
19 
20 namespace sdl
21 {
23 template<typename T, class Base = details::Vec2Base<T>>
24 class Vec2 : public Base
25 {
26 public:
28  constexpr Vec2() : Base{0, 0} {}
30  constexpr Vec2(T x, T y) : Base{x, y} {}
32  constexpr Vec2(SDL_Point const& p) : Base{p.x, p.y} {}
33 
34  constexpr Vec2(Vec2 const&) noexcept = default;
35  constexpr Vec2(Vec2&&) noexcept = default;
36 
38  template<typename A>
39  static constexpr Vec2 from_polar(A alpha, T radius)
40  {
41  return Vec2{radius * std::cos(alpha), radius * std::sin(alpha)};
42  }
43 
44  Vec2& operator=(Vec2 const&) noexcept = default;
45  Vec2& operator=(Vec2&&) noexcept = default;
46 
48  constexpr Vec2 operator-() const { return Vec2{-Base::x, -Base::y}; }
49 
51  Vec2& operator+=(Vec2 const& other)
52  {
53  Base::x += other.x;
54  Base::y += other.y;
55  return *this;
56  }
58  Vec2& operator-=(Vec2 const& other)
59  {
60  Base::x -= other.x;
61  Base::y -= other.y;
62  return *this;
63  }
65  Vec2& operator*=(T value)
66  {
67  Base::x *= value;
68  Base::y *= value;
69  return *this;
70  }
72  Vec2& operator/=(T value)
73  {
74  Base::x /= value;
75  Base::y /= value;
76  return *this;
77  }
78 
80  constexpr Vec2 operator+(Vec2 const& other) const { return Vec2{Base::x, Base::y} += other; }
82  constexpr Vec2 operator-(Vec2 const& other) const { return Vec2{Base::x, Base::y} -= other; }
84  constexpr Vec2 operator*(T value) const { return Vec2{Base::x, Base::y} *= value; }
86  constexpr Vec2 operator/(T value) const { return Vec2{Base::x, Base::y} /= value; }
87 
89  constexpr bool operator==(Vec2 const& other) const
90  {
91  return (Base::x == other.x && Base::y == other.y);
92  }
94  constexpr bool operator!=(Vec2 const& other) const { return !(*this == other); }
95 
97  friend constexpr Vec2 operator*(T lhs, Vec2 const& rhs) { return rhs * lhs; }
98 
101  Vec2 clamped(SDL_Rect const& box) const
102  {
103  auto r = Vec2{Base::x, Base::y};
104  r.clamp(box);
105  return r;
106  }
107 
109  void clamp(SDL_Rect const& box)
110  {
111  Base::x = clamp(Base::x, box.x, box.x + box.w);
112  Base::y = clamp(Base::y, box.y, box.y + box.h);
113  }
114 
116  T length() const { return std::sqrt(Base::x * Base::x + Base::y * Base::y); }
118  T sqlength() const { return Base::x * Base::x + Base::y * Base::y; }
119 
121  bool is_null() const { return Base::x == T(0.0L) || Base::y == T(0.0L); }
122 
124  Vec2 normalized() const
125  {
126  auto r = Vec2{Base::x, Base::y};
127  r.normalize();
128  return r;
129  }
130 
132  void normalize()
133  {
134  if (is_null()) return;
135 
136  const auto l = length();
137  Base::x /= l;
138  Base::y /= l;
139  }
140 
142  template<typename U>
143  explicit operator Vec2<U>() const
144  {
145  return Vec2<U>{static_cast<U>(Base::x), static_cast<U>(Base::y)};
146  }
147 
149  friend std::ostream& operator<<(std::ostream& stream, Vec2 const& v)
150  {
151  return stream << "(x:" << v.x << ",y:" << v.y << ")";
152  }
153 
154 private:
156  T clamp(T x, T a, T b) { return std::min(std::max(Base::x, a), b); }
157 };
158 
165 
166 } // namespace sdl
T sqlength() const
Get squared lenght of this vector.
Definition: vec2.hpp:118
constexpr Vec2(SDL_Point const &p)
Convert SDL_Point to sdl::Vec2.
Definition: vec2.hpp:32
friend std::ostream & operator<<(std::ostream &stream, Vec2 const &v)
Print that vector to stream.
Definition: vec2.hpp:149
Vec2 clamped(SDL_Rect const &box) const
Clamp vector inside a box rectangle were to clamp vector.
Definition: vec2.hpp:101
T clamp(T x, T a, T b)
Actual implementation of clamp function.
Definition: vec2.hpp:156
constexpr Vec2 operator-(Vec2 const &other) const
Substract vectors together.
Definition: vec2.hpp:82
constexpr Vec2()
ctor, that will initialize vector to null vector
Definition: vec2.hpp:28
base content of a vector 2
Definition: vec2.hpp:13
Generic templated 2D vector class.
Definition: vec2.hpp:24
constexpr Vec2(T x, T y)
ctor that will initialize it to the current value
Definition: vec2.hpp:30
static constexpr Vec2 from_polar(A alpha, T radius)
Convert a vector in polar cordinates to cartesian in the 2D plan.
Definition: vec2.hpp:39
void clamp(SDL_Rect const &box)
Clamp vector inside a box rectangle were to clamp vector.
Definition: vec2.hpp:109
friend constexpr Vec2 operator*(T lhs, Vec2 const &rhs)
Multiply operator that works on the other side.
Definition: vec2.hpp:97
Vec2 & operator*=(T value)
Multiply vector to this one (x1*x2, y1*y2)
Definition: vec2.hpp:65
Vec2 & operator-=(Vec2 const &other)
Substract vector to this one.
Definition: vec2.hpp:58
constexpr Vec2 operator/(T value) const
Divide vectors together.
Definition: vec2.hpp:86
constexpr bool operator!=(Vec2 const &other) const
Compare vectors, false if they are the same.
Definition: vec2.hpp:94
Define to deactivate exception support.
Definition: color.hpp:7
Vec2 normalized() const
Return normalized copy of this vector.
Definition: vec2.hpp:124
constexpr Vec2 operator-() const
Return the opposite vector.
Definition: vec2.hpp:48
bool is_null() const
Return true if this vector is null.
Definition: vec2.hpp:121
constexpr Vec2 operator+(Vec2 const &other) const
Add vectors together.
Definition: vec2.hpp:80
allocator< U > a
Definition: simd.hpp:144
Vec2 & operator/=(T value)
Divide vector to this one (x1/x2, y1/y2)
Definition: vec2.hpp:72
void normalize()
Normalize this vector.
Definition: vec2.hpp:132
T length() const
Get lenghts of this vector.
Definition: vec2.hpp:116
Contains implementation details.
Definition: vec2.hpp:9
constexpr Vec2 operator*(T value) const
Multiply vectors together.
Definition: vec2.hpp:84
Vec2 & operator+=(Vec2 const &other)
Add vector to this one.
Definition: vec2.hpp:51
constexpr bool operator==(Vec2 const &other) const
Compare vectors, true if they are the same.
Definition: vec2.hpp:89