Calculate perimeter of a shape using x,y coordinates input to an array

20 vues (au cours des 30 derniers jours)
LR
LR le 9 Mai 2021
Modifié(e) : LR le 11 Mai 2021
How can I find the perimeter of a shape using x,y coordinates input to an array. The code below adds up the number of sides of the shape instead of the length of the sides which is what I need. The equation for calculating the distance between each pair of x,y points is: sqrt((dataX(i)+1)-dataX(i))^2+((dataY(i)+1)-dataY(i))^2
Many thanks
numberOfCoordinates=input('Please input the number of coordinates:');
for i=1:1:numberOfCoordinates
dataX(i)=input('Please enter the X co-ordinate:')
dataY(i)=input('Please enter the Y co-ordinate:')
end
end
perimeter=0;
for i=1:1:numberOfCoordinates
x=((dataX(i)+1)-dataX(i))^2
y=((dataY(i)+1)-dataY(i))^2
answer=sqrt(sum(x,y))
perimeter=perimeter+answer
end

Réponses (1)

Matt J
Matt J le 9 Mai 2021
pgon=polyshape(dataX,dataY);
perimeter(pgon)
  5 commentaires
Matt J
Matt J le 10 Mai 2021
You could just do
dataX(end+1)=dataX(1)
and similarly for dataY.
LR
LR le 11 Mai 2021
Modifié(e) : LR le 11 Mai 2021
How would I add that to my current code?
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by