bwidgets/inc/basic_widgets/core/math.hpp

56 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 <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 int center_rect(int container_size, int rect_size) noexcept
2021-07-10 19:55:53 +02:00
{
return (container_size - rect_size) / 2;
}
static inline float distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
2021-07-10 19:55:53 +02:00
{
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
static inline float distance(const SDL_Point& a, const SDL_Point& b) noexcept
2021-07-10 19:55:53 +02:00
{
return std::sqrt(distance_sqrd(a, b));
}
static inline bool rect_in_rect(const SDL_Rect outer, const SDL_Rect inner) noexcept
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_PointInRect(&bottom_right, &outer);
}
static inline SDL_Rect rect_margin(const SDL_Rect& r, const Size& margin) noexcept
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
};
}
static inline SDL_Rect rect_offset(const SDL_Rect& r, const SDL_Rect& offset) noexcept
2021-07-10 19:55:53 +02:00
{
return {
r.x + offset.x,
r.y + offset.y,
r.w, r.h
};
}
}
#endif