Clipping

Clipping algorithms are used to implement ∪, ∩ and setdiff for Polygon and MultiPolygon.

Meshes.clip — Function
clip(subject, other, method)

Clip the subject polygon with other polygon using clipping method.

source

Sutherland-Hodgman

# polygon to clip
outer = Ring((8, 0), (4, 8), (2, 8), (-2, 0), (0, 0), (1, 2), (5, 2), (6, 0))
inner = Ring((4, 4), (2, 4), (3, 6))
poly = Pgon([outer, inner])

# clipping polygon
other = Quadrangle((0, 1), (3, 1), (3, 7), (0, 7))

# clipped polygon
clipped = clip(poly, other, SutherlandHodgmanClipping())

viz(poly)
viz!(other, color = "black", alpha = 0.2)
viz!(boundary(clipped), color = "red", segmentsize = 3)
Mke.current_figure()
Example block output

Greiner-Hormann

Meshes.GreinerHormannClipping — Type
GreinerHormannClipping(operation=:∩)

The Greiner-Hormann algorithm for clipping polygons, with the extension of Foster et al. for degenerate intersections. Optionally, specify the type of clipping operation to perform, which can be :∩, :∪, or :-.

References

Notes

In contrast to SutherlandHodgmanClipping, the clipping geometry can be non-convex, and vertices of one geometry can lie on edges of the other geometry.

source
# polygon to clip
outer = Ring((8, 0), (4, 8), (2, 8), (-2, 0), (0, 0), (1, 2), (5, 2), (6, 0))
inner = Ring((4, 4), (2, 4), (3, 6))
poly = Pgon([outer, inner])

# non-convex clipping polygon
other = Pgon((0, 1), (6, 1), (6, 7), (4, 7), (4, 3), (2, 3), (2, 7), (0, 7))

# clipped polygon
clipped = clip(poly, other, GreinerHormannClipping())

viz(poly)
viz!(other, color = "black", alpha = 0.2)
viz!(boundary(clipped), color = "red", segmentsize = 3)
Mke.current_figure()
Example block output