POLYGONLENGTH Perimeter of a polygon. L = polygonLength(POLYGON); Computes the boundary length of a polygon. POLYGON is given by a N-by-2 array of vertices. Example % Perimeter of a circle approximation poly = circleToPolygon([0 0 1], 200); polygonLength(poly) ans = 6.2829 See also polygons2d, polygonCentroid, polygonArea, drawPolygon, polylineLength
0001 function len = polygonLength(poly, varargin) 0002 %POLYGONLENGTH Perimeter of a polygon. 0003 % 0004 % L = polygonLength(POLYGON); 0005 % Computes the boundary length of a polygon. POLYGON is given by a N-by-2 0006 % array of vertices. 0007 % 0008 % Example 0009 % % Perimeter of a circle approximation 0010 % poly = circleToPolygon([0 0 1], 200); 0011 % polygonLength(poly) 0012 % ans = 0013 % 6.2829 0014 % 0015 % See also 0016 % polygons2d, polygonCentroid, polygonArea, drawPolygon, polylineLength 0017 % 0018 0019 % ------ 0020 % Author: David Legland 0021 % E-mail: david.legland@inrae.fr 0022 % Created: 2005-05-11 0023 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE 0024 0025 % If first argument is a cell array, this is a multi-polygon, and we simply 0026 % add the lengths of individual polygons 0027 if iscell(poly) 0028 len = 0; 0029 for i = 1:length(poly) 0030 len = len + polygonLength(poly{i}); 0031 end 0032 return; 0033 end 0034 0035 % case of a polygon given as two coordinate arrays 0036 if nargin == 2 0037 poly = [poly varargin{1}]; 0038 end 0039 0040 % check there are enough points 0041 if size(poly, 1) < 2 0042 len = 0; 0043 return; 0044 end 0045 0046 % compute length 0047 if size(poly, 2) == 2 0048 % polygon in dimension 2 (classical case) 0049 dp = diff(poly([1:end 1], :), 1, 1); 0050 len = sum(hypot(dp(:, 1), dp(:, 2))); 0051 else 0052 % polygon of larger dimension 0053 len = sum(sqrt(sum(diff(poly([2:end 1], :), 1, 1).^2, 2))); 0054 end