23 template<
typename T,
class Base = details::Vec2Base<T>>
28 constexpr
Vec2() : Base{0, 0} {}
32 constexpr
Vec2(SDL_Point
const& p) : Base{p.x, p.y} {}
34 constexpr
Vec2(
Vec2 const&) noexcept =
default;
35 constexpr
Vec2(
Vec2&&) noexcept =
default;
41 return Vec2{radius * std::cos(alpha), radius * std::sin(alpha)};
44 Vec2& operator=(
Vec2 const&) noexcept =
default;
45 Vec2& operator=(
Vec2&&) noexcept =
default;
91 return (Base::x == other.x && Base::y == other.y);
94 constexpr
bool operator!=(
Vec2 const& other)
const {
return !(*
this == other); }
103 auto r =
Vec2{Base::x, Base::y};
111 Base::x = clamp(Base::x, box.x, box.x + box.w);
112 Base::y = clamp(Base::y, box.y, box.y + box.h);
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; }
121 bool is_null()
const {
return Base::x == T(0.0L) || Base::y == T(0.0L); }
126 auto r =
Vec2{Base::x, Base::y};
134 if (is_null())
return;
136 const auto l = length();
145 return Vec2<U>{
static_cast<U
>(Base::x), static_cast<U>(Base::y)};
151 return stream <<
"(x:" << v.x <<
",y:" << v.y <<
")";
156 T
clamp(T
x, T
a, T b) {
return std::min(std::max(Base::x, a), b); }
T sqlength() const
Get squared lenght of this vector.
constexpr Vec2(SDL_Point const &p)
Convert SDL_Point to sdl::Vec2.
friend std::ostream & operator<<(std::ostream &stream, Vec2 const &v)
Print that vector to stream.
Vec2 clamped(SDL_Rect const &box) const
Clamp vector inside a box rectangle were to clamp vector.
T clamp(T x, T a, T b)
Actual implementation of clamp function.
constexpr Vec2 operator-(Vec2 const &other) const
Substract vectors together.
constexpr Vec2()
ctor, that will initialize vector to null vector
base content of a vector 2
Generic templated 2D vector class.
constexpr Vec2(T x, T y)
ctor that will initialize it to the current value
static constexpr Vec2 from_polar(A alpha, T radius)
Convert a vector in polar cordinates to cartesian in the 2D plan.
void clamp(SDL_Rect const &box)
Clamp vector inside a box rectangle were to clamp vector.
friend constexpr Vec2 operator*(T lhs, Vec2 const &rhs)
Multiply operator that works on the other side.
Vec2 & operator*=(T value)
Multiply vector to this one (x1*x2, y1*y2)
Vec2 & operator-=(Vec2 const &other)
Substract vector to this one.
constexpr Vec2 operator/(T value) const
Divide vectors together.
constexpr bool operator!=(Vec2 const &other) const
Compare vectors, false if they are the same.
Define to deactivate exception support.
Vec2 normalized() const
Return normalized copy of this vector.
constexpr Vec2 operator-() const
Return the opposite vector.
bool is_null() const
Return true if this vector is null.
constexpr Vec2 operator+(Vec2 const &other) const
Add vectors together.
Vec2 & operator/=(T value)
Divide vector to this one (x1/x2, y1/y2)
void normalize()
Normalize this vector.
T length() const
Get lenghts of this vector.
Contains implementation details.
constexpr Vec2 operator*(T value) const
Multiply vectors together.
Vec2 & operator+=(Vec2 const &other)
Add vector to this one.
constexpr bool operator==(Vec2 const &other) const
Compare vectors, true if they are the same.