%% Convex Hull Computation Using the delaunayTriangulation Class
% This example shows the relationship between a Delaunay triangulation of a set of points in 2-D and the convex hull of that set of points.
% The delaunayTriangulation class supports computation of Delaunay triangulations in 2-D and 3-D space. This class also provides a convexHull method to derive the convex hull from the triangulation.
%
% Create a Delaunay triangulation of a set of points in 2-D.

X = [-1.5 3.2; 1.8 3.3; -3.7 1.5; -1.5 1.3; 0.8 1.2; ...
      3.3 1.5; -4.0 -1.0; -2.3 -0.7; 0 -0.5; 2.0 -1.5; ...
      3.7 -0.8; -3.5 -2.9; -0.9 -3.9; 2.0 -3.5; 3.5 -2.25];

dt = delaunayTriangulation(X);

%
% Plot the triangulation and highlight the edges that are shared only by a single triangle reveals the convex hull.

triplot(dt)

fe = freeBoundary(dt)';
hold on
plot(X(fe,1), X(fe,2), '-r', 'LineWidth',2)
hold off

% In 3-D, the facets of the triangulation that are shared only by one tetrahedron represent the boundary of the convex hull.
% The dedicated convhull function is generally more efficient than a computation based on the convexHull method. However, the triangulation based approach is appropriate if:
% You have a delaunayTriangulation of the point set already and the convex hull is also required.
% You need to add or remove points from the set incrementally and need to recompute the convex hull frequently after you have edited the points.
% Copyright 2015 The MathWorks, Inc.