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!