ofRingSigned static method

double ofRingSigned(
  1. List<Coordinate> ring
)

Computes the signed area for a ring. The signed area is positive if the ring is oriented CW, negative if the ring is oriented CCW, and zero if the ring is degenerate or flat.

@param ring the coordinates forming the ring @return the signed area of the ring

Implementation

static double ofRingSigned(List<Coordinate> ring) {
  if (ring.length < 3) return 0.0;
  double sum = 0.0;
  /**
   * Based on the Shoelace formula.
   * http://en.wikipedia.org/wiki/Shoelace_formula
   */
  double x0 = ring[0].x;
  for (int i = 1; i < ring.length - 1; i++) {
    double x = ring[i].x - x0;
    double y1 = ring[i + 1].y;
    double y2 = ring[i - 1].y;
    sum += x * (y2 - y1);
  }
  return sum / 2.0;
}