Home > matGeom > polygons2d > polylineCentroid.m

polylineCentroid

PURPOSE ^

POLYLINECENTROID Computes the centroid of a curve defined by a series of points.

SYNOPSIS ^

function center = polylineCentroid(varargin)

DESCRIPTION ^

POLYLINECENTROID Computes the centroid of a curve defined by a series of points.

   PT = polylineCentroid(POINTS);
   Computes center of mass of a polyline defined by POINTS. POINTS is a
   N-by-D array of double, representing a set of N points in a
   D-dimensional space.

   PT = polylineCentroid(PTX, PTY);
   PT = polylineCentroid(PTX, PTY, PTZ);
   Specifies points as separate column vectors

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

   Example
   poly = [0 0;10 0;10 10;20 10];
   polylineCentroid(poly)
   ans = 
       [10 5]

   See also
     polygons2d, centroid, polygonCentroid, polylineLength

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function center = polylineCentroid(varargin)
0002 %POLYLINECENTROID Computes the centroid of a curve defined by a series of points.
0003 %
0004 %   PT = polylineCentroid(POINTS);
0005 %   Computes center of mass of a polyline defined by POINTS. POINTS is a
0006 %   N-by-D array of double, representing a set of N points in a
0007 %   D-dimensional space.
0008 %
0009 %   PT = polylineCentroid(PTX, PTY);
0010 %   PT = polylineCentroid(PTX, PTY, PTZ);
0011 %   Specifies points as separate column vectors
0012 %
0013 %   PT = polylineCentroid(..., TYPE);
0014 %   Specifies if the last point is connected to the first one. TYPE can be
0015 %   either 'closed' or 'open'.
0016 %
0017 %   Example
0018 %   poly = [0 0;10 0;10 10;20 10];
0019 %   polylineCentroid(poly)
0020 %   ans =
0021 %       [10 5]
0022 %
0023 %   See also
0024 %     polygons2d, centroid, polygonCentroid, polylineLength
0025 %
0026 
0027 % ------
0028 % Author: David Legland
0029 % E-mail: david.legland@inrae.fr
0030 % Created: 2006-05-22
0031 % Copyright 2006-2024 INRA - TPV URPOI - BIA IMASTE
0032 
0033 %% process input arguments
0034 
0035 % check whether the curve is closed
0036 closed = false;
0037 var = varargin{end};
0038 if ischar(var)
0039     if strcmpi(var, 'closed')
0040         closed = true;
0041     end
0042     % remove last argument
0043     varargin(end) = [];
0044 end
0045 
0046 % extract point coordinates
0047 if isscalar(varargin)
0048     points = varargin{1};
0049 elseif length(varargin)==2
0050     points = [varargin{1} varargin{2}];
0051 end
0052 
0053 
0054 %% Main computation
0055 
0056 % compute centers and lengths composing the curve
0057 if closed
0058     centers = (points + points([2:end 1],:))/2;
0059     lengths = sqrt(sum(diff(points([1:end 1],:)).^2, 2));
0060 else
0061     centers = (points(1:end-1,:) + points(2:end,:))/2;
0062     lengths = sqrt(sum(diff(points).^2, 2));
0063 end
0064 
0065 % centroid of edge centers weighted by edge length
0066 %weigths = repmat(lengths/sum(lengths), [1 size(points, 2)]);
0067 center = sum(centers .* repmat(lengths, [1 size(points, 2)]), 1) / sum(lengths);
0068

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