Vertex closest to specified point
Compute the nearest neighbors in a 3-D triangulation.
Create a 3-D Delaunay triangulation.
P = [1 1 0; -1 1 0; -1 -1 0; 1 -1 0; 0 0 2; 0 0 0]; TR = delaunayTriangulation(P);
Plot the triangulation and a query point.
tri = TR(:,:); trisurf(tri,P(:,1),P(:,2),P(:,3),'FaceAlpha',0.5) hold on x = 0; y = -0.5; z = 2; plot3(x,y,z,'k*')
Find the coordinates of the nearest neighbor to the query point.
ID = nearestNeighbor(TR,x,y,z); C = TR.Points(ID,:)
C = 1×3 0 0 2
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 = 2×1 1.1180 1.5811
TR — Triangulation representation
scalar triangulation object
Triangulation representation, specified as a scalar
nearestNeighbor does not support
delaunayTriangulation objects with constrained
P — Query points
2-column matrix (2-D) | 3-column matrix (3-D)
Query points, specified as a matrix with 2 or 3 columns.
P contains the x-coordinates,
y-coordinates, and (possibly)
z-coordinates of the query points.
x — x-coordinates
x-coordinates of query points, specified as a column vector.
y — y-coordinates
y-coordinates of query points, specified as a column vector.
z — z-coordinates
z-coordinates of query points, specified as a column vector.
ID — Vertex IDs
Vertex IDs of the nearest neighbors to the query points, returned as a
column vector. A vertex ID is the row number of the corresponding vertex in
d — Euclidean distances
Euclidean distances from the query points to their nearest neighbors,
returned as a column vector the same length as
Introduced in R2013a