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 % INRA - TPV URPOI - BIA IMASTE 0022 % created the 11/05/2005. 0023 % 0024 0025 % HISTORY 0026 % 2011-03-31 add control for empty polygons, code cleanup 0027 % 2011-05-27 fix bugs 0028 0029 % If first argument is a cell array, this is a multi-polygon, and we simply 0030 % add the lengths of individual polygons 0031 if iscell(poly) 0032 len = 0; 0033 for i = 1:length(poly) 0034 len = len + polygonLength(poly{i}); 0035 end 0036 return; 0037 end 0038 0039 % case of a polygon given as two coordinate arrays 0040 if nargin == 2 0041 poly = [poly varargin{1}]; 0042 end 0043 0044 % check there are enough points 0045 if size(poly, 1) < 2 0046 len = 0; 0047 return; 0048 end 0049 0050 % compute length 0051 if size(poly, 2) == 2 0052 % polygon in dimension 2 (classical case) 0053 dp = diff(poly([1:end 1], :), 1, 1); 0054 len = sum(hypot(dp(:, 1), dp(:, 2))); 0055 else 0056 % polygon of larger dimension 0057 len = sum(sqrt(sum(diff(poly([2:end 1], :), 1, 1).^2, 2))); 0058 end