Home > utilities > centroid.m

centroid

PURPOSE ^

x and y must be non-scalar vectors of equal sizes defining a polygonal

SYNOPSIS ^

function [x0,y0,a] = centroid(x,y)

DESCRIPTION ^

 x and y must be non-scalar vectors of equal sizes defining a polygonal
 region in counterclockwise sequence. a is returned with the polygon's
 area, and (x0,y0) with the coordinates of the polygon's centroid. Note:
 with clockwise order, x0 and y0 are still correct but a is the negative
 of the area.
 
 RAS - 1/17/05

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [x0,y0,a] = centroid(x,y)
0002 % x and y must be non-scalar vectors of equal sizes defining a polygonal
0003 % region in counterclockwise sequence. a is returned with the polygon's
0004 % area, and (x0,y0) with the coordinates of the polygon's centroid. Note:
0005 % with clockwise order, x0 and y0 are still correct but a is the negative
0006 % of the area.
0007 %
0008 % RAS - 1/17/05
0009 
0010 [m1,n1] = size(x); [m2,n2] = size(y);
0011 n = max(m1,n1);
0012 if (m1 ~= m2 && n1 ~= n2) || min(m1,n1) ~= 1 || n <= 1
0013     error('Args must be equal-sized non-scalar vectors')
0014 end
0015 x = x(:); y = y(:);
0016 x2 = [x(2:n);x(1)];
0017 y2 = [y(2:n);y(1)];
0018 a = 1/2*sum (x.*y2-x2.*y);
0019 x0 = 1/6*sum((x.*y2-x2.*y).*(x+x2))/a;
0020 y0 = 1/6*sum((x.*y2-x2.*y).*(y+y2))/a;

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005