getDirectionFromAngle static method

Direction getDirectionFromAngle(
  1. double angle
)

Implementation

static Direction getDirectionFromAngle(double angle) {
  double degrees = angle * 180 / pi;

  if (degrees > -22.5 && degrees <= 22.5) {
    return Direction.right;
  }

  if (degrees > 22.5 && degrees <= 67.5) {
    return Direction.downRight;
  }

  if (degrees > 67.5 && degrees <= 112.5) {
    return Direction.down;
  }

  if (degrees > 112.5 && degrees <= 157.5) {
    return Direction.downLeft;
  }

  if ((degrees > 157.5 && degrees <= 180) ||
      (degrees >= -180 && degrees <= -157.5)) {
    return Direction.left;
  }

  if (degrees > -157.5 && degrees <= -112.5) {
    return Direction.upLeft;
  }

  if (degrees > -112.5 && degrees <= -67.5) {
    return Direction.up;
  }

  if (degrees > -67.5 && degrees <= -22.5) {
    return Direction.upRight;
  }
  return Direction.left;
}