Reputation: 355
I have a curve cutting through my mesh in 2D. It is a moving to the front with time. I have set of points on this curve (front) and my nodes on the mesh. At each time step I need to find which point on the curve (front) is closest to the nodes on my mesh. In other words for each node in my mesh I would like to know which point on the curve is closest to it. Is there a built in MATLAB function to search for this ? (I am using MATLAB environment)
In the figure the question would be which one is the closest black circle to any of the yellow squares.
Upvotes: 0
Views: 1058
Reputation: 3536
Here is an efficient function to calculate pairwise distances:
function D = sqDistance(X, Y)
D = bsxfun(@plus,dot(X,X,1)',dot(Y,Y,1))-2*(X'*Y);
end
Assuming circles
are the coordinates of the black circles and squares
the coordinates of the yellow squares as you described, you can do the following:
% example matrices
circles = rand(5,2);
squares = rand(8,2);
D = sqDistance(squares', circles');
[~,idx] = sort(D, 2)
closest_points = circles(idx(:,1),:)
closest_points
has the same dimension as squares
and stores the coordinates of the closest circle for every yellow square.
Upvotes: 2