bwidgets/inc/basic_widgets/utils/math.hpp

48 lines
1.1 KiB
C++
Raw Normal View History

2021-07-10 19:55:53 +02:00
#ifndef MATH_HPP
#define MATH_HPP
#include <exception>
#include <SDL2/SDL_rect.h>
namespace bwidgets::utils::math
{
struct RectOverflow : std::exception {};
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_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