Hi, I wrote this code. It essentially calculates area of a polygon when given coordinates by shoelace formula. But it gives me wrong answer. Couldn't figure out why.
function AreaOfPolygon = gaussarea (xymatrice)[r,c]=size(xymatrice);x=xymatrice(:,1);y=xymatrice(:,2);calculation=0;x1=xymatrice(1,1);y1=xymatrice(1,2);xLast=xymatrice(r,1);yLast=xymatrice(r,2);for i=1:r if i==1 calculation=calculation+(x1*(y(i+1)-yLast)); elseif i==r calculation=calculation+(xLast*(y1-y(i-1))); else calculation=calculation+(x(i)*y(i+1)-y(i-1)); endendAreaOfPolygon=abs(calculation)/2end
Best Answer