geometry
circle
closest point
mathematical methods
problem-solving

Best way to find a point on a circle closest to a given point

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

The closest point on a circle to a given point lies on the line from the circle’s center to that point. Once you see the problem as a vector-normalization step, the solution becomes short, efficient, and easy to implement in code.

The Geometry Behind the Answer

Assume the circle has center (cx, cy) and radius r, and the point is (px, py).

First compute the vector from the center to the point:

  • 'dx = px - cx'
  • 'dy = py - cy'

That vector tells you the direction from the center toward the point. The nearest point on the circle is simply that direction scaled to length r.

If the distance from the center to the point is:

  • 'd = sqrt(dx * dx + dy * dy)'

then the closest point on the circle is:

  • 'x = cx + r * dx / d'
  • 'y = cy + r * dy / d'

This works because every point on the circle is exactly r units from the center, and the shortest path from the external point to the circle lies along the radial line.

Why This Is the Closest Point

Geometrically, any point on the circle that is not on the radial line forms a longer triangle side to the target point. The radial direction gives the minimum Euclidean distance because it aligns the circle point directly with the center-to-point direction.

This is true whether the point is outside the circle or inside it. The same formula gives the nearest point on the circumference in both cases.

If the point is outside, the result is the point on the near side of the circle. If the point is inside, the result is still the nearest boundary point in that same direction.

A Simple Implementation

Here is a small Python function:

python
1import math
2
3def closest_point_on_circle(cx, cy, r, px, py):
4    dx = px - cx
5    dy = py - cy
6    d = math.hypot(dx, dy)
7
8    if d == 0:
9        raise ValueError("point is at the center; direction is undefined")
10
11    return (
12        cx + r * dx / d,
13        cy + r * dy / d,
14    )
15
16
17print(closest_point_on_circle(2.0, 3.0, 5.0, 7.0, 9.0))

This prints a point close to (5.2, 6.84).

A Worked Example

Take:

  • center (2, 3)
  • radius 5
  • point (7, 9)

Then:

  • 'dx = 5'
  • 'dy = 6'
  • 'd = sqrt(61)'

Substitute into the formula:

  • 'x = 2 + 5 * 5 / sqrt(61)'
  • 'y = 3 + 5 * 6 / sqrt(61)'

Numerically, the answer is approximately:

  • 'x = 5.20'
  • 'y = 6.84'

That point lies on the circle and is the unique closest point on the circumference.

Handling the Center Case

There is one special case: the given point is exactly at the center of the circle. Then dx and dy are both zero, so there is no unique closest point. Every point on the circle is equally far away.

In code, you need to decide what behavior you want:

  • raise an error
  • return a fixed point such as (cx + r, cy)
  • choose a direction based on application context

For geometry libraries, raising an error is often the cleanest choice because it forces the caller to handle the ambiguity explicitly.

Performance and Numerical Stability

This method is constant time and uses only a few arithmetic operations plus one square root. That makes it appropriate for graphics, collision detection, robotics, and path-planning loops.

For floating-point inputs, the only real numerical issue is the center case or points extremely close to the center. In those situations, compare the distance to a small tolerance instead of exact zero if your inputs are noisy.

Common Pitfalls

The most common mistake is using the point-to-circle distance formula and stopping there without actually computing the coordinates of the nearest point.

Another mistake is forgetting the center case. If the target point equals the circle center, division by zero occurs and there is no unique solution.

Developers also sometimes normalize the wrong vector, such as from the point to the center instead of from the center to the point. That gives the farthest point on the circle instead of the nearest one.

Finally, be careful with integer division in languages where dividing integers truncates values. Use floating-point arithmetic for the final coordinate calculation.

Summary

  • Compute the vector from the circle center to the given point.
  • Normalize that vector and scale it by the radius.
  • Add the scaled vector back to the center to get the closest point.
  • The method works for points both outside and inside the circle.
  • The only ambiguous case is when the point is exactly at the center.

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