Calculate total length of perimeter

2 views (last 30 days)
Tim
Tim on 20 Mar 2014
Edited: Andrei Bobrov on 21 Mar 2014
Hey guys, I am quite new to MATLAB (2 weeks in) and have never coded before.
I have a task to produce a script that uses an nx2 array of coordinates, say x and y that correspond to the corners that form a boundary of a shape.
I want to code this so it calculates the total length of perimeter distance no matter what the input in the array is, for example a square could be:
Square =
00
30
33
03
So the perimeter would be 12, displayed to the user at the end. The last coordinate is the last corner before returning to the start. I can't use any built-in function that calculates co-ordinate distances or perimeters.
All advice even if newer than me would be great!
EDIT - I have done this so far, the input from the user:
clc
clear
n=input('How many Corners do you have?');
Corners = zeros (n,2);
for row=1:n
for column=1:2
usernumber=input('Enter the X and Y coordinates individually:');
Corners(row, column)=usernumber;
end
end
I am quite new to maths, so unsure on where to go from here.

Answers (2)

Walter Roberson
Walter Roberson on 21 Mar 2014
Euclidean distance is the standard sqrt((x2-x1)^2 + (y2-y1)^2)

Andrei Bobrov
Andrei Bobrov on 21 Mar 2014
Edited: Andrei Bobrov on 21 Mar 2014
xy=[0 0
3 0
3 3
0 3];
K = convhull(xy(:,1),xy(:,2));
out = sum(sqrt(sum(diff(xy(K,:)).^2,2)));

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!