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

114 ビュー (過去 30 日間)
Sim
Sim 2022 年 9 月 30 日
編集済み: Sim 2023 年 11 月 24 日
Is there any compact way to calculate the centroid of a boundary of a set of points ?

採用された回答

Jan
Jan 2022 年 9 月 30 日
編集済み: Jan 2022 年 9 月 30 日
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 件のコメント
Bruno Luong
Bruno Luong 2023 年 11 月 23 日
編集済み: Bruno Luong 2023 年 11 月 24 日
@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 2023 年 11 月 24 日
編集済み: Sim 2023 年 11 月 24 日
Thanks a lot @Bruno Luong!! :-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by