简体   繁体   中英

Calculating Area of a Polygon

I'm allowing a user to draw a polygon, and I wanted to see if there's a way to calculate the area, based off the lat/lng paths.

Has anyone ever done this before? I'd like to account for the spherical area of the earth as well, if possible.

yeah, this is really just a math question, but I'm happy to do a bit of help.

So, unless you have a regular polygon to work with (which it sounds like you don't) and you need to take curvature into account you are going to need to solve the double integral of a parametric equation describing the polygon drawn on a sphere.

or you can use this method http://www.mathsisfun.com/geometry/area-irregular-polygons.html and ignore the spherical nature of the earth...

You may just want to use the google api. Calculating area of a polygon drawn on google map

You can do it (without the curvatures). I once faced this same problem and came to this geometric-based formula.

Given a closed polygon of N 2D points P[i]:= (X[i], Y[i]), i from 1 to N and P[1] = P[N] (it is closed) then the area can be calculated as follows:

Area = Abs(Sum(Z value of cross product (P[i]-P[1]) X (P[i+1]-P[1])))/2
with i ranging from 2 to N-1

As pseudo-code:

Area:= 0
for i:= 2 to N-1 do
  Area:= Area + ((X[i]-X[1]) * (Y[i+1]-Y[1]) - (Y[i]-Y[1]) * (X[i+1]-X[1]))
Area:= Abs(Area)/2

This code works for convex of not convex polygons, simple or with sub-polygons, on 2D.

If points where in 3D, you will use only X and Y (as setting Z:= 0) to get the area of the proyection of the 3D poligon on the 2D XY plane.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM