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

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@inrae.fr
0030 % Created: 2009-04-30, using Matlab 7.7.0.471 (R2008b)
0031 % Copyright 2009-2024 INRA - Cepia Software Platform
0032 
0033 % check there are enough points
0034 if size(poly, 1) < 2
0035     len = 0;
0036     return;
0037 end
0038 
0039 % check whether the curve is closed or not (default is open)
0040 closed = false;
0041 if ~isempty(varargin)
0042     var = varargin{end};
0043     if ischar(var)
0044         if strcmpi(var, 'closed')
0045             closed = true;
0046         end
0047         varargin = varargin(1:end-1);
0048     end
0049 end
0050 
0051 % if the length is computed between 2 positions, compute only for a
0052 % subcurve
0053 if ~isempty(varargin)
0054     % values for 1 input argument
0055     t0 = 0;
0056     t1 = varargin{1};
0057     
0058     % values for 2 input arguments
0059     if length(varargin)>1
0060         t0 = varargin{1};
0061         t1 = varargin{2};
0062     end
0063     
0064     % extract a portion of the polyline
0065     poly = polylineSubcurve(poly, t0, t1);
0066 end
0067 
0068 % compute lengths of each line segment, and sum up
0069 if closed
0070     len = sum(sqrt(sum(diff(poly([1:end 1],:)).^2, 2)));
0071 else
0072     len = sum(sqrt(sum(diff(poly).^2, 2)));
0073 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022