简体   繁体   中英

Find the point on a curve which is closest to a given point

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM