util.dart 2.57 KB
Newer Older
1 2
part of sprites;

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

math.Random _random = new math.Random();

// Random methods

double randomDouble() {
  return _random.nextDouble();
}

double randomSignedDouble() {
  return _random.nextDouble() * 2.0 - 1.0;
}

int randomInt(int max) {
  return _random.nextInt(max);
}

// atan2

22
class _Atan2Constants {
23

24
  _Atan2Constants() {
25 26
    for (int i = 0; i <= size; i++) {
      double f = i.toDouble() / size.toDouble();
27 28 29 30 31 32 33 34
      ppy[i] = math.atan(f) * stretch / math.PI;
      ppx[i] = stretch * 0.5 - ppy[i];
      pny[i] = -ppy[i];
      pnx[i] = ppy[i] - stretch * 0.5;
      npy[i] = stretch - ppy[i];
      npx[i] = ppy[i] + stretch * 0.5;
      nny[i] = ppy[i] - stretch;
      nnx[i] = -stretch * 0.5 - ppy[i];
35 36 37
    }
  }

38 39
  static const int size = 1024;
  static const double stretch = math.PI;
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  static const int ezis = -size;

  final Float64List ppy = new Float64List(size + 1);
  final Float64List ppx = new Float64List(size + 1);
  final Float64List pny = new Float64List(size + 1);
  final Float64List pnx = new Float64List(size + 1);
  final Float64List npy = new Float64List(size + 1);
  final Float64List npx = new Float64List(size + 1);
  final Float64List nny = new Float64List(size + 1);
  final Float64List nnx = new Float64List(size + 1);
}

class GameMath {
  static final _Atan2Constants _atan2 = new _Atan2Constants();

  static double atan2(double y, double x) {
57 58 59
    if (x >= 0) {
      if (y >= 0) {
        if (x >= y)
60
          return _atan2.ppy[(_Atan2Constants.size * y / x + 0.5).toInt()];
61
        else
62
          return _atan2.ppx[(_Atan2Constants.size * x / y + 0.5).toInt()];
63 64
      } else {
        if (x >= -y)
65
          return _atan2.pny[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
66
        else
67
          return _atan2.pnx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
68 69 70 71
      }
    } else {
      if (y >= 0) {
        if (-x >= y)
72
          return _atan2.npy[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
73
        else
74
          return _atan2.npx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
75 76
      } else {
        if (x <= y)
77
          return _atan2.nny[(_Atan2Constants.size * y / x + 0.5).toInt()];
78
        else
79
          return _atan2.nnx[(_Atan2Constants.size * x / y + 0.5).toInt()];
80 81 82
      }
    }
  }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

  static double pointQuickDist(Point a, Point b) {
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    if (dx < 0.0) dx = -dx;
    if (dy < 0.0) dy = -dy;
    if (dx > dy) {
      return dx + dy/2.0;
    }
    else {
      return dy + dx/2.0;
    }
  }

  static double filter (double a, double b, double filterFactor) {
      return (a * (1-filterFactor)) + b * filterFactor;
  }
100
}