Main Content

This example shows how to perform nearest-neighbor interpolation on a scattered set of points using a specific Delaunay triangulation.

Create a `delaunayTriangulation`

of a set of scattered points in 2-D.

```
rng('default')
P = -2.5 + 5*rand([50 2]);
DT = delaunayTriangulation(P)
```

DT = delaunayTriangulation with properties: Points: [50x2 double] ConnectivityList: [84x3 double] Constraints: []

Sample a parabolic function, *V(x,y)*, at the points specified in `P`

.

V = P(:,1).^2 + P(:,2).^2;

Define 10 random query points.

Pq = -2 + 4*rand([10 2]);

Perform nearest-neighbor interpolation on `V`

using the triangulation, `DT`

. Use `nearestNeighbor`

to find the indices of the nearest-neighbor vertices, `vi`

, for the set of query points, `Pq`

. Then examine the specific values of `V`

at the indices.

vi = nearestNeighbor(DT,Pq); Vq = V(vi)

`Vq = `*10×1*
2.7208
3.7792
1.8394
3.5086
1.8394
3.5086
1.4258
5.4053
4.0670
0.5586

This example shows how to perform linear interpolation on a scattered set of points with a specific Delaunay triangulation.

You can use the `triangulation`

method, `pointLocation`

, to compute the enclosing triangle of a query point and the magnitudes of the vertex weights. The weights are called barycentric coordinates, and they represent a partition of unity. That is, the sum of the three weights equals 1. The interpolated value of a function, *V*, at a query point is the sum of the weighted values of *V* at the three vertices. That is, if the function has values, V1, V2, V3 at the three vertices, and the weights are B1, B2, B3, then the interpolated value is (V1)(B1) + (V2)(B2) + (V3)(B3).

Create a `delaunayTriangulation`

of a set of scattered points in 2-D.

```
rng('default')
P = -2.5 + 5*rand([50 2]);
DT = delaunayTriangulation(P)
```

DT = delaunayTriangulation with properties: Points: [50x2 double] ConnectivityList: [84x3 double] Constraints: []

Sample a parabolic function, *V(x,y)*, at the points in `P`

.

V = P(:,1).^2 + P(:,2).^2;

Define 10 random query points.

Pq = -2 + 4*rand([10 2]);

Find the triangle that encloses each query point using the `pointLocation`

method. In the code below, `ti`

contains the IDs of the enclosing triangles and `bc`

contains the barycentric coordinates associated with each triangle.

[ti,bc] = pointLocation(DT,Pq);

Find the values of *V(x,y)* at the vertices of each enclosing triangle.

triVals = V(DT(ti,:));

Calculate the sum of the weighted values of *V(x,y)* using the dot product.

Vq = dot(bc',triVals')'

`Vq = `*10×1*
2.2736
4.2596
2.1284
3.5372
4.6232
2.1797
1.2779
4.7644
3.6311
1.2196

`delaunayTriangulation`

| `nearestNeighbor`

| `pointLocation`