bwidgets/inc/basic_widgets/core/math.hpp

53 lines
1.4 KiB
C++
Raw Normal View History

#ifndef BWIDGETS_MATH_HPP
#define BWIDGETS_MATH_HPP
2021-07-10 19:55:53 +02:00
#include <cmath>
2021-07-10 19:55:53 +02:00
#include <SDL2/SDL_rect.h>
#include <basic_widgets/core/type/size.hpp>
2021-07-16 17:18:13 +02:00
namespace bwidgets::core
2021-07-10 19:55:53 +02:00
{
static inline auto center_rect(int container_size, int rect_size) noexcept -> int
2021-07-10 19:55:53 +02:00
{
return (container_size - rect_size) / 2;
}
static inline auto distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
-> float
2021-07-10 19:55:53 +02:00
{
return (a.x - b.x) * (a.x - b.x) // NOLINT(bugprone-narrowing-conversions)
+ (a.y - b.y) * (a.y - b.y);
2021-07-10 19:55:53 +02:00
}
static inline auto distance(const SDL_Point& a, const SDL_Point& b) noexcept -> float
2021-07-10 19:55:53 +02:00
{
return std::sqrt(distance_sqrd(a, b));
}
static inline auto rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
-> bool
2021-07-10 19:55:53 +02:00
{
SDL_Point top_left {inner.x, inner.y};
SDL_Point bottom_right {inner.x + inner.w, inner.y + inner.h};
return (SDL_PointInRect(&top_left, &outer) == SDL_TRUE)
&& (SDL_PointInRect(&bottom_right, &outer) == SDL_TRUE);
2021-07-10 19:55:53 +02:00
}
static inline auto rect_margin(const SDL_Rect& r, const Size& margin) noexcept
-> SDL_Rect
2021-07-16 17:18:13 +02:00
{
return {r.x + margin.w, r.y + margin.h, r.w - 2 * margin.w, r.h - 2 * margin.h};
2021-07-16 17:18:13 +02:00
}
static inline auto rect_offset(const SDL_Rect& r, const SDL_Rect& offset) noexcept
-> SDL_Rect
2021-07-10 19:55:53 +02:00
{
return {r.x + offset.x, r.y + offset.y, r.w, r.h};
2021-07-10 19:55:53 +02:00
}
}
#endif