util.dart 4.45 KB
Newer Older
1
part of flutter_sprites;
2

3 4 5 6 7

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

// Random methods

8
/// Returns a random [double] in the range of 0.0 to 1.0.
9 10 11 12
double randomDouble() {
  return _random.nextDouble();
}

13
/// Returns a random [double] in the range of -1.0 to 1.0.
14 15 16 17
double randomSignedDouble() {
  return _random.nextDouble() * 2.0 - 1.0;
}

18
/// Returns a random [int] from 0 to max - 1.
19 20 21 22
int randomInt(int max) {
  return _random.nextInt(max);
}

23
/// Returns either [true] or [false] in a most random fashion.
Viktor Lidholt's avatar
Viktor Lidholt committed
24 25 26 27
bool randomBool() {
  return _random.nextDouble() < 0.5;
}

28 29
// atan2

30
class _Atan2Constants {
31

32
  _Atan2Constants() {
33 34
    for (int i = 0; i <= size; i++) {
      double f = i.toDouble() / size.toDouble();
35 36 37 38 39 40 41 42
      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];
43 44 45
    }
  }

46 47
  static const int size = 1024;
  static const double stretch = math.PI;
48

49 50 51 52 53 54 55 56 57 58 59 60
  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);
}

61 62
/// Provides convenience methods for calculations often carried out in graphics.
/// Some of the methods are returning approximations.
63 64 65
class GameMath {
  static final _Atan2Constants _atan2 = new _Atan2Constants();

66 67
  /// Returns the angle of two vector components. The result is less acurate
  /// than the standard atan2 function in the math package.
68
  static double atan2(double y, double x) {
69 70 71
    if (x >= 0) {
      if (y >= 0) {
        if (x >= y)
72
          return _atan2.ppy[(_Atan2Constants.size * y / x + 0.5).toInt()];
73
        else
74
          return _atan2.ppx[(_Atan2Constants.size * x / y + 0.5).toInt()];
75 76
      } else {
        if (x >= -y)
77
          return _atan2.pny[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
78
        else
79
          return _atan2.pnx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
80 81 82 83
      }
    } else {
      if (y >= 0) {
        if (-x >= y)
84
          return _atan2.npy[(_Atan2Constants.ezis * y / x + 0.5).toInt()];
85
        else
86
          return _atan2.npx[(_Atan2Constants.ezis * x / y + 0.5).toInt()];
87 88
      } else {
        if (x <= y)
89
          return _atan2.nny[(_Atan2Constants.size * y / x + 0.5).toInt()];
90
        else
91
          return _atan2.nnx[(_Atan2Constants.size * x / y + 0.5).toInt()];
92 93 94
      }
    }
  }
95

96 97 98
  /// Approximates the distance between two points. The returned value can be
  /// up to 6% wrong in the worst case.
  static double distanceBetweenPoints(Point a, Point b) {
99 100 101 102 103 104 105 106 107 108 109 110
    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;
    }
  }

111 112
  /// Interpolates a [double] between [a] and [b] according to the
  /// [filterFactor], which should be in the range of 0.0 to 1.0.
113 114 115
  static double filter (double a, double b, double filterFactor) {
      return (a * (1-filterFactor)) + b * filterFactor;
  }
116

117 118
  /// Interpolates a [Point] between [a] and [b] according to the
  /// [filterFactor], which should be in the range of 0.0 to 1.0.
119 120 121
  static Point filterPoint(Point a, Point b, double filterFactor) {
    return new Point(filter(a.x, b.x, filterFactor), filter(a.y, b.y, filterFactor));
  }
122

123 124 125
  /// Returns the intersection between two line segmentss defined by p0, p1 and
  /// q0, q1. If the lines are not intersecting null is returned.
  static Point lineIntersection(Point p0, Point p1, Point q0, Point q1) {
126 127
    double epsilon = 1e-10;

128 129 130
    Vector2 r = new Vector2(p1.x - p0.x, p1.y - p0.y);
    Vector2 s = new Vector2(q1.x - q0.x, q1.y - q0.y);
    Vector2 qp = new Vector2(q0.x - p0.x, q0.y - p0.y);
131 132 133 134 135 136 137 138 139 140 141 142

    double rxs = cross2(r, s);

    if (rxs.abs() < epsilon) {
      // The lines are linear or collinear
      return null;
    }

    double t = cross2(qp, s) / rxs;
    double u = cross2(qp, r) / rxs;

    if ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0)) {
143
      return new Point(p0.x + t * r.x, p0.y + t * r.y);
144 145 146 147 148
    }

    // No intersection between the lines
    return null;
  }
149
}