Circle to Circle Segment Collision
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
In computational geometry and physics simulations, detecting collisions between shapes is fundamental. A common problem is detecting intersections between a circle and a line segment. This type of collision detection is crucial in various applications such as video games, computer graphics, and robotics. This article delves into the technicalities of circle to circle segment collision detection, offering explanations, examples, and a concise summary.
Basics of Collision Detection
Collision detection between a circle and a line segment involves determining if and where a circle intersects the given segment. A circle, defined by its center and radius , can potentially intersect a line segment defined by two endpoints . The goal is to establish whether these two geometric entities overlap.
Mathematical Approach
Line Segment Representation
Assume we have a line segment with endpoints and . The parametric equation of the line segment is given by:
where . This parameter traces the line from to .
Distance Calculation
To determine the intersection, compute the perpendicular distance from the center of the circle to the line defined by the segment. The distance from a point to the line to can be calculated using the formula:
Collision Detection
- Distance Check: First, check if the perpendicular distance from the circle's center to the line is less than or equal to the radius .
- Projection: Next, project the center of the circle onto the line segment and determine the closest point on the segment. This involves finding the projection using:
- Segment Clamping: Clamp to ensure the closest point is on the line segment:
- Intersection Check: If the closest point on the segment is from the circle's center, an intersection occurs.
Implementation
Here's a concise pseudo-code implementation:
• Coincident Points: If equals , treat as a point-to-circle distance check. • Tangency: If the circle is just tangent to the segment, the distance from the center to the segment equals . • Early Exit: Skip distance computation if the difference in bounds of or coordinates exceeds . • Spatial Partitioning: Utilize quadtree or grid systems for multiple object collision management.
Related reading
- Closest group of 3 points
- Closest point to a given point
- Code a linear programming exercise by hand
- Collatz Conjecture Python - Incorrect Output Above 2 Trillion Only
- Collision detection of huge number of circles
- Combinatorics Algorithm
- Combinatorics of cardgame hand evaluation for runs, with wildcards and duplicates
- Combine smaller rectangles into larger ones

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.