Compact way to calculate the centroid of a boundary of a set of points

90 visualizaciones (últimos 30 días)
Sim
Sim el 30 de Sept. de 2022
Editada: Sim el 24 de Nov. de 2023
Is there any compact way to calculate the centroid of a boundary of a set of points ?

Respuesta aceptada

Jan
Jan el 30 de Sept. de 2022
Editada: Jan el 30 de Sept. de 2022
The centroid of the boundary is the mean value of the coordinates:
x = rand(40, 1).^2; % More points on the left
y = rand(40, 1).^2; % More points on the bottom
k = boundary(x, y);
c = mean([x(k), y(k)], 1); % Center of points of boundary
plot(x, y, 'r.');
hold('on');
plot(x(k), y(k), 'b');
plot(c(1), c(2), '*g');
You see, that this is not the center of mass, but the centroid of points. To get the center of mass:
[CoMx, CoMy] = centroid(polyshape(x(k), y(k))); % Center of Mass
plot(CoMx, CoMy, '*k');
% or:
[cx, cy] = CenterOfMass(x(k), y(k))
cx = 0.4679
cy = 0.4957
function [cx, cy] = CenterOfMass(x, y)
% This fails, if lines of the polygon intersect.
x = x(:);
y = y(:);
x_ = circshift(x, 1);
y_ = circshift(y, 1);
A = x .* y_ - x_ .* y;
As = sum(A) * 3;
cx = sum((x_ + x) .* A) / As;
cy = sum((y_ + y) .* A) / As;
end
  16 comentarios
Bruno Luong
Bruno Luong el 23 de Nov. de 2023
Editada: Bruno Luong el 24 de Nov. de 2023
@Sim The area of the polygonal (A in the book) is sum(A)/2 in Jan code.
So sum(A)*3 in Jan's code is equal to 6*area. They are the same. (EDIT typo)
Sim
Sim el 24 de Nov. de 2023
Editada: Sim el 24 de Nov. de 2023
Thanks a lot @Bruno Luong!! :-)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by