%% Linear Interpolation Using a delaunayTriangulation Query
% 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)

%
% 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')'

% Copyright 2012 The MathWorks, Inc.