POLYGONCENTROID3D Centroid (or center of mass) of a polygon. PTC = polygonCentroid3d(POLY) Computes center of mass of a polygon defined by POLY. POLY is a N-by-3 array of double containing coordinates of polygon vertices. PTC = polygonCentroid3d(VX, VY, VZ) Specifies vertex coordinates as three separate arrays. Example % compute centroid of a basic polygon poly = [0 0 0; 10 0 10;10 10 20;0 10 10]; centro = polygonCentroid3d(poly) centro = 5.0000 5.0000 10.0000 See also polygons3d, polygonArea3d, polygonCentroid
0001 function centroid = polygonCentroid3d(varargin) 0002 %POLYGONCENTROID3D Centroid (or center of mass) of a polygon. 0003 % 0004 % PTC = polygonCentroid3d(POLY) 0005 % Computes center of mass of a polygon defined by POLY. POLY is a N-by-3 0006 % array of double containing coordinates of polygon vertices. 0007 % 0008 % PTC = polygonCentroid3d(VX, VY, VZ) 0009 % Specifies vertex coordinates as three separate arrays. 0010 % 0011 % Example 0012 % % compute centroid of a basic polygon 0013 % poly = [0 0 0; 10 0 10;10 10 20;0 10 10]; 0014 % centro = polygonCentroid3d(poly) 0015 % centro = 0016 % 5.0000 5.0000 10.0000 0017 % 0018 % See also 0019 % polygons3d, polygonArea3d, polygonCentroid 0020 % 0021 0022 % ------ 0023 % Author: David Legland 0024 % e-mail: david.legland@inra.fr 0025 % Created: 2007-09-18 0026 % Copyright 2007 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0027 0028 0029 if nargin == 1 0030 % polygon is given as a single argument 0031 pts = varargin{1}; 0032 0033 elseif nargin == 3 0034 % polygon is given as 3 coordinate arrays 0035 px = varargin{1}; 0036 py = varargin{2}; 0037 pz = varargin{3}; 0038 pts = [px py pz]; 0039 end 0040 0041 % create supporting plane (assuming first 3 points are not colinear...) 0042 plane = createPlane(pts(1:3, :)); 0043 0044 % project points onto the plane 0045 pts = planePosition(pts, plane); 0046 0047 % compute centroid in 2D 0048 centro2d = polygonCentroid(pts); 0049 0050 % project back in 3D 0051 centroid = planePoint(plane, centro2d);