calculate the perimeter of a polygon arranged in space

4 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 19 Gen 2024
Modificato: Dyuman Joshi il 19 Gen 2024
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

Più risposte (1)

Torsten
Torsten il 19 Gen 2024
Modificato: Torsten il 19 Gen 2024
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

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by