フィルターのクリア

finding the area using Shoelace Polygon formula

24 ビュー (過去 30 日間)
Kratos
Kratos 2014 年 9 月 4 日
回答済み: Aykut Satici 2014 年 9 月 4 日
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.

回答 (1 件)

Aykut Satici
Aykut Satici 2014 年 9 月 4 日
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"

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by