CONVEXHULL Convex hull of a set of points. POLY = convexHull(POINTS) Computes the convex hull of the set of points POINTS. This function is mainly a wrapper to the convhull function, that format the result to a polygon. [POLY, INDS] = convexHull(POINTS) Also returns the indices of convex hull vertices within the original array of points. ... = convexHull(POINTS, 'simplify', BOOL) specifies the 'simplify' option use dfor calling convhull. By default, the convexHull functions uses simplify equals to TRUE (contrary to the convhull function), resulting in a more simple convex polygon. Example % Draws the convex hull of a set of random points pts = rand(30,2); drawPoint(pts, '.'); hull = convexHull(pts); hold on; drawPolygon(hull); % Draws the convex hull of a paper hen x = [0 10 20 0 -10 -20 -10 -10 0]; y = [0 0 10 10 20 10 10 0 -10]; poly = [x' y']; hull = convexHull(poly); figure; drawPolygon(poly); hold on; axis equal; drawPolygon(hull, 'm'); See also polygons2d, convhull
0001 function [hull, inds] = convexHull(points, varargin) 0002 %CONVEXHULL Convex hull of a set of points. 0003 % 0004 % POLY = convexHull(POINTS) 0005 % Computes the convex hull of the set of points POINTS. This function is 0006 % mainly a wrapper to the convhull function, that format the result to a 0007 % polygon. 0008 % 0009 % [POLY, INDS] = convexHull(POINTS) 0010 % Also returns the indices of convex hull vertices within the original 0011 % array of points. 0012 % 0013 % ... = convexHull(POINTS, 'simplify', BOOL) 0014 % specifies the 'simplify' option use dfor calling convhull. By default, 0015 % the convexHull functions uses simplify equals to TRUE (contrary to the 0016 % convhull function), resulting in a more simple convex polygon. 0017 % 0018 % 0019 % Example 0020 % % Draws the convex hull of a set of random points 0021 % pts = rand(30,2); 0022 % drawPoint(pts, '.'); 0023 % hull = convexHull(pts); 0024 % hold on; 0025 % drawPolygon(hull); 0026 % 0027 % % Draws the convex hull of a paper hen 0028 % x = [0 10 20 0 -10 -20 -10 -10 0]; 0029 % y = [0 0 10 10 20 10 10 0 -10]; 0030 % poly = [x' y']; 0031 % hull = convexHull(poly); 0032 % figure; drawPolygon(poly); 0033 % hold on; axis equal; 0034 % drawPolygon(hull, 'm'); 0035 % 0036 % See also 0037 % polygons2d, convhull 0038 % 0039 0040 % ------ 0041 % Author: David Legland 0042 % e-mail: david.legland@nantes.inra.fr 0043 % Created: 2011-04-08, using Matlab 7.9.0.529 (R2009b) 0044 % Copyright 2011 INRA - Cepia Software Platform. 0045 0046 % checkup on array size 0047 if size(points, 1) < 3 0048 hull = points; 0049 inds = 1:size(points, 1); 0050 return; 0051 end 0052 0053 % parse simplify option 0054 simplify = true; 0055 if nargin > 2 && strcmpi(varargin{1}, 'simplify') 0056 simplify = varargin{2}; 0057 end 0058 0059 % compute convex hull by calling the 'convhull' function 0060 inds = convhull(points(:,1), points(:,2), 'simplify', simplify); 0061 hull = points(inds, :);