octant static method

int octant(
  1. double dx,
  2. double dy
)

Returns the octant of a directed line segment (specified as x and y displacements, which cannot both be 0).

Implementation

static int octant(double dx, double dy) {
  if (dx == 0.0 && dy == 0.0)
    throw ArgumentError("Cannot compute the octant for point ( $dx, $dy )");

  double adx = dx.abs();
  double ady = dy.abs();

  if (dx >= 0) {
    if (dy >= 0) {
      if (adx >= ady)
        return 0;
      else
        return 1;
    } else {
      // dy < 0
      if (adx >= ady)
        return 7;
      else
        return 6;
    }
  } else {
    // dx < 0
    if (dy >= 0) {
      if (adx >= ady)
        return 3;
      else
        return 2;
    } else {
      // dy < 0
      if (adx >= ady)
        return 4;
      else
        return 5;
    }
  }
}