Filter löschen
Filter löschen

finding the area using Shoelace Polygon formula

23 Ansichten (letzte 30 Tage)
Kratos
Kratos am 4 Sep. 2014
Beantwortet: Aykut Satici am 4 Sep. 2014
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.

Antworten (1)

Aykut Satici
Aykut Satici am 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"

Kategorien

Mehr zu Elementary Polygons finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by