I'm trying to find the area and the center of a polygon, made of a set of coordinates, x & y Here is the code I have so far, the results it's giving me are way off, can anyone see the problem?
%Load the polygon points file
load Drawing1.txt;
x = Drawing1(:,1);
y = Drawing1(:,2);
%Calculate the number of rows
Nrows = numel(textread('Drawing1.txt','%1c%*[^\n]'));
%Set initial values
A = 0;
Cx = 0;
Cy = 0;
for k=0:(Nrows-1);
if k==0 %End point is the same as point 0
i=Nrows; j=1; else %Otherwise use the current point
i=k; j=k+1; end %Calculate centre points of polygons
A=A+(((x(i)*y(j))-(x(j)*y(i)))*cosd((y(i)+y(j))/2)); Cx=Cx+((x(i)+x(j))*((x(i)*y(j))-(x(j)*y(i)))); Cy=Cy+((y(i)+y(j))*((x(i)*y(j))-(x(j)*y(i))));end
%Apply terms outside summation
A=0.5*A
Cx = (1/(6*A))*Cx
Cy = (1/(6*A))*Cy
Thanks!
Best Answer