%% 2-D Query
% Compute the nearest neighbors in a 2-D triangulation.
% Create a 2-D triangulation.

C = [5 3 1; 3 2 1; 3 4 2; 4 6 2];
TP = [2.5 8.0; 6.5 8.0; 2.5 5.0; 6.5 5.0; 1.0 6.5; 8.0 6.5];
TR = triangulation(C,TP);

%
% Define two query points.

P = [2 4; 6 6.5];

%
% Plot the triangulation and the query points.

triplot(TR)
hold on
plot(P(:,1),P(:,2),'k*')
ylim([1.5 8.5])
xlim([0.5 8.5])

%
% Find the nearest neighbors to the query points and the distances between them.

[ID,d] = nearestNeighbor(TR,P);

%
% Highlight in red the points in the triangulation that are the nearest neighbors to the query points.

N = TP(ID,:);
plot(N(:,1),N(:,2),'*r')

%
% Display the distance between each query point and its nearest neighbor.

d

% Copyright 2015 The MathWorks, Inc.