calculate the perimeter of a polygon arranged in space
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alberto Acri
le 19 Jan 2024
Modifié(e) : Dyuman Joshi
le 19 Jan 2024
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 commentaires
Réponse acceptée
Dyuman Joshi
le 19 Jan 2024
Modifié(e) : Dyuman Joshi
le 19 Jan 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
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 commentaires
Plus de réponses (1)
Torsten
le 19 Jan 2024
Modifié(e) : Torsten
le 19 Jan 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
0 commentaires
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!