How to check if line segment intersects a rectangle?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Checking whether a line segment intersects a rectangle is a common problem that arises in fields such as computer graphics, computational geometry, and game development. Determining this intersection can be achieved through a systematic approach that involves geometric reasoning and linear algebra. Below is a detailed exploration of methods to determine if a line segment intersects a rectangle.
Geometric Approach
To solve this problem, we can use the Separating Axis Theorem (SAT) or compute the intersections directly. The goal is to check for the intersection of the line segment with each of the rectangle's four edges.
Line Segment and Rectangle
Let's denote: • The line segment by endpoints and . • The rectangle by its bottom-left corner and top-right corner .
The rectangle can be thought of as consisting of four edges: • Bottom edge: from to . • Top edge: from to . • Left edge: from to . • Right edge: from to .
Line Equation Method
A line in a 2D plane can be described using a parametric equation involving a parameter : where ranges from 0 to 1 for the segment.
To check for intersection with a rectangle, determine if the line parametrically intersects any edge. For example, check for intersection with the horizontal edges:
- Given the segment from to , compute the intersection between the segment and the line .
- Solve the line equations to find the potential intersection point .
- Similarly, compute for other sides.
Intersection Logic
- Calculate Intersection: Solve for intersections where the line and the parametric line intersect, i.e., where .
- Parameter Check: Ensure the computed intersection is within segment bounds and within the bounds of rectangle sides.
- Repeat: Perform the same calculation for other edges of the rectangle.
Code Example in Python
Here is a sample Python code to illustrate this:
• Numerical Precision: Be aware of floating-point arithmetic issues, particularly with collinear points. • Parallel Edges: Handle the case where the line segment is parallel to rectangle sides.
Related reading
- How to check if two permutations are symmetric?
- How to compute locations of mesh points when resolution is increased?
- How to compute mean average robustly?
- How to compute shortest unique prefixes of a set of strings?
- How to compute the intersection points of a line and an arbitrary shape?
- How to compute the union polygon of two or more rectangles
- how to convert logits to probability in binary classification in tensorflow?
- How to convert the half-spaces that constitute a convex hull to a set of extreme points?

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.