orientationIndex static method

int orientationIndex(
  1. Coordinate p1,
  2. Coordinate p2,
  3. Coordinate q
)

Returns the index of the direction of the point q relative to a vector specified by p1-p2.

@param p1 the origin point of the vector @param p2 the final point of the vector @param q the point to compute the direction to

@return 1 if q is counter-clockwise (left) from p1-p2 @return -1 if q is clockwise (right) from p1-p2 @return 0 if q is collinear with p1-p2

Implementation

static int orientationIndex(Coordinate p1, Coordinate p2, Coordinate q) {
  // fast filter for orientation index
  // avoids use of slow extended-precision arithmetic in many cases
  int index = orientationIndexFilter(p1, p2, q);
  if (index <= 1) return index;

  // normalize coordinates
  DD dx1 = DD.valueOf(p2.x).selfAdd(-p1.x);
  DD dy1 = DD.valueOf(p2.y).selfAdd(-p1.y);
  DD dx2 = DD.valueOf(q.x).selfAdd(-p2.x);
  DD dy2 = DD.valueOf(q.y).selfAdd(-p2.y);

  // sign of determinant - unrolled for performance
  return dx1
      .selfMultiplyDD(dy2)
      .selfSubtractDD(dy1.selfMultiplyDD(dx2))
      .signum();
}