2021-07-18 18:26:12 +02:00
|
|
|
#ifndef BWIDGETS_MATH_HPP
|
|
|
|
#define BWIDGETS_MATH_HPP
|
2021-07-10 19:55:53 +02:00
|
|
|
|
2021-07-29 15:31:39 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2021-07-10 19:55:53 +02:00
|
|
|
#include <SDL2/SDL_rect.h>
|
|
|
|
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/type/size.hpp>
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-07-18 18:26:12 +02:00
|
|
|
namespace bwidgets::core
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-07-30 20:41:47 +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;
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +02:00
|
|
|
static inline auto distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
|
|
|
|
-> float
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-07-30 20:41:47 +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
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +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));
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +02:00
|
|
|
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};
|
|
|
|
|
2021-07-30 20:41:47 +02:00
|
|
|
return (SDL_PointInRect(&top_left, &outer) == SDL_TRUE)
|
|
|
|
&& (SDL_PointInRect(&bottom_right, &outer) == SDL_TRUE);
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +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
|
|
|
{
|
2021-07-29 16:06:03 +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
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +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
|
|
|
{
|
2021-07-29 16:06:03 +02:00
|
|
|
return {r.x + offset.x, r.y + offset.y, r.w, r.h};
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|