isBetween method

bool isBetween(
  1. Coordinate c1,
  2. Coordinate c2,
  3. Coordinate c3
)

@return whether the three coordinates are collinear and c2 lies between c1 and c3 inclusive

Implementation

bool isBetween(Coordinate c1, Coordinate c2, Coordinate c3) {
  if (Orientation.index(c1, c2, c3) != 0) {
    return false;
  }
  if (c1.x != c3.x) {
    if (c1.x <= c2.x && c2.x <= c3.x) {
      return true;
    }
    if (c3.x <= c2.x && c2.x <= c1.x) {
      return true;
    }
  }
  if (c1.y != c3.y) {
    if (c1.y <= c2.y && c2.y <= c3.y) {
      return true;
    }
    if (c3.y <= c2.y && c2.y <= c1.y) {
      return true;
    }
  }
  return false;
}