Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to cover maximal number of points with one circle of given radius

Let's imagine we have a plane with some points on it. We also have a circle of given radius.

I need an algorithm that determines such position of the circle that it covers maximal possible number of points. Of course, there are many such positions, so the algorithm should return one of them.
Precision is not important and the algorithm may do small mistakes.

Here is an example picture:
Example

Input:
  int n (n<=50) – number of points;
  float x[n] and float y[n] – arrays with points' X and Y coordinates;
  float r – radius of the circle.

Output:
  float cx and float cy – coordinates of the circle's center

Lightning speed of the algorithm is not required, but it shouldn't be too slow (because I know a few slow solutions for this situation).

C++ code is preferred, but not obligatory.

like image 404
Oleh Prypin Avatar asked Jul 12 '10 14:07

Oleh Prypin


People also ask

What is the maximum number of points that can lie inside the circle?

There are only 3 points lie inside or on the circumference of the circle.

How do you find more points on a circle?

Then we can use the radius to find the circle's leftmost and rightmost points. The equation of a circle in standard form is (x−h)2+(y−k)2=r2, where (h,k) is the center, and r is the radius of the circle. The ordered pair (x,y) represents any point on the circle.


1 Answers

Edited to better wording, as suggested :

Basic observations :

  • I assume the radius is one, since it doesn't change anything.
  • given any two points, there exists at most two unit circles on which they lie.
  • given a solution circle to your problem, you can move it until it contains two points of your set while keeping the same number of points of your set inside it.

The algorithm is then:

  • For each pair of points, if their distance is < 2, compute the two unit circles C1 and C2 that pass through them.
  • Compute the number of points of your set inside C1 and C2
  • Take the max.
like image 138
Alexandre C. Avatar answered Oct 03 '22 06:10

Alexandre C.