Skip to content

Commit

Permalink
Add intersection utility
Browse files Browse the repository at this point in the history
CURA-11293
  • Loading branch information
casperlamboo committed Nov 9, 2023
1 parent fb837a4 commit a7318de
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions UM/Math/Polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a7318de

Please sign in to comment.