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?

采纳的回答

Dyuman Joshi
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
result_1 = 26.1782
pgon = polyshape(x,y);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
result_2 = perimeter(pgon) %23.4138
result_2 = 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)))
result_3 = 23.4138

更多回答(1 个)

Torsten
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

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by