Javascript - Function to calculate polygon area, Algorithm to calculate polygon area
Function to calculate polygon area
Algorithm to calculate polygon area in Javascriptfunction polygonArea(X, Y, numPoints) { area = 0; // Accumulates area in the loop j = numPoints-1; // The last vertex is the 'previous' one to the first for (i=0; i<numPoints; i++) { area = area + (X[j]+X[i]) * (Y[j]-Y[i]); j = i; //j is previous vertex to i } return area/2; }Where,
X, Y = Arrays of the x and y coordinates of the vertices, traced in a clockwise direction, starting at any vertex. If you trace them counterclockwise, the result will be correct but have a negative sign.
numPoints = The number of vertices
Returns = the area of the polygon
The topic on Javascript - Function to calculate polygon area is posted by - Math
Hope you have enjoyed, Javascript - Function to calculate polygon areaThanks for your time