%% Creating a triangulation Using delaunayTriangulation
% This example shows how to create a Delaunay triangulation using delaunayTriangulation.
% When you create a Delaunay triangulation using the delaunayTriangulation class, you automatically get access to the triangulation methods because delaunayTriangulation is a subclass of triangulation.
%
% Create a delaunayTriangulation from a set of points.

P = [ 2.5    8.0
      6.5    8.0
      2.5    5.0
      6.5    5.0
      1.0    6.5
      8.0    6.5];

DT = delaunayTriangulation(P)

% The resulting delaunayTriangulation object has the properties, Points and ConnectivityList, just like a triangulation object.
%
% You can access the triangulation using direct indexing, just like triangulation. For example, examine the connectivity of the first triangle.

DT(1,:)

%
% Next, examine the connectivity of the entire triangulation.

DT(:,:)

%
% Use the triplot function to plot the triangulation.

triplot(DT)
axis equal

%
% The parent class, triangulation, provides the incenter method to compute the incenters of each triangle.

IC = incenter(DT)

% The returned value, IC, is an array of coordinates representing the incenters of the triangles.
%
% Now, use the incenters to find the positions for placing triangle labels on the plot.

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

%
% Instead of creating a Delaunay triangulation using delaunayTriangulation, you could use the delaunay function to create the triangulation connectivity data, and then pass the connectivity data to triangulation. For example,

P = [ 2.5    8.0
      6.5    8.0
      2.5    5.0
      6.5    5.0
      1.0    6.5
      8.0    6.5];

T = delaunay(P);
TR = triangulation(T,P);
IC = incenter(TR);

% Both approaches are valid in this example, but if you want to create a Delaunay triangulation and perform queries on it, then you should use delaunayTriangulation for these reasons:
% The delaunayTriangulation class provides additional methods that are useful for working with triangulations. For example, you can to perform nearest-neighbor and point-in-triangle searches.
% It allows you to edit the triangulation to add, move, or remove points.
% It allows you to create constrained Delaunay triangulations. This allows you to create a triangulation for a 2-D domain.
% Copyright 2015 The MathWorks, Inc.