calculate the perimeter of a polygon arranged in space
5 ビュー (過去 30 日間)
古いコメントを表示
Hi. I need to calculate the perimeter of the geometry in the figure.
load plane_new
figure
x = plane_new(:,1);
y = plane_new(:,2);
plot(x,y,'.-r')
line(x(k),y(k))
Initially I calculated the perimeter, as the sum of each segment, in this way:
r1 = height(plane_new);
union_dist = {};
for k = 1:(r1-1)
d1 = pdist(plane_new(k:k+1,:),'euclidean');
union_dist = [union_dist;{d1}];
end
union_dist = cell2mat(union_dist);
last_first = [plane_new(r1,:); plane_new(1,:)];
d_last_first = pdist(last_first(1:2,:),'euclidean');
union_dist(r1) = d_last_first;
result_1 = sum(union_dist); %26.1782
On the internet I saw instead that the function 'perimeter' can be used:
dataX = plane_new(:,1);
dataY = plane_new(:,2);
pgon = polyshape(dataX,dataY);
result_2 = perimeter(pgon); %23.4138
What is the correct solution?
0 件のコメント
採用された回答
Dyuman Joshi
2024 年 1 月 19 日
編集済み: Dyuman Joshi
2024 年 1 月 19 日
I am not sure what the idea behind that method is, but it does not give the correct result -
load plane_new
figure
x = plane_new(:,1);
y = plane_new(:,2);
plot(x,y,'.-r')
r1 = height(plane_new);
union_dist = {};
for k = 1:(r1-1)
d1 = pdist(plane_new(k:k+1,:),'euclidean');
union_dist = [union_dist;{d1}];
end
union_dist = cell2mat(union_dist);
last_first = [plane_new(r1,:); plane_new(1,:)];
d_last_first = pdist(last_first(1:2,:),'euclidean');
union_dist(r1) = d_last_first;
result_1 = sum(union_dist) %26.1782
pgon = polyshape(x,y);
result_2 = perimeter(pgon) %23.4138
%Calculating pair-wise distance and adding it
X = [x; x(1)];
Y = [y; y(1)];
result_3 = sum(sqrt(sum([diff(X).^2 diff(Y).^2],2)))
0 件のコメント
その他の回答 (1 件)
Torsten
2024 年 1 月 19 日
編集済み: Torsten
2024 年 1 月 19 日
After reading the x/y data, sum up the lengths of the line segments that constitute the circumference:
perimeter = 0;
for i = 1:numel(x)-1
perimeter = perimeter + sqrt((x(i+1)-x(i))^2+(y(i+1)-y(i))^2);
end
perimeter = perimeter + sqrt((x(1)-x(end))^2+(y(1)-y(end))^2);
perimeter
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!