How to calculate the area of ​​a non-convex polygon?

Assuming the polygon is not self-intersecting, what would be the most efficient way to do this? A polygon has N vertices. I know that it can be calculated using coordinates, but is there any other general way?

+4
source share
4 answers

signed area , A(T)the triangle T = ((x1, y1), (x2, y2), (x3, y3))defined in 1/2 times the determinant of the following matrix:

|x1 y1 1|
|x2 y2 1|
|x3 y3 1|

Qualifier -y1*x2 + x1*y2 + y1*x3 - y2*x3 - x1*y3 + x2*y3.

Given the polygon (convex or concave) defined by the vertices p[0], p[1], ..., p[N - 1], you can calculate the region of the polygon as follows.

area = 0
for i in [0, N - 2]:
    area += A((0, 0), p[i], p[i + 1])
area += A((0, 0), p[N - 1], p[0])
area = abs(area)

, A((0, 0), p, q) 0.5 * (-p.y*q.x + p.x*q.y). , 0.5 :

area = 0
for i in [0, N - 2]:
    area += -p[i].y * p[i+1].x + p[i].x * p[i+1].y
area += -p[N-1].y * p[0].x + p[N-1].x * p[0].y
area = 0.5 * abs(area)

, . , , .

+3
  • 3 .
  • .
  • .
  • , , . , .
  • , .

: , @NicolasMiari, , , , .

+2

, , - , . , , ( , , , ). .

:

function polygonArea(Xcoords, Ycoords) { 
  numPoints = len(Xcoords)
  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 +  (Xcoords[j]+Xcoords[i]) * (Ycoords[j]-Ycoords[i]); 
      j = i;  //j is previous vertex to i
    }
  return area/2;
}

Xcoords Ycoords - , Xcoords X Ycoords Y.

.

, Math Open Ref

, , , , .

+1

" " , , , "" ( ).

, :

enter image description here

However, this can always be done (I can not prove it mathematically right now, but you must trust me). You just need to go through the vertices of the polygons and perform some inclusion tests until you find a suitable triple.

Source: I once applied the triangulation of arbitrary disjoint polygons based on what I read in Computational Geometry in C by Joseph O'Rourke .

0
source

Source: https://habr.com/ru/post/1620476/


All Articles