Line clipping to arbitary 2D polygon
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 to Line Clipping
Line clipping is a fundamental operation in computer graphics, where the objective is to determine which portions of a line segment are inside or outside a given polygon. While clipping lines to rectangles, like in the Cohen-Sutherland and Liang-Barsky algorithms, is straightforward due to their symmetry and simple shape, clipping to arbitrary polygons requires more sophisticated techniques due to the complexity of the polygons' edges and vertices.
The Problem Statement
In general, line clipping against an arbitrary polygon involves:
- Determining the intersection points of the line segment with the polygon edges.
- Identifying the portion of the line segment that lies inside the polygon.
- Returning the clipped line segment, if it exists.
Key Concepts
Convex vs. Concave Polygons
- Convex Polygon: A polygon is convex if a line segment joining any two points within the polygon lies entirely inside it. Algorithms for clipping lines against convex polygons are generally simpler and faster because such polygons have monotonic boundaries.
- Concave Polygon: This is a polygon that has at least one internal angle greater than 180 degrees. Clipping becomes more complex as lines can intersect the edges multiple times.
Clipping Algorithms
Cyrus-Beck Algorithm
The Cyrus-Beck algorithm is a parametric line-clipping algorithm suitable for convex polygons. It calculates the intersections of a line segment's parametric form with the polygon's edges, leveraging the dot product to determine the entering and leaving points.
- Parametric Line Representation: The line is represented as , where is a parameter ranging from 0 to 1, and are the endpoints of the line segment.
- Normal Vectors and Dot Products: Compute the normal vector to each edge and use the dot product to classify intersections as "potentially entering" or "potentially leaving."
- Calculate t Values: For each intersection with a polygon edge, compute the parameter . Given an edge with a point and outward normal , the intersection parameter is:
- Determine Clipped Line: Collect all entering values (where the dot product is negative) and leaving values (where the dot product is positive). The clipped segment runs from to . If , the line is entirely outside.
Sutherland-Hodgman Algorithm
Though originally designed for polygon clipping, the Sutherland-Hodgman algorithm can be extended for line clipping to arbitrary polygons, particularly concave ones. This algorithm works by systematically processing each line against each edge of the polygon:
- Processing Vertices: For each edge of the polygon, classify each vertex of the line as either inside or outside.
- Processing Intersections: Calculate the intersection points for segments crossing the polygon edges.
- Output the Clipped Line: By iterating through all edges, extract the line segments that remain inside the polygon.
Intersection Calculation
For both algorithms, efficiently calculating the line-polygon edge intersection is crucial. Given a line segment from to and a polygon edge from to , the intersection can be found by solving the system:
where and for a valid intersection within both segments. This system yields:
Here denotes the 2D cross product (scalar result).
Handling Special Cases
- Collinear Segment: When a line segment is collinear with an edge of the polygon, numerical precision issues can cause complications. Use an epsilon tolerance for floating-point comparisons.
- Degenerate Cases: Handle zero-length line segments and singular polygon shapes carefully to avoid computational errors.
Comparison of Algorithms
| Algorithm | Complexity | Suitable for | Pros | Cons |
| Cyrus-Beck | Convex polygons | Fast and precise for convex shapes | Not applicable for concave polygons | |
| Sutherland-Hodgman Extension | Concave and convex polygons | Handles both convex and concave polygons | Potentially slower for polygons with many vertices |
represents the number of polygon edges. is the number of line-edge intersections.
Implementation Example
Below is a Python implementation of the Cyrus-Beck algorithm for clipping a line segment against a convex polygon:
Summary
Line clipping to arbitrary 2D polygons is a foundational problem in computational geometry. For convex polygons, the Cyrus-Beck algorithm provides an efficient solution using parametric intersection tests. For concave polygons, extensions of the Sutherland-Hodgman algorithm handle the additional complexity of multiple intersection regions. The choice of algorithm depends on the polygon type and the performance requirements of the application.
Related reading
- Line rasterisation Cover all pixels, regardless of line gradient?
- Linear Search Algorithm Optimization
- Linear time algorithm for 2-SUM
- Linear time algorithm for Minimum number of jumps required to reach end
- Linear algebra application in Machine Learning
- Linear time algorithm for slicing stacked boxes
- Linear Time Voting Algorithm. I don't get it
- Linked list loop detection algorithm

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.