PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ ptarray_signed_area()

double ptarray_signed_area ( const POINTARRAY pa)

Returns the area in cartesian units.

Area is negative if ring is oriented CCW, positive if it is oriented CW and zero if the ring is degenerate or flat. http://en.wikipedia.org/wiki/Shoelace_formula

Definition at line 990 of file ptarray.c.

References getPoint2d_cp(), POINTARRAY::npoints, pixval::x, POINT2D::x, and POINT2D::y.

Referenced by lwgeom_subdivide_recursive(), lwpoly_area(), ptarray_isccw(), and test_ptarray_signed_area().

991 {
992  const POINT2D *P1;
993  const POINT2D *P2;
994  const POINT2D *P3;
995  double sum = 0.0;
996  double x0, x, y1, y2;
997  uint32_t i;
998 
999  if (! pa || pa->npoints < 3 )
1000  return 0.0;
1001 
1002  P1 = getPoint2d_cp(pa, 0);
1003  P2 = getPoint2d_cp(pa, 1);
1004  x0 = P1->x;
1005  for ( i = 2; i < pa->npoints; i++ )
1006  {
1007  P3 = getPoint2d_cp(pa, i);
1008  x = P2->x - x0;
1009  y1 = P3->y;
1010  y2 = P1->y;
1011  sum += x * (y2-y1);
1012 
1013  /* Move forwards! */
1014  P1 = P2;
1015  P2 = P3;
1016  }
1017  return sum / 2.0;
1018 }
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
double y
Definition: liblwgeom.h:330
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: