annotate toolboxes/FullBNT-1.0.7/KPMtools/polygon_centroid.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [x0,y0] = centroid(x,y)
Daniel@0 2 % CENTROID Center of mass of a polygon.
Daniel@0 3 % [X0,Y0] = CENTROID(X,Y) Calculates centroid
Daniel@0 4 % (center of mass) of planar polygon with vertices
Daniel@0 5 % coordinates X, Y.
Daniel@0 6 % Z0 = CENTROID(X+i*Y) returns Z0=X0+i*Y0 the same
Daniel@0 7 % as CENTROID(X,Y).
Daniel@0 8
Daniel@0 9 % Copyright (c) 1995 by Kirill K. Pankratov,
Daniel@0 10 % kirill@plume.mit.edu.
Daniel@0 11 % 06/01/95, 06/07/95
Daniel@0 12
Daniel@0 13 % Algorithm:
Daniel@0 14 % X0 = Int{x*ds}/Int{ds}, where ds - area element
Daniel@0 15 % so that Int{ds} is total area of a polygon.
Daniel@0 16 % Using Green's theorem the area integral can be
Daniel@0 17 % reduced to a contour integral:
Daniel@0 18 % Int{x*ds} = -Int{x^2*dy}, Int{ds} = Int{x*dy} along
Daniel@0 19 % the perimeter of a polygon.
Daniel@0 20 % For a polygon as a sequence of line segments
Daniel@0 21 % this can be reduced exactly to a sum:
Daniel@0 22 % Int{x^2*dy} = Sum{ (x_{i}^2+x_{i+1}^2+x_{i}*x_{i+1})*
Daniel@0 23 % (y_{i+1}-y_{i})}/3;
Daniel@0 24 % Int{x*dy} = Sum{(x_{i}+x_{i+1})(y_{i+1}-y_{i})}/2.
Daniel@0 25 % Similarly
Daniel@0 26 % Y0 = Int{y*ds}/Int{ds}, where
Daniel@0 27 % Int{y*ds} = Int{y^2*dx} =
Daniel@0 28 % = Sum{ (y_{i}^2+y_{i+1}^2+y_{i}*y_{i+1})*
Daniel@0 29 % (x_{i+1}-x_{i})}/3.
Daniel@0 30
Daniel@0 31 % Handle input ......................
Daniel@0 32 if nargin==0, help centroid, return, end
Daniel@0 33 if nargin==1
Daniel@0 34 sz = size(x);
Daniel@0 35 if sz(1)==2 % Matrix 2 by n
Daniel@0 36 y = x(2,:); x = x(1,:);
Daniel@0 37 elseif sz(2)==2 % Matrix n by 2
Daniel@0 38 y = x(:,2); x = x(:,1);
Daniel@0 39 else
Daniel@0 40 y = imag(x);
Daniel@0 41 x = real(x);
Daniel@0 42 end
Daniel@0 43 end
Daniel@0 44
Daniel@0 45 % Make a polygon closed ..............
Daniel@0 46 x = [x(:); x(1)];
Daniel@0 47 y = [y(:); y(1)];
Daniel@0 48
Daniel@0 49 % Check length .......................
Daniel@0 50 l = length(x);
Daniel@0 51 if length(y)~=l
Daniel@0 52 error(' Vectors x and y must have the same length')
Daniel@0 53 end
Daniel@0 54
Daniel@0 55 % X-mean: Int{x^2*dy} ................
Daniel@0 56 del = y(2:l)-y(1:l-1);
Daniel@0 57 v = x(1:l-1).^2+x(2:l).^2+x(1:l-1).*x(2:l);
Daniel@0 58 x0 = v'*del;
Daniel@0 59
Daniel@0 60 % Y-mean: Int{y^2*dx} ................
Daniel@0 61 del = x(2:l)-x(1:l-1);
Daniel@0 62 v = y(1:l-1).^2+y(2:l).^2+y(1:l-1).*y(2:l);
Daniel@0 63 y0 = v'*del;
Daniel@0 64
Daniel@0 65 % Calculate area: Int{y*dx} ..........
Daniel@0 66 a = (y(1:l-1)+y(2:l))'*del;
Daniel@0 67 tol= 2*eps;
Daniel@0 68 if abs(a)<tol
Daniel@0 69 disp(' Warning: area of polygon is close to 0')
Daniel@0 70 a = a+sign(a)*tol+(~a)*tol;
Daniel@0 71 end
Daniel@0 72 % Multiplier
Daniel@0 73 a = 1/3/a;
Daniel@0 74
Daniel@0 75 % Divide by area .....................
Daniel@0 76 x0 = -x0*a;
Daniel@0 77 y0 = y0*a;
Daniel@0 78
Daniel@0 79 if nargout < 2, x0 = x0+i*y0; end