How to find point closest to multiple circles?

12 views (last 30 days)
Hi!
I want to solve the following problem using Matlab:
In the xy-plane, I have multiple circles, defined by their midpoint and their radius. Some of the circles overlap, others dont.
Now, I want to find a point A in the xy-plane that is closest to all circles.
So in other words: I want to find the point A for which the sum of distances to a point (it does not matter which) on each of the circles is minimal.
Does anyone know how to do this?
Edit:
To clarify my question: the circles can also be completely inside another circle. I added two images of two example situations. In both situations, I want to find point A. Question2.jpg
Question1.jpg
Thanks!
Bas
  4 Comments
Image Analyst
Image Analyst on 31 Jan 2019
I have the same question as Jan. And your latest comment did not answer his or my questions.
What does distance of your point to a circle mean?
  1. Distance to the center?
  2. Or the perimeter points?
If it's the center, maybe just averaging the centers will be where you want it. If it's to the perimeter points, then be aware that the radii will affect the answer since there will be more perimeter points on a larger circle, so their distances will give a greater sum than for a small circle.
Bas Wagemaker
Bas Wagemaker on 31 Jan 2019
Hi!
I defined the distance that we are talking about as the distance between the point and one of the perimeter points of each circle. The distance to the center would be easier, since this is one single point per circle, instead of a set.
Since all of you have helped me a lot, I think you deserve some background information. The question is related to my Mechanical Engineering Masters thesis. In fact, the centers of the circles represent points at which one end of a mechanical spring is attached, for different positions of a lever arm. The length of the spring is determined by the required energy in the spring per position. The radius of each circle is equal to the required spring length in each position.
Ideally, all circles intersect in one point, which would be the optimal attachment point of the other end spring (this is why I defined the distance to be the distance between the point and the perimeter). However, in most cases, this ideal point does not exist, and I was therefore looking for the point that is closest to the non-existing intersection point.
Thanks again for your help!

Sign in to comment.

Accepted Answer

Matt J
Matt J on 31 Jan 2019
Edited: Matt J on 31 Jan 2019
Let C be a 1x2xN matrix of the circle centers and R an 1x1xN vector of their radii. It is a low-dimensional, non-smooth minimization problem so, I would use fminsearch:
rownorm=@(z) sqrt( sum(z.^2,2) );
fun=@(A) sum( rownorm( (A-C) - (A-C).*R./rownorm(A-C)) , 3 ); %edited
A = fminsearch(fun,A0(:).');
Luckily, it is only a 2D problem, so you can come up with a decent initial guess A0 using a brute force grid search, e.g.,
[X,Y]=ndgrid(linspace(-1,1,100)); %adjust grid limits depending on C and R
Agrid=[X(:),Y(:)];
[~,gridmin]=min( fun(Agrid) );
A0=Agrid(gridmin,:);
  10 Comments
Bas Wagemaker
Bas Wagemaker on 31 Jan 2019
Thanks, it works like a charm!
Could you please explain what the fun function does?
Matt J
Matt J on 31 Jan 2019
You're welcome. Please Accept-click the answer, though, to certify that it addresses the problem.
fun is the thing you are trying to minimize. It computes the sum of the distances from A to your circles.
Capture.PNG

Sign in to comment.

More Answers (2)

Jim Riggs
Jim Riggs on 31 Jan 2019
Personally, I would use a numerical searcing algorithm.
Overlay a grid on the problem space, then compute the distance function (sum of distance to all circles) from each grid point.
Select the minimum value, then refine the grid and search from the minimum point.
Continue until the desired numerical precision is schieved.
Distance function is the sum of distance from Point P, to each circle. If P is outside the circle, then the distance to that circle is the distance from P to the circle center, minus the circle radius, but if P is inside the circle (Distance from P to the center is less than the radius) then the distance to the circle is the radius minus the distance from P to the center.
  2 Comments
Jan
Jan on 31 Jan 2019
The grid search will fail for a set of concentric circles, because you will get a circle as solution, not a single point.
Bas Wagemaker
Bas Wagemaker on 31 Jan 2019
Hi!
Thanks for your reply.
Indeed, while I was writing this question, I thought about the same solution. However, imagine that I define each circle by 100 points. And I have a grid of 10x10 points. Supposing I have six circles and I want to evaluate every point in my grid, I have to calculate the distance between each of the grid points and each point on the six circles. This results in 100*6*100=60.000 calculations, without any refinement steps. I do not know how fast Matlab can do this..
Do you know any efficient way to do this?
Thanks!

Sign in to comment.


Jan
Jan on 31 Jan 2019
Edited: Jan on 31 Jan 2019
A simple grid search with loops - just for demonstration:
% Define circles:
nCircle = 10;
Center = rand(nCircle, 2);
Radius = rand(nCircle, 1) / 5;
% To show that result is not unique:
% nCircle = 3;
% Center = repmat(0.5, nCircle, 2);
% Radius = [0.5, 1, 1.5];
% Get distances on a 50x50 grid:
nGrid = 50;
gridv = linspace(0, 1, nGrid);
Dist = zeros(nGrid, nGrid);
axes('NextPlot', 'add');
view(3)
for ix = 1:nGrid
x = gridv(ix);
for iy = 1:nGrid
y = gridv(iy);
D = 0;
for iC = 1:nCircle
D = D + abs(sqrt((x - C(iC, 1))^2 + (y - C(iC, 2))^2) - R(iC));
end
Dist(ix, iy) = D;
end
end
% Draw sum of distances:
surf(gridv, gridv, Dist.' - min(Dist(:)), 'FaceAlpha', 0.5);
% Draw the circles
v = linspace(0, 2*pi, 50);
for iC = 1:nCircle
line(C(iC, 1) + sin(v) * R(iC), C(iC, 2) + cos(v) * R(iC));
end
Grafics for 3 concentric circles:
Clipboard-1.png
  1 Comment
Bas Wagemaker
Bas Wagemaker on 31 Jan 2019
Thanks!
I like the visual representation of the distance! I will certainly look into your example code to implement it in mine.
Have a nice evening!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!