static void test_ptarray_signed_area() { LWLINE *line; double area; /* parallelogram */ line = lwgeom_as_lwline(lwgeom_from_text("LINESTRING(0 0,1 1, 2 1, 1 0, 0 0)")); area = ptarray_signed_area(line->points); CU_ASSERT_DOUBLE_EQUAL(area, 1.0, 0.0000001); lwline_free(line); /* square */ line = lwgeom_as_lwline(lwgeom_from_text("LINESTRING(0 0,0 2, 2 2, 2 0, 0 0)")); area = ptarray_signed_area(line->points); CU_ASSERT_DOUBLE_EQUAL(area, 4.0, 0.0000001); lwline_free(line); /* square backwares*/ line = lwgeom_as_lwline(lwgeom_from_text("LINESTRING(0 0,2 0, 2 2, 0 2, 0 0)")); area = ptarray_signed_area(line->points); //printf("%g\n",area); CU_ASSERT_DOUBLE_EQUAL(area, -4.0, 0.0000001); lwline_free(line); }
/** * Find the area of the outer ring - sum (area of inner rings). */ double lwpoly_area(const LWPOLY *poly) { double poly_area = 0.0; int i; if ( ! poly ) lwerror("lwpoly_area called with null polygon pointer!"); for ( i=0; i < poly->nrings; i++ ) { POINTARRAY *ring = poly->rings[i]; double ringarea = 0.0; /* Empty or messed-up ring. */ if ( ring->npoints < 3 ) continue; ringarea = fabs(ptarray_signed_area(ring)); if ( i == 0 ) /* Outer ring, positive area! */ poly_area += ringarea; else /* Inner ring, negative area! */ poly_area -= ringarea; } return poly_area; }
int ptarray_isccw(const POINTARRAY *pa) { double area = 0; area = ptarray_signed_area(pa); if ( area > 0 ) return LW_FALSE; else return LW_TRUE; }