increasingDirection static method

int increasingDirection(
  1. List<Coordinate> pts
)

Determines which orientation of the {@link Coordinate} array is (overall) increasing. In other words, determines which end of the array is "smaller" (using the standard ordering on {@link Coordinate}). Returns an integer indicating the increasing direction. If the sequence is a palindrome, it is defined to be oriented in a positive direction.

@param pts the array of Coordinates to test @return 1 if the array is smaller at the start or is a palindrome, -1 if smaller at the end

Implementation

static int increasingDirection(List<Coordinate> pts) {
  for (int i = 0; i < pts.length / 2; i++) {
    int j = pts.length - 1 - i;
    // skip equal points on both ends
    int comp = pts[i].compareTo(pts[j]);
    if (comp != 0) return comp;
  }
  // array must be a palindrome - defined to be in positive direction
  return 1;
}