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-08-01 11:12:54 +02:00
|
|
|
#include <algorithm>
|
2021-07-29 15:31:39 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
2021-07-10 19:55:53 +02:00
|
|
|
#include <SDL2/SDL_rect.h>
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
#include <basic_widgets/core/type/color.hpp>
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/type/size.hpp>
|
2021-07-16 17:18:13 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
namespace bwidgets
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] inline auto center_line(int available_len, int used_len) noexcept -> int
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-08-01 11:12:54 +02:00
|
|
|
return (available_len - used_len) / 2;
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 16:37:02 +02:00
|
|
|
[[nodiscard]] inline auto distance_sqrd(SDL_Point a, SDL_Point b) noexcept
|
2021-07-30 20:41:47 +02:00
|
|
|
-> float
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
2021-08-19 15:00:44 +02:00
|
|
|
return float(a.x - b.x) * float(a.x - b.x)
|
|
|
|
+ float(a.y - b.y) * float(a.y - b.y);
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-14 16:37:02 +02:00
|
|
|
[[nodiscard]] inline auto distance(SDL_Point a, SDL_Point b) noexcept -> float
|
2021-07-10 19:55:53 +02:00
|
|
|
{
|
|
|
|
return std::sqrt(distance_sqrd(a, b));
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
template<FloatingPoint F>
|
2021-08-14 16:37:02 +02:00
|
|
|
[[nodiscard]] inline auto lerp(Color a, Color b, F x, bool op_alpha=false, bool op_color=true) noexcept -> Color
|
2021-08-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
return {{
|
|
|
|
op_color ? (uint8_t)std::lerp(a().r, b().r, x) : a().r,
|
|
|
|
op_color ? (uint8_t)std::lerp(a().g, b().g, x) : a().g,
|
|
|
|
op_color ? (uint8_t)std::lerp(a().b, b().b, x) : a().b,
|
|
|
|
op_alpha ? (uint8_t)std::lerp(a().a, b().a, x) : a().a,
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:12:54 +02:00
|
|
|
template<Numeric N>
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] inline auto linear(N x, N a, N b) noexcept -> float
|
2021-08-01 11:12:54 +02:00
|
|
|
{
|
|
|
|
return (float)(x - a) / (float)(b - a);
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] inline auto rect_in_rect(const SDL_Rect& outer, const SDL_Rect& inner) noexcept
|
2021-07-30 20:41:47 +02:00
|
|
|
-> 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-08-14 16:37:02 +02:00
|
|
|
[[nodiscard]] inline auto rect_margin(const SDL_Rect& r, Size margin) noexcept
|
2021-07-30 20:41:47 +02:00
|
|
|
-> 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-08-14 16:37:02 +02:00
|
|
|
[[nodiscard]] inline auto rect_offset(const SDL_Rect& r, SDL_Point offset) noexcept
|
2021-07-30 20:41:47 +02:00
|
|
|
-> 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
|
|
|
}
|
2021-08-01 11:12:54 +02:00
|
|
|
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] inline auto rect_offset(const SDL_Rect& r, const SDL_Rect& offset) noexcept
|
2021-08-01 11:12:54 +02:00
|
|
|
-> SDL_Rect
|
|
|
|
{
|
|
|
|
return rect_offset(r, SDL_Point {offset.x, offset.y});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Numeric N>
|
2021-08-14 09:17:51 +02:00
|
|
|
[[nodiscard]] inline auto smoothstep(N x, N a, N b) noexcept -> float
|
2021-08-01 11:12:54 +02:00
|
|
|
{
|
|
|
|
const float x_norm = linear(std::clamp<float>(x, a, b), a, b);
|
|
|
|
return 3 * x_norm * x_norm - 2 * x_norm * x_norm * x_norm;
|
|
|
|
}
|
2021-07-10 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|