# nearestNeighbor

## 语法

``ID = nearestNeighbor(TR,P)``
``ID = nearestNeighbor(TR,x,y)``
``ID = nearestNeighbor(TR,x,y,z)``
``[ID,d] = nearestNeighbor(___)``

## 说明

``ID = nearestNeighbor(TR,P)` 返回最接近 `P` 中查询点的顶点的 ID。矩阵 `P` 中的每行都包含一个查询点的坐标。`
``ID = nearestNeighbor(TR,x,y)` 将二维查询点的 `x` 坐标和 `y` 坐标指定为单独的列向量。`

``ID = nearestNeighbor(TR,x,y,z)` 将三维查询点的 x 坐标、y 坐标和 z 坐标指定为单独的列向量。`

`对于上述任意语法，`[ID,d] = nearestNeighbor(___)` 还会返回每个查询点与其最近邻点之间的欧几里德距离。`

## 示例

```P = [1 1 0; -1 1 0; -1 -1 0; 1 -1 0; 0 0 2; 0 0 0]; TR = delaunayTriangulation(P);```

```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*')```

```ID = nearestNeighbor(TR,x,y,z); C = TR.Points(ID,:)```
```C = 1×3 0 0 2 ```

```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);```

`P = [2 4; 6 6.5];`

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

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

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

`d`
```d = 2×1 1.1180 1.5811 ```