ofRingSignedSeq static method

double ofRingSignedSeq(
  1. CoordinateSequence 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
  • 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 ofRingSignedSeq(CoordinateSequence ring) {
  int n = ring.size();
  if (n < 3) return 0.0;
  /**
   * Based on the Shoelace formula.
   * http://en.wikipedia.org/wiki/Shoelace_formula
   */
  Coordinate p0 = new Coordinate.empty2D();
  Coordinate p1 = new Coordinate.empty2D();
  Coordinate p2 = new Coordinate.empty2D();
  ring.getCoordinateInto(0, p1);
  ring.getCoordinateInto(1, p2);
  double x0 = p1.x;
  p2.x -= x0;
  double sum = 0.0;
  for (int i = 1; i < n - 1; i++) {
    p0.y = p1.y;
    p1.x = p2.x;
    p1.y = p2.y;
    ring.getCoordinateInto(i + 1, p2);
    p2.x -= x0;
    sum += p1.x * (p0.y - p2.y);
  }
  return sum / 2.0;
}