From 4b4ab65f8687ac9067cc45a18656353399142043 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 12:04:42 +0000 Subject: [PATCH 01/26] Add CMake generated files and directories to gitignore --- .gitignore | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index cafd3c2..d1e21fc 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,19 @@ /Test_Geometry2D.vcxproj /Test_Geometry2D.sln build + +#CMake generated files and directories +/ALL_BUILD.vcxproj +/ALL_BUILD.vcxproj.filters +/ALL_BUILD.vcxproj.user +/CMakeCache.txt +/Test_Geometry2D.vcxproj.user +/ZERO_CHECK.vcxproj +/ZERO_CHECK.vcxproj.filters +/ZERO_CHECK.vcxproj.user +/cmake_install.cmake +/olcUTIL_Geometry2D.sln + +CMakeFiles/ +TEST_Geometry2D.dir/ +bin/ \ No newline at end of file From 517db1d190b7b8cd9f83fe88d387ef97141b69fc Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:21:52 +0000 Subject: [PATCH 02/26] Add (polygon, x), (x, polygon) overlaps, contains, intersects, closest and reflect function stubs --- olcUTIL_Geometry2D.h | 292 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 7e3be8a..db1482a 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2492,4 +2492,296 @@ namespace olc::utils::geom2d return internal::filter_duplicate_points(intersections); } + + + // ================================================================================================================ + // POLYGON ======================================================================================================== + + // overlaps(poly,poly) + // Checks if polygon overlaps polygon + template + inline constexpr bool overlaps(const polygon& poly1, const polygon& poly2) + { + return false; + } + + // overlaps(poly,p) + // Checks if polygon overlaps point + template + inline constexpr bool overlaps(const polygon& poly, const v_2d& p) + { + return false; + } + + // overlaps(poly,l) + // Checks if polygon overlaps line segment + template + inline constexpr bool overlaps(const polygon& poly, const line& l) + { + return false; + } + + // overlaps(poly,r) + // Checks if polygon overlaps rect + template + inline constexpr bool overlaps(const polygon& poly, const rect& r) + { + return false; + } + + // overlaps(poly,c) + // Checks if polygon overlaps circle + template + inline constexpr bool overlaps(const polygon& poly, const circle& c) + { + return false; + } + + // overlaps(poly,t) + // Checks if polygon overlaps triangle + template + inline constexpr bool overlaps(const polygon& poly, const triangle& t) + { + return false; + } + + // overlaps(p,poly) + // Checks if point overlaps polygon + template + inline constexpr bool overlaps(const v_2d& p, const polygon& poly) + { + return false; + } + + // overlaps(l,poly) + // Checks if line segment overlaps polygon + template + inline constexpr bool overlaps(const line& l, const polygon& poly) + { + return false; + } + + // overlaps(r,poly) + // Checks if rect overlaps polygon + template + inline constexpr bool overlaps(const rect& r, const polygon& poly) + { + return false; + } + + // overlaps(c,poly) + // Checks if circle overlaps polygon + template + inline constexpr bool overlaps(const circle& c, const polygon& poly) + { + return false; + } + + // overlaps(t,poly) + // Checks if triangle overlaps polygon + template + inline constexpr bool overlaps(const triangle& t, const polygon& poly) + { + return false; + } + + // contains(poly,poly) + // Checks if polygon contains polygon + template + inline constexpr bool contains(const polygon& poly1, const polygon& poly2) + { + return false; + } + + // contains(poly,p) + // Checks if polygon contains point + template + inline constexpr bool contains(const polygon& poly, const v_2d& p) + { + return false; + } + + // contains(poly,l) + // Checks if polygon contains line segment + template + inline constexpr bool contains(const polygon& poly, const line& l) + { + return false; + } + + // contains(poly,r) + // Checks if polygon contains rect + template + inline constexpr bool contains(const polygon& poly, const rect& r) + { + return false; + } + + // contains(poly,c) + // Checks if polygon contains circle + template + inline constexpr bool contains(const polygon& poly, const circle& c) + { + return false; + } + + // contains(poly,t) + // Checks if polygon contains triangle + template + inline constexpr bool contains(const polygon& poly, const triangle& t) + { + return false; + } + + // contains(p,poly) + // Checks if point contains polygon + template + inline constexpr bool contains(const v_2d& p, const polygon& poly) + { + return false; + } + + // contains(l,poly) + // Checks if line segment contains polygon + template + inline constexpr bool contains(const line& l, const polygon& poly) + { + return false; + } + + // contains(r,poly) + // Checks if rect contains polygon + template + inline constexpr bool contains(const rect& r, const polygon& poly) + { + return false; + } + + // contains(c,poly) + // Checks if circle contains polygon + template + inline constexpr bool contains(const circle& c, const polygon& poly) + { + return false; + } + + // contains(t,poly) + // Checks if triangle contains polygon + template + inline constexpr bool contains(const triangle& t, const polygon& poly) + { + return false; + } + + // intersects(poly,poly) + // Get intersection points where polygon intersects with polygon + template + inline std::vector> intersects(const polygon& poly1, const polygon& poly2) + { + return {}; + } + + // intersects(poly,p) + // Get intersection points where polygon intersects with point + template + inline std::vector> intersects(const polygon& poly, const v_2d& p) + { + return {}; + } + + // intersects(poly,l) + // Get intersection points where polygon intersects with line segment + template + inline std::vector> intersects(const polygon& poly, const line& l) + { + return {}; + } + + // intersects(poly,r) + // Get intersection points where polygon intersects with rect + template + inline std::vector> intersects(const polygon& poly, const rect& r) + { + return {}; + } + + // intersects(poly,c) + // Get intersection points where polygon intersects with circle + template + inline std::vector> intersects(const polygon& poly, const circle& c) + { + return {}; + } + + // intersects(poly,t) + // Get intersection points where polygon intersects with triangle + template + inline std::vector> intersects(const polygon& poly, const triangle& t) + { + return {}; + } + + // intersects(p,poly) + // Get intersection points where point intersects with polygon + template + inline std::vector> intersects(const v_2d& p, const polygon& poly) + { + return {}; + } + + // intersects(l,poly) + // Get intersection points where line segment intersects with polygon + template + inline std::vector> intersects(const line& l, const polygon& poly) + { + return {}; + } + + // intersects(r,poly) + // Get intersection points where rect intersects with polygon + template + inline std::vector> intersects(const rect& r, const polygon& poly) + { + return {}; + } + + // intersects(c,poly) + // Get intersection points where circle intersects with polygon + template + inline std::vector> intersects(const circle& c, const polygon& poly) + { + return {}; + } + + // intersects(t,poly) + // Get intersection points where triangle intersects with polygon + template + inline std::vector> intersects(const triangle& t, const polygon& poly) + { + return {}; + } + + // intersects(q,poly) + // Get intersection points where ray intersects with polygon + template + inline std::vector> intersects(const ray& q, const polygon& poly) + { + return {}; + } + + // reflect(q,poly) + // Optionally returns a ray reflected off a polygon if collision occurs + template + inline std::optional> reflect(const ray& q, const polygon& poly) + { + return {}; + } + + // closest(poly,p) + // Returns closest point on polygon to point + template + inline olc::v_2d closest(const polygon& poly, const olc::v_2d& p) + { + return {}; + } } From 28f3553332584099c9e9cc60beffb8f5bbf3c75b Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:24:13 +0000 Subject: [PATCH 03/26] Add (polygon, point) contains function --- olcUTIL_Geometry2D.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index db1482a..915f471 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2598,7 +2598,17 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const v_2d& p) { - return false; + for(uint32_t i = 0; i < poly.pos.size(); ++i) + { + uint32_t next = (i + 1) % poly.pos.size(); + olc::v_2d edge = poly.pos[next] - poly.pos[i]; + olc::v_2d normal = -edge.perp(); + + if(normal.dot(p - poly.pos[i]) > 0) + return false; + } + + return true; } // contains(poly,l) From f487f182a2db7ced2260445ad09d3a2bb1bb7ff5 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:42:31 +0000 Subject: [PATCH 04/26] Add (polygon, polygon) overlaps function --- olcUTIL_Geometry2D.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 915f471..8f72327 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -540,6 +540,29 @@ namespace olc::utils::geom2d return filtered_points; } + + template + bool overlaps(const std::vector>& a, const std::vector>& b) + { + for(uint32_t i = 0; i < a.size(); ++i) + { + uint32_t next = (i + 1) % a.size(); + olc::v_2d edge = a[next] - a[i]; + olc::v_2d normal = -edge.perp(); + T minProj = std::numeric_limits::max(); + + for(uint32_t j = 0; j < b.size(); ++j) + { + T proj = normal.dot(b[j] - a[i]); + minProj = std::min(minProj, proj); + } + + if(minProj >= 0) + return false; + } + + return true; + } }; //https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c @@ -2502,7 +2525,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly1, const polygon& poly2) { - return false; + return internal::overlaps(poly1.pos, poly2.pos) && internal::overlaps(poly2.pos, poly1.pos); } // overlaps(poly,p) From 804a2d327a67e472492a9fd642a641e73deeda37 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:43:45 +0000 Subject: [PATCH 05/26] Add (polygon, point) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 8f72327..1c756bf 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2533,7 +2533,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly, const v_2d& p) { - return false; + return contains(poly, p); } // overlaps(poly,l) From 723ebc29c6b2dca13d704ef3841836ca217ba170 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:45:56 +0000 Subject: [PATCH 06/26] Add Polygon type to ShapeWrap variant --- TEST_Geometry2D.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/TEST_Geometry2D.cpp b/TEST_Geometry2D.cpp index fbdb6ee..dd384d4 100644 --- a/TEST_Geometry2D.cpp +++ b/TEST_Geometry2D.cpp @@ -86,6 +86,11 @@ class Test_Geometry2D : public olc::PixelGameEngine olc::vf2d points[2]; // origin, direction }; + struct Polygon + { + std::vector points; // vertices + }; + // Create desired shapes using a sequence of points static auto make_internal(const Point& p) { return p.points[0]; } static auto make_internal(const Line& p) { return line{ p.points[0], p.points[1] }; } @@ -93,9 +98,10 @@ class Test_Geometry2D : public olc::PixelGameEngine static auto make_internal(const Circle& p) { return circle{ p.points[0], (p.points[1]-p.points[0]).mag() }; } static auto make_internal(const Triangle& p) { return triangle{ p.points[0], p.points[1], p.points[2] }; } static auto make_internal(const Ray& p) { return ray{ p.points[0], (p.points[1]-p.points[0]).norm() }; } + static auto make_internal(const Polygon& p) { return polygon{ p.points }; } // The clever bit (and a bit new to me - jx9) - using ShapeWrap = std::variant; + using ShapeWrap = std::variant; From 9a37908d8d728a8ad42bdbf82d35693ffddabe43 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:47:41 +0000 Subject: [PATCH 07/26] Add draw_internal function for Polygon type --- TEST_Geometry2D.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/TEST_Geometry2D.cpp b/TEST_Geometry2D.cpp index dd384d4..4f8a542 100644 --- a/TEST_Geometry2D.cpp +++ b/TEST_Geometry2D.cpp @@ -222,6 +222,17 @@ class Test_Geometry2D : public olc::PixelGameEngine DrawLine(t.origin, t.origin+t.direction * 1000.0f, col, 0xF0F0F0F0); } + void draw_internal(const Polygon& x, const olc::Pixel col) + { + const auto t = make_internal(x); + + for(uint32_t i = 0; i < t.pos.size(); ++i) + { + uint32_t next = (i + 1) % t.pos.size(); + DrawLine(t.pos[i], t.pos[next], col); + } + } + void DrawShape(const ShapeWrap& shape, const olc::Pixel col = olc::WHITE) { std::visit([&](const auto& x) From 1fa11821a3a22a7b4cff16322fba1e64c9ae1ae5 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 14:51:01 +0000 Subject: [PATCH 08/26] Add regular convex Polygons to ShapeWrap vector --- TEST_Geometry2D.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/TEST_Geometry2D.cpp b/TEST_Geometry2D.cpp index 4f8a542..30b4a5b 100644 --- a/TEST_Geometry2D.cpp +++ b/TEST_Geometry2D.cpp @@ -263,6 +263,9 @@ class Test_Geometry2D : public olc::PixelGameEngine vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {10.0f, 150.0f}, {90.0f, 150.0f}} }}); vecShapes.push_back({ Triangle{{ {350.0f, 200.0f}, {500.0f, 150.0f}, {450.0f, 400.0f}} }}); + vecShapes.push_back({create_regular_convex_polygon(5, 50, {150, 150})}); + vecShapes.push_back({create_regular_convex_polygon(8, 80, {300, 150})}); + return true; } @@ -447,6 +450,20 @@ class Test_Geometry2D : public olc::PixelGameEngine return true; } + + Polygon create_regular_convex_polygon(uint32_t point_count, float scale = 1, const olc::vf2d& translation = {}) + { + Polygon polygon; + float angle = olc::utils::geom2d::pi * 2 / (float)point_count; + + for(uint32_t i = 0; i < point_count; ++i) + { + olc::vf2d point = olc::vf2d{std::cos(angle * i), std::sin(angle * i)} * scale + translation; + polygon.points.push_back(point); + } + + return polygon; + } }; int main() From 210cb46d7a5f52f2912bdfa5403ec8d4bcebf37c Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 15:28:33 +0000 Subject: [PATCH 09/26] Add (polygon, triangle) overlaps function --- TEST_Geometry2D.cpp | 3 ++- olcUTIL_Geometry2D.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/TEST_Geometry2D.cpp b/TEST_Geometry2D.cpp index 30b4a5b..5beb4ab 100644 --- a/TEST_Geometry2D.cpp +++ b/TEST_Geometry2D.cpp @@ -260,7 +260,8 @@ class Test_Geometry2D : public olc::PixelGameEngine vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {420.0f, 300.0f} } } }); vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {400.0f, 300.0f} } } }); - vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {10.0f, 150.0f}, {90.0f, 150.0f}} }}); + // Potential merge conflict, changed winding of triangle vertices from counter clockwise to clockwise + vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {90.0f, 150.0f}, {10.0f, 150.0f}} }}); vecShapes.push_back({ Triangle{{ {350.0f, 200.0f}, {500.0f, 150.0f}, {450.0f, 400.0f}} }}); vecShapes.push_back({create_regular_convex_polygon(5, 50, {150, 150})}); diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 1c756bf..54a80c7 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2565,7 +2565,8 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly, const triangle& t) { - return false; + std::vector> triVerts{t.pos.begin(), t.pos.end()}; + return internal::overlaps(poly.pos, triVerts) && internal::overlaps(triVerts, poly.pos); } // overlaps(p,poly) From 438ab8512ac39c362fcfa27c3d40df67e4f06f67 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 15:31:24 +0000 Subject: [PATCH 10/26] Add (triangle, polygon) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 54a80c7..c331e91 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2606,7 +2606,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const triangle& t, const polygon& poly) { - return false; + return overlaps(poly, t); } // contains(poly,poly) From c641ea840d85a93daa1400db87ea0b6445e642b7 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 15:45:22 +0000 Subject: [PATCH 11/26] Add (point, polygon) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index c331e91..40721cc 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2574,7 +2574,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const v_2d& p, const polygon& poly) { - return false; + return contains(poly, p); } // overlaps(l,poly) From c441de4a399edc5c880fa426fdad9b4b08944732 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 16:12:04 +0000 Subject: [PATCH 12/26] Add (polygon, rect) overlaps function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 40721cc..3d04762 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2549,7 +2549,8 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly, const rect& r) { - return false; + std::vector> rectVerts{r.pos, {r.pos.x + r.size.x, r.pos.y}, r.pos + r.size, {r.pos.x, r.pos.y + r.size.y}}; + return internal::overlaps(poly.pos, rectVerts) && internal::overlaps(rectVerts, poly.pos); } // overlaps(poly,c) From 08ff5d81020b080b6be6bf26075ee0a4ce3246d7 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 16:16:58 +0000 Subject: [PATCH 13/26] Add (rect, polygon) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 3d04762..59beaf6 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2591,7 +2591,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const rect& r, const polygon& poly) { - return false; + return overlaps(poly, r); } // overlaps(c,poly) From bb12a499cdd6c3569dd6e95da78f341ba9d48ca0 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:19:46 +0000 Subject: [PATCH 14/26] Add (polygon, polygon) contains function --- olcUTIL_Geometry2D.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 59beaf6..340c4ee 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -563,6 +563,25 @@ namespace olc::utils::geom2d return true; } + + template + bool contains(const std::vector>& a, const std::vector>& b) + { + for(uint32_t i = 0; i < a.size(); ++i) + { + uint32_t next = (i + 1) % a.size(); + olc::v_2d edge = a[next] - a[i]; + olc::v_2d normal = -edge.perp(); + + for(uint32_t j = 0; j < b.size(); ++j) + { + if(normal.dot(b[j] - a[i]) > 0) + return false; + } + } + + return true; + } }; //https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c @@ -2615,7 +2634,7 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly1, const polygon& poly2) { - return false; + return internal::contains(poly1.pos, poly2.pos); } // contains(poly,p) From b534e9b066eb9a2c0c20b859851ace169fad54c0 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:26:35 +0000 Subject: [PATCH 15/26] Add (polygon, line) contains function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 340c4ee..c368c04 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2660,7 +2660,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const line& l) { - return false; + std::vector> lineVerts{l.start, l.end}; + return internal::contains(poly.pos, lineVerts); } // contains(poly,r) From 3eed4cb8b28a11bf223cc491f2d492a19937e517 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:31:43 +0000 Subject: [PATCH 16/26] Add (polygon, rect) contains function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index c368c04..2f6f44c 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2669,7 +2669,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const rect& r) { - return false; + std::vector> rectVerts{r.pos, {r.pos.x + r.size.x, r.pos.y}, r.pos + r.size, {r.pos.x, r.pos.y + r.size.y}}; + return internal::contains(poly.pos, rectVerts); } // contains(poly,c) From d364c81b1840d1144fadc1e5f6b8f1cb6c3083a7 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:36:26 +0000 Subject: [PATCH 17/26] Add (polygon, triangle) contains function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 2f6f44c..874b8e4 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2686,7 +2686,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const triangle& t) { - return false; + std::vector> triVerts{t.pos.begin(), t.pos.end()}; + return internal::contains(poly.pos, triVerts); } // contains(p,poly) From a0d18ed5b3bd76de73ad09f79012f91f29c7a66b Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:43:17 +0000 Subject: [PATCH 18/26] Add (rect, polygon) contains function --- TEST_Geometry2D.cpp | 2 +- olcUTIL_Geometry2D.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/TEST_Geometry2D.cpp b/TEST_Geometry2D.cpp index 5beb4ab..91efc87 100644 --- a/TEST_Geometry2D.cpp +++ b/TEST_Geometry2D.cpp @@ -255,12 +255,12 @@ class Test_Geometry2D : public olc::PixelGameEngine vecShapes.push_back({ Line{ { { 80.0f, 10.0f }, {10.0f, 20.0f} } } }); vecShapes.push_back({ Rect{ { { 80.0f, 10.0f }, {110.0f, 60.0f} } } }); + vecShapes.push_back({ Rect{ { { 80.0f, 10.0f }, {200.0f, 150.0f} } }}); vecShapes.push_back({ Circle{ { { 130.0f, 20.0f }, {170.0f, 20.0f} } } }); vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {420.0f, 300.0f} } } }); vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {400.0f, 300.0f} } } }); - // Potential merge conflict, changed winding of triangle vertices from counter clockwise to clockwise vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {90.0f, 150.0f}, {10.0f, 150.0f}} }}); vecShapes.push_back({ Triangle{{ {350.0f, 200.0f}, {500.0f, 150.0f}, {450.0f, 400.0f}} }}); diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 874b8e4..20d575a 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2711,7 +2711,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const rect& r, const polygon& poly) { - return false; + std::vector> rectVerts{r.pos, {r.pos.x + r.size.x, r.pos.y}, r.pos + r.size, {r.pos.x, r.pos.y + r.size.y}}; + return internal::contains(rectVerts, poly.pos); } // contains(c,poly) From fb8d0b80cd11ab85fb5141de967869af1395b708 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:46:39 +0000 Subject: [PATCH 19/26] Add (triangle, polygon) contains function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 20d575a..0a5612a 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2728,7 +2728,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const triangle& t, const polygon& poly) { - return false; + std::vector> triVerts{t.pos.begin(), t.pos.end()}; + return internal::contains(triVerts, poly.pos); } // intersects(poly,poly) From c5f7e710e341629eff631c21d2610e11aaeb6819 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 19:55:49 +0000 Subject: [PATCH 20/26] Refactor (polygon, point) contains function --- olcUTIL_Geometry2D.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 0a5612a..aaa576f 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2642,17 +2642,8 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const v_2d& p) { - for(uint32_t i = 0; i < poly.pos.size(); ++i) - { - uint32_t next = (i + 1) % poly.pos.size(); - olc::v_2d edge = poly.pos[next] - poly.pos[i]; - olc::v_2d normal = -edge.perp(); - - if(normal.dot(p - poly.pos[i]) > 0) - return false; - } - - return true; + std::vector> point{p}; + return internal::contains(poly.pos, point); } // contains(poly,l) From 5e9ec1726567dba8febc3e2b1d3da92eaaabbe6c Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 23:18:04 +0000 Subject: [PATCH 21/26] Add (polygon, line) overlaps function --- olcUTIL_Geometry2D.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index aaa576f..29a7ba1 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2560,7 +2560,8 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly, const line& l) { - return false; + std::vector> lineVerts{l.start, l.end}; + return internal::overlaps(poly.pos, lineVerts) && internal::overlaps(lineVerts, poly.pos); } // overlaps(poly,r) From 3bf6cfa7e0dee65e79008b89e56d299e99ba024c Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Tue, 6 Feb 2024 23:21:34 +0000 Subject: [PATCH 22/26] Add (line, polygon) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 29a7ba1..4c3b90e 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2603,7 +2603,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const line& l, const polygon& poly) { - return false; + return overlaps(poly, l); } // overlaps(r,poly) From 54d3e100af084d1726e4a8a256ae9d6f3a0afb0c Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Sun, 18 Feb 2024 20:34:27 +0000 Subject: [PATCH 23/26] Add (polygon, circle) overlaps function --- olcUTIL_Geometry2D.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 4c3b90e..57d45fb 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2578,7 +2578,32 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const polygon& poly, const circle& c) { - return false; + T maxProj = std::numeric_limits::lowest(); + olc::v_2d start, edge; + + for(uint32_t i = 0; i < poly.pos.size(); ++i) + { + uint32_t next = (i + 1) % poly.pos.size(); + olc::v_2d e = poly.pos[next] - poly.pos[i]; + olc::v_2d normal = -e.perp(); + T proj = normal.dot(c.pos - poly.pos[i]); + + if(proj > maxProj) + { + maxProj = proj; + start = poly.pos[i]; + edge = e; + } + } + + if(maxProj <= 0) + return true; + + T t = std::clamp(edge.dot(c.pos - start) / edge.mag2(), T(0), T(1)); + olc::v_2d closest = start + edge * t; + olc::v_2d separation = c.pos - closest; + + return separation.mag2() < c.radius * c.radius; } // overlaps(poly,t) From b3e122cc664af8a3546f2ddbf99f2e3bcc46e551 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Sun, 18 Feb 2024 20:38:18 +0000 Subject: [PATCH 24/26] Add (circle, polygon) overlaps function --- olcUTIL_Geometry2D.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 57d45fb..72df334 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2644,7 +2644,7 @@ namespace olc::utils::geom2d template inline constexpr bool overlaps(const circle& c, const polygon& poly) { - return false; + return overlaps(poly, c); } // overlaps(t,poly) From 69d80ff470aab141babec1d4cf6e7c6278ee6b11 Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Sun, 18 Feb 2024 21:04:11 +0000 Subject: [PATCH 25/26] Add (polygon, circle) contains function --- olcUTIL_Geometry2D.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index 72df334..cc6ef86 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2695,7 +2695,26 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const polygon& poly, const circle& c) { - return false; + for(uint32_t i = 0; i < poly.pos.size(); ++i) + { + uint32_t next = (i + 1) % poly.pos.size(); + olc::v_2d edge = poly.pos[next] - poly.pos[i]; + olc::v_2d normal = -edge.perp(); + olc::v_2d diff = c.pos - poly.pos[i]; + T proj = normal.dot(diff); + + if(proj > 0) + return false; + + T t = edge.dot(diff) / edge.mag2(); + olc::v_2d closest = poly.pos[i] + edge * t; + olc::v_2d separation = c.pos - closest; + + if(separation.mag2() < c.radius * c.radius) + return false; + } + + return true; } // contains(poly,t) From 82066b4fc20d8ceffa18a9bc351443329e7eb2dc Mon Sep 17 00:00:00 2001 From: AlterEgoIV Date: Sun, 18 Feb 2024 21:10:13 +0000 Subject: [PATCH 26/26] Add (circle, polygon) contains function --- olcUTIL_Geometry2D.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/olcUTIL_Geometry2D.h b/olcUTIL_Geometry2D.h index cc6ef86..6481e0f 100644 --- a/olcUTIL_Geometry2D.h +++ b/olcUTIL_Geometry2D.h @@ -2756,7 +2756,13 @@ namespace olc::utils::geom2d template inline constexpr bool contains(const circle& c, const polygon& poly) { - return false; + for(uint32_t i = 0; i < poly.pos.size(); ++i) + { + if((poly.pos[i] - c.pos).mag2() > c.radius * c.radius) + return false; + } + + return true; } // contains(t,poly)