From d43b4667d56054a130c18397db41f02b847073d4 Mon Sep 17 00:00:00 2001
From: Garth Minette <gminette@gmail.com>
Date: Sun, 29 Nov 2020 22:39:42 -0800
Subject: [PATCH] Fix for #241: split_polygons_at_each_y produces non simple
 polygons

---
 geometry.scad | 27 +++++++++++++++------------
 version.scad  |  2 +-
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/geometry.scad b/geometry.scad
index b18456f..62290a5 100644
--- a/geometry.scad
+++ b/geometry.scad
@@ -2005,10 +2005,11 @@ function _split_polygon_at_x(poly, x) =
         ],
         out1 = [for (p = poly2) if(p.x <= x) p],
         out2 = [for (p = poly2) if(p.x >= x) p],
-        out = [
-            if (len(out1)>=3) out1,
-            if (len(out2)>=3) out2,
-        ]
+        out3 = [
+            if (len(out1)>=3) each split_path_at_self_crossings(out1),
+            if (len(out2)>=3) each split_path_at_self_crossings(out2),
+        ],
+        out = [for (p=out3) if (len(p) > 2) cleanup_path(p)]
     ) out;
 
 
@@ -2034,10 +2035,11 @@ function _split_polygon_at_y(poly, y) =
         ],
         out1 = [for (p = poly2) if(p.y <= y) p],
         out2 = [for (p = poly2) if(p.y >= y) p],
-        out = [
-            if (len(out1)>=3) out1,
-            if (len(out2)>=3) out2,
-        ]
+        out3 = [
+            if (len(out1)>=3) each split_path_at_self_crossings(out1),
+            if (len(out2)>=3) each split_path_at_self_crossings(out2),
+        ],
+        out = [for (p=out3) if (len(p) > 2) cleanup_path(p)]
     ) out;
 
 
@@ -2063,10 +2065,11 @@ function _split_polygon_at_z(poly, z) =
         ],
         out1 = [for (p = poly2) if(p.z <= z) p],
         out2 = [for (p = poly2) if(p.z >= z) p],
-        out = [
-            if (len(out1)>=3) out1,
-            if (len(out2)>=3) out2,
-        ]
+        out3 = [
+            if (len(out1)>=3) each split_path_at_self_crossings(close_path(out1), closed=false),
+            if (len(out2)>=3) each split_path_at_self_crossings(close_path(out2), closed=false),
+        ],
+        out = [for (p=out3) if (len(p) > 2) cleanup_path(p)]
     ) out;
 
 
diff --git a/version.scad b/version.scad
index 12852f3..be986f3 100644
--- a/version.scad
+++ b/version.scad
@@ -8,7 +8,7 @@
 //////////////////////////////////////////////////////////////////////
 
 
-BOSL_VERSION = [2,0,471];
+BOSL_VERSION = [2,0,472];
 
 
 // Section: BOSL Library Version Functions