From d9e8c6ddbdee879f033d3e1932eed0fb0aed2b7a Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 23 Jun 2023 01:34:26 -0700 Subject: [PATCH] Add line to rectangle collision detection in geom2d header. --- Crawler/Version.h | 2 +- Crawler/olcUTIL_Geometry2D.h | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Crawler/Version.h b/Crawler/Version.h index 6fc5f74f..f0c82786 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -2,7 +2,7 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 0 -#define VERSION_BUILD 221 +#define VERSION_BUILD 231 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Crawler/olcUTIL_Geometry2D.h b/Crawler/olcUTIL_Geometry2D.h index 26622a8e..36f50962 100644 --- a/Crawler/olcUTIL_Geometry2D.h +++ b/Crawler/olcUTIL_Geometry2D.h @@ -594,11 +594,10 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const rect& r, const line& l) { - return contains(r, l.start) - || contains(r, l.end); - - // TODO: This method is no good, it cant detect lines whose start and end - // points are outside the rectangle + return overlaps(r.left(),l)|| + overlaps(r.top(),l)|| + overlaps(r.bottom(),l)|| + overlaps(r.right(),l); } // Check if circle overlaps line segment @@ -649,8 +648,16 @@ namespace olc::utils::geom2d template inline std::vector> intersects(const rect& r, const line& l) { - // TODO: - return {}; + std::vector>intersections; + std::vector>result=intersects(r.left(),l); + if(result.size()>0)intersections.push_back(result[0]); + result=intersects(r.right(),l); + if(result.size()>0)intersections.push_back(result[0]); + result=intersects(r.top(),l); + if(result.size()>0)intersections.push_back(result[0]); + result=intersects(r.bottom(),l); + if(result.size()>0)intersections.push_back(result[0]); + return intersections; } // Get intersection points where circle intersects with line segment @@ -789,8 +796,7 @@ namespace olc::utils::geom2d template inline std::vector> intersects(const line& l, const rect& r) { - // TODO: - return {}; + return intersects(r,l); } // Get intersection points where rectangle intersects with rectangle