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

    %% 2-D Point Location Search
%% Section 1
% This example shows how to use the |delaunayTriangulation| class to perform a point location search in 2-D.

% Copyright 2015 The MathWorks, Inc.


%%
% Begin with a set of 2-D points.
X = [3.5 8.2; 6.8 8.3; 1.3 6.5; 3.5 6.3; 5.8 6.2; ...
     8.3 6.5; 1 4; 2.7 4.3; 5 4.5; 7 3.5; 8.7 4.2; ...
     1.5 2.1; 4.1 1.1; 7 1.5; 8.5 2.75];
 
 %%
 % Create the triangulation and plot it showing the triangle ID labels at
 % the incenters of the triangles.
 dt = delaunayTriangulation(X);
triplot(dt);

hold on
ic = incenter(dt);
numtri = size(dt,1);
trilabels = arrayfun(@(x) {sprintf('T%d', x)}, (1:numtri)');
Htl = text(ic(:,1), ic(:,2), trilabels, 'FontWeight', ...
      'bold', 'HorizontalAlignment', 'center', 'Color', ...
      'blue');
hold off

%%
% Now create some query points and add them to the plot. Then find the
% index of the corresponding enclosing triangles using the |pointLocation|
% method.
q = [5.9344    6.2363;
    2.2143    2.1910;
    7.0948    3.6615;
    7.6040    2.2770;
    6.0724    2.5828;
    6.5464    6.9407;
    6.4588    6.1690;
    4.3534    3.9026;
    5.9329    7.7013;
    3.0271    2.2067];

hold on; 
plot(q(:,1),q(:,2),'*r'); 
vxlabels = arrayfun(@(n) {sprintf('q%d', n)}, (1:10)');
Hpl = text(q(:,1)+0.2, q(:,2)+0.2, vxlabels, 'FontWeight', ...
      'bold', 'HorizontalAlignment','center', ... 
      'BackgroundColor', 'none');
hold off

ti = pointLocation(dt,q);


%%
% Performing a point-location search in 3-D is a direct extension of
% performing a point-location search in 2-D with |delaunayTriangulation|.