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

90 vues (au cours des 30 derniers jours)
Sim
Sim le 30 Sep 2022
Modifié(e) : Sim le 24 Nov 2023
Is there any compact way to calculate the centroid of a boundary of a set of points ?

Réponse acceptée

Jan
Jan le 30 Sep 2022
Modifié(e) : Jan le 30 Sep 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 commentaires
Bruno Luong
Bruno Luong le 23 Nov 2023
Modifié(e) : Bruno Luong le 24 Nov 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 le 24 Nov 2023
Modifié(e) : Sim le 24 Nov 2023
Thanks a lot @Bruno Luong!! :-)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by