%% Nearest Vertex
% Create a polyshape object and compute the nearest vertex of the polyshape to the query point (2,1.8).

P = [1 1; 3 1; 2 2];
polyin = polyshape(P);
[vertexid,boundaryid,ind] = nearestvertex(polyin,2,1.8);

% Plot the polyshape and the query point in black.

plot(polyin)
hold on
plot(2,2.5,'k*')

% Now plot the nearest vertex in red. The vertexid value enables you to index into the row of polyin.Vertices corresponding to the nearest vertex, accessing its coordinates.

plot(polyin.Vertices(vertexid,1),polyin.Vertices(vertexid,2),'r*')
hold off

% Copyright 2012 The MathWorks, Inc.