how to find area of polygon with coordinates in javascript

Solutions on MaxInterview for how to find area of polygon with coordinates in javascript by the best coders in the world

showing results for - "how to find area of polygon with coordinates in javascript"
Laura
12 Oct 2018
1Algorithm to find the area of a polygon
2If you know the coordinates of the vertices of a polygon, this algorithm can be used to find the area.
3
4Parameters
5X, 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.
6numPoints	The number of vertices
7Returns	the area of the polygon
8The algorithm, in JavaScript:
9
10function polygonArea(X, Y, numPoints) 
11{ 
12area = 0;   // Accumulates area 
13j = numPoints-1; 
14
15for (i=0; i<numPoints; i++)
16{ area +=  (X[j]+X[i]) * (Y[j]-Y[i]); 
17  j = i;  //j is previous vertex to i
18}
19  return area/2;
20}
21
22