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.
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:
- compute all intersections between the cut and polygon edges
- insert the intersection points into both the polygon boundary and the cut line ordering
- split the boundary graph at those inserted points
- walk the resulting graph to build closed loops
- 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.
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.
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
- Generate Non-Degenerate Point Set in 2D - C
- Generate random numbers with a given numerical distribution
- Generate random numbers with logarithmic distribution and custom slope
- Generate random permutation of huge list in Python
- Generating a gaussian distribution with only positive numbers
- Generating all 5 card poker hands
- Generating All Combinations of List n Levels Deep in Java
- Generating all factors of a number given its prime factorization

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 courseTrack 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.