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

조회 수: 87 (최근 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 CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by