geometry
polygon cutting
computational geometry
2D shapes
polygon generation

Generate new polygons from a cut polygon 2D

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Generating new polygons from a cut polygon in 2D means taking an existing polygon and a cutting line or segment, computing where they intersect, and constructing the resulting pieces as new valid polygons. The core difficulty is not drawing the cut. It is preserving correct topology: intersection points must be inserted in the right places, edges must be reconnected in the right order, and degenerate fragments must be discarded.

Define the Geometry Problem Precisely

Before choosing an algorithm, be explicit about the inputs:

  • is the source polygon simple or self-intersecting
  • can it contain holes
  • is the cutter an infinite line, a segment, or another polygon
  • do you want all pieces or only one side of the cut

For many practical applications, the source polygon is simple and the cut is a line segment. That is the easiest case to implement reliably.

The High-Level Algorithm

For a simple polygon cut by a segment, the usual steps are:

  1. compute all intersections between the cut and polygon edges
  2. insert the intersection points into both the polygon boundary and the cut line ordering
  3. split the boundary graph at those inserted points
  4. walk the resulting graph to build closed loops
  5. keep only valid polygon loops with non-zero area

Conceptually, this is a graph-building problem more than a drawing problem.

A Practical Approach with Shapely

If you are using Python, a geometry library is usually the fastest path to correct results. shapely.ops.split handles many polygon-splitting cases directly.

python
1from shapely.geometry import Polygon, LineString
2from shapely.ops import split
3
4polygon = Polygon([(0, 0), (6, 0), (6, 4), (0, 4)])
5cut = LineString([(3, -1), (3, 5)])
6
7parts = split(polygon, cut)
8
9for i, part in enumerate(parts.geoms, start=1):
10    print(f"Part {i}:", list(part.exterior.coords))

This is often the best engineering answer if you need robust polygon splitting rather than a teaching implementation from scratch.

What Happens Under the Hood

Even when a library performs the split for you, the underlying geometric logic still matters. The cutting line must actually intersect the polygon boundary in a way that separates the polygon into distinct regions. If the line only touches one vertex or runs along an edge, the result may be unchanged or ambiguous.

That is why robust polygon cutting requires careful handling of cases such as:

  • tangent contact at one point
  • cuts that overlap an existing edge
  • duplicate intersection points due to floating-point precision
  • cuts that enter and leave through the same narrow region

These details explain why library-based solutions are attractive: topology edge cases are harder than the happy path.

Building It Yourself with Segment Intersections

If you need a custom implementation, start by computing segment intersections between the cut and every polygon edge.

python
def edges(points):
    for i in range(len(points)):
        yield points[i], points[(i + 1) % len(points)]

From there, you would:

  • find the intersection points
  • sort them along the cut line
  • splice them into the polygon boundary order
  • build adjacency between split boundary pieces
  • trace closed loops

This is completely doable, but once holes, shared edges, or precision issues appear, the amount of bookkeeping grows quickly.

Validate the Resulting Pieces

After generating candidate polygons, validate them. A valid result should usually satisfy:

  • no self-intersections
  • closed outer ring
  • positive area above a small tolerance
  • vertices ordered consistently

Without a validation step, a nearly correct implementation can still emit sliver polygons or malformed rings when cuts pass near existing vertices.

Numerical Robustness Matters

Computational geometry is sensitive to floating-point comparisons. Points that should be equal may differ by tiny epsilon-sized amounts, and that can break edge matching or polygon closure.

Practical implementations often use:

  • a tolerance for comparing coordinates
  • snapping logic for near-equal points
  • library predicates designed for geometric robustness

Ignoring precision issues is one of the fastest ways to get a solution that works only on simple drawings and fails on real data.

Choose the Right Tool for the Goal

If the goal is production geometry, use a geometry library. If the goal is learning or implementing a custom constrained cutter, then write the algorithm yourself and test heavily with edge cases.

That distinction matters because “can I implement polygon splitting” and “should I implement polygon splitting” are different questions.

Common Pitfalls

The most common mistake is assuming that intersection points alone are enough. They are only the start; you still need to reconnect the topology into valid loops.

Another mistake is ignoring cuts that only touch edges or vertices, even though those cases often determine whether the split should produce zero, one, or multiple new polygons.

Developers also underestimate floating-point precision problems, which can easily produce invalid or nearly duplicate vertices.

Summary

  • Splitting a polygon in 2D is a topology problem built on intersections and graph reconstruction.
  • For production code, a geometry library such as Shapely is usually the strongest answer.
  • A valid cut must intersect the polygon in a way that actually separates it into pieces.
  • Custom implementations need intersection handling, loop reconstruction, and validation.
  • Precision handling is essential if you want the generated polygons to be robust on real input data.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.