필터 지우기
필터 지우기

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 CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by