www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/LinearInterpolationUsingadelaunayTriangulationQueryExample.m

    %% 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. 
P = -2.5 + 5*gallery('uniformdata',[50 2],0);
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*gallery('uniformdata',[10 2],1);  

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