Home > matGeom > polygons2d > polylineLength.m

polylineLength

PURPOSE ^

POLYLINELENGTH Return length of a polyline given as a list of points.

SYNOPSIS ^

function len = polylineLength(poly, varargin)

DESCRIPTION ^

POLYLINELENGTH Return length of a polyline given as a list of points.

   L = polylineLength(POLY);
   POLY should be a N-by-D array, where N is the number of points and D is
   the dimension of the points.

   L = polylineLength(..., TYPE);
   Specifies if the last point is connected to the first one. TYPE can be
   either 'closed' or 'open'.

   L = polylineLength(POLY, POS);
   Compute the length of the polyline between its origin and the position
   given by POS. POS should be between 0 and N-1, where N is the number of
   points of the polyline.


   Example:
   % Compute the perimeter of a circle with radius 1
   polylineLength(circleAsPolygon([0 0 1], 500), 'closed')
   ans = 
       6.2831

   See also:
   polygons2d, polylineCentroid, polygonLength

 ------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 Created: 2009-04-30,    using Matlab 7.7.0.471 (R2008b)
 Copyright 2009 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function len = polylineLength(poly, varargin)
0002 %POLYLINELENGTH Return length of a polyline given as a list of points.
0003 %
0004 %   L = polylineLength(POLY);
0005 %   POLY should be a N-by-D array, where N is the number of points and D is
0006 %   the dimension of the points.
0007 %
0008 %   L = polylineLength(..., TYPE);
0009 %   Specifies if the last point is connected to the first one. TYPE can be
0010 %   either 'closed' or 'open'.
0011 %
0012 %   L = polylineLength(POLY, POS);
0013 %   Compute the length of the polyline between its origin and the position
0014 %   given by POS. POS should be between 0 and N-1, where N is the number of
0015 %   points of the polyline.
0016 %
0017 %
0018 %   Example:
0019 %   % Compute the perimeter of a circle with radius 1
0020 %   polylineLength(circleAsPolygon([0 0 1], 500), 'closed')
0021 %   ans =
0022 %       6.2831
0023 %
0024 %   See also:
0025 %   polygons2d, polylineCentroid, polygonLength
0026 %
0027 % ------
0028 % Author: David Legland
0029 % e-mail: david.legland@grignon.inra.fr
0030 % Created: 2009-04-30,    using Matlab 7.7.0.471 (R2008b)
0031 % Copyright 2009 INRA - Cepia Software Platform.
0032 
0033 
0034 %   HISTORY
0035 %   2006-05-22 manage any dimension for points, closed and open curves,
0036 %       and update doc accordingly.
0037 %   2009-04-30 rename as polylineLength
0038 %   2011-03-31 add control for empty polylines
0039 
0040 % check there are enough points
0041 if size(poly, 1) < 2
0042     len = 0;
0043     return;
0044 end
0045 
0046 % check whether the curve is closed or not (default is open)
0047 closed = false;
0048 if ~isempty(varargin)
0049     var = varargin{end};
0050     if ischar(var)
0051         if strcmpi(var, 'closed')
0052             closed = true;
0053         end
0054         varargin = varargin(1:end-1);
0055     end
0056 end
0057 
0058 % if the length is computed between 2 positions, compute only for a
0059 % subcurve
0060 if ~isempty(varargin)
0061     % values for 1 input argument
0062     t0 = 0;
0063     t1 = varargin{1};
0064     
0065     % values for 2 input arguments
0066     if length(varargin)>1
0067         t0 = varargin{1};
0068         t1 = varargin{2};
0069     end
0070     
0071     % extract a portion of the polyline
0072     poly = polylineSubcurve(poly, t0, t1);
0073 end
0074 
0075 % compute lengths of each line segment, and sum up
0076 if closed
0077     len = sum(sqrt(sum(diff(poly([1:end 1],:)).^2, 2)));
0078 else
0079     len = sum(sqrt(sum(diff(poly).^2, 2)));
0080 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019