From c29acc74cc4632f89ed476604a649ae25910046b Mon Sep 17 00:00:00 2001 From: Javidx9 <25419386+OneLoneCoder@users.noreply.github.com> Date: Tue, 20 Sep 2022 22:18:49 +0100 Subject: [PATCH] Added some more Geom2D functions, only 32 to go... --- utilities/olcUTIL_Geometry2D.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/utilities/olcUTIL_Geometry2D.h b/utilities/olcUTIL_Geometry2D.h index 0400598..cd13a29 100644 --- a/utilities/olcUTIL_Geometry2D.h +++ b/utilities/olcUTIL_Geometry2D.h @@ -496,8 +496,7 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const circle& c1, const line& l) { - // TODO: - return false; + return contains(c1, l.start) && contains(c1, l.end); } // Check if triangle contains line segment @@ -637,8 +636,10 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const circle& c, const rect& r) { - // TODO: - return false; + return contains(c, r.pos) + && contains(c, olc::v2d_generic{ r.pos.x + r.size.x, r.pos.y }) + && contains(c, olc::v2d_generic{ r.pos.x, r.pos.y + r.size.y }) + && contains(c, r.pos + r.size); } // Check if triangle contains rectangle @@ -680,8 +681,12 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const circle& c, const rect& r) { - // TODO: - return false; + // Inspired by this (very clever btw) + // https://stackoverflow.com/questions/45370692/circle-rectangle-collision-response + // But modified to work :P + T2 overlap = (olc::v2d_generic{ std::clamp(c.pos.x, r.pos.x, r.pos.x + r.size.x), std::clamp(c.pos.y, r.pos.y, r.pos.y + r.size.y) } - c.pos).mag2(); + if (std::isnan(overlap)) overlap = T2(0); + return (overlap - (c.radius * c.radius)) < T2(0); } // Check if triangle overlaps rectangle