finding the area using Shoelace Polygon formula

24 views (last 30 days)
function area = shoelace (x,y)
n = length(x);
area = 0;
for i = 1 : n-1
area = area + (x(i) + x(i+1)) * (y(i) - y(i+1));
end
area = abs(area)/2;
end
How do I assign the values of x and y and get the answer on the command windows.

Answers (1)

Aykut Satici
Aykut Satici on 4 Sep 2014
I don't think that is quite the right formula.
Using the correct formula, your function would be very similar:
function area = shoelace(x,y)
n = length(x);
xp = [x; x(1)];
yp = [y; y(1)];
area = 0;
for i = 1:n
area = area + det([xp(i), xp(i+1); yp(i), yp(i+1)]);
end
area = 1/2*abs(area);
Then you can call this function with two vectors containing the vertices of any n-gon:
x = rand(100,1);
y = rand(100,1);
shoelace(x,y)
You can check the result by comparing it with MATLAB's built-in function "polyarea"

Categories

Find more on Elementary Polygons 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!