turnSprite function

void turnSprite(
  1. Painter p,
  2. List<String> rows,
  3. Map<String, int> pal,
  4. double x,
  5. double y,
  6. double ang,
  7. double px,
  8. double py, [
  9. double a = 1,
])

Draws a character sprite rotated by ang around pivot (px, py) at (x, y).

Implementation

void turnSprite(
  Painter p,
  List<String> rows,
  Map<String, int> pal,
  double x,
  double y,
  double ang,
  double px,
  double py, [
  double a = 1,
]) {
  final h = rows.length;
  final w = rows[0].length;
  final cs = math.cos(ang);
  final sn = math.sin(ang);
  final r = hypot(math.max(px, w - px), math.max(py, h - py)) + 1;
  p.shade(x - r, y - r, x + r, y + r, (lx, ly, out) {
    final dx = lx - x;
    final dy = ly - y;
    final i = (dx * cs + dy * sn + px).floor();
    final j = (-dx * sn + dy * cs + py).floor();
    if (i < 0 || j < 0 || i >= w || j >= h) return null;
    final c = pal[rows[j][i]];
    if (c == null) return null;
    out.c = c;
    out.a = a;
    return null;
  });
}