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

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

Risposte (1)

pgon=polyshape(dataX,dataY);
perimeter(pgon)

5 Commenti

LR
LR il 9 Mag 2021
Modificato: LR il 9 Mag 2021
Thanks. However, I'm now getting the following message:
Unable to use a value of type polyshape as an index.
Error in filename (line 37) - perimeter(pgon)
perimeter(pgon)
You must clear the variable called perimeter (the one you've created with your previous code) from the workspace.
Sorry, I should have said when I asked I need to use the formula given to calculate the perimeter using a For loop. I'm trying to work out how to add the first coodinates again as the last coordinates, to create a complete shape. Any help would be appreciated.
Thank you
You could just do
dataX(end+1)=dataX(1)
and similarly for dataY.
LR
LR il 11 Mag 2021
Modificato: LR il 11 Mag 2021
How would I add that to my current code?
Thanks

Accedi per commentare.

Categorie

Tag

Richiesto:

LR
il 9 Mag 2021

Modificato:

LR
il 11 Mag 2021

Community Treasure Hunt

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

Start Hunting!

Translated by