From a7318de4a5f24d4fff4a4528ebeec9af8055f65f Mon Sep 17 00:00:00 2001 From: "c.lamboo" Date: Thu, 9 Nov 2023 16:07:35 +0100 Subject: [PATCH] Add `intersection` utility CURA-11293 --- UM/Math/Polygon.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/UM/Math/Polygon.py b/UM/Math/Polygon.py index bfaa96dce..634f54584 100644 --- a/UM/Math/Polygon.py +++ b/UM/Math/Polygon.py @@ -184,16 +184,21 @@ def intersectionConvexHulls(self, other: "Polygon") -> "Polygon": :param other: The other polygon to intersect convex hulls with. :return: The intersection of the two polygons' convex hulls. """ - me = self.getConvexHull() - him = other.getConvexHull() + return self.getConvexHull().intersection(other.getConvexHull()) - # If either polygon has no surface area, then the intersection is empty. - if len(me.getPoints()) <= 2 or len(him.getPoints()) <= 2: + def intersection(self, other: "Polygon") -> "Polygon": + """Computes the intersection of this and another polygon. + + :param other: The other polygon to intersect with. + :return: The intersection of the two polygons. + """ + + if len(self.getPoints()) <= 2 or len(other.getPoints()) <= 2: return Polygon() clipper = pyclipper.Pyclipper() - clipper.AddPath(me._clipperPoints(), pyclipper.PT_SUBJECT, closed = True) - clipper.AddPath(him._clipperPoints(), pyclipper.PT_CLIP, closed=True) + clipper.AddPath(self._clipperPoints(), pyclipper.PT_SUBJECT, closed=True) + clipper.AddPath(other._clipperPoints(), pyclipper.PT_CLIP, closed=True) points = clipper.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO) if len(points) == 0: