coin3d function

double coin3d(
  1. SpriteLayer l,
  2. Painter p,
  3. double x,
  4. double y,
  5. double r,
  6. double th,
  7. double theta,
  8. List<int> c, {
  9. double a = 1.0,
  10. bool white = false,
})

Implementation

double coin3d(
  SpriteLayer l,
  Painter p,
  double x,
  double y,
  double r,
  double th,
  double theta,
  List<int> c, {
  double a = 1.0,
  bool white = false,
}) {
  final ct = math.cos(theta);
  final st = math.sin(theta);
  final sg = ct < 0.0 ? -1.0 : 1.0;
  final act = ct.abs();
  final ast = st.abs();
  final xc = (th / 2.0) * st * sg;
  final nfx = st * sg;
  const lx = -0.55;
  const ly = 0.45;
  const lz = 0.7;
  final ln = math.sqrt(lx * lx + ly * ly + lz * lz);
  final lam = (nfx * lx + act * lz) / ln;
  final hx = lx / ln;
  final hz = lz / ln + 1.0;
  final hn = hypot(hx, hz);
  final base = clampInt((2.0 + lam * 3.2).round(), 2, 5);
  final xi = (x + 0.5).floor();
  final yi = (y + 0.5).floor();
  final b = (r + th).ceil() + 2;
  var flash = 0.0;
  l.begin(xi - b, yi - b);
  l.fill(xi - b, yi - b, xi + b, yi + b, (px, py) {
    final sx = px - xi;
    final sy = -(py - yi);
    if (sy.abs() > r) return 0;
    final s = math.sqrt(math.max(0.0, 1.0 - (sy / r) * (sy / r)));
    final hw = r * s * act;
    final xr = -(st < 0.0 ? -1.0 : 1.0) * s * r * ct;
    final rw = (th / 2.0) * ast;
    if (white &&
        ((sx - xc).abs() <= hw || (sx - xr).abs() <= rw + 0.05)) {
      return kWhite;
    }
    if ((sx - xc).abs() <= hw && act > 0.08) {
      final fx = (sx - xc) / act;
      final fy = sy;
      final d = hypot(fx, fy) / r;
      final tx = nfx + (fx / r) * 0.3 * act;
      final sp = math.pow(
        math.max(0.0, (tx * hx + act * hz) / hn / hypot(tx, act)),
        130,
      ).toDouble();
      flash = math.max(flash, sp);
      final lit = (-fx * 0.6 + fy * 0.8) / math.max(0.5, hypot(fx, fy));
      int k;
      if (d > 0.76) {
        k = base + (lit > 0.75 ? 2 : (lit > 0.3 ? 1 : (lit < -0.35 ? -1 : 0)));
      } else {
        final q = (fx.abs() + fy.abs()) / (r * 0.52);
        k = q >= 1.0
            ? base - 1
            : (q < 0.45
                ? base + 1
                : (fx <= 0.0 && fy >= 0.0
                    ? base + 1
                    : (fx > 0.0 && fy < 0.0 ? base - 2 : base)));
      }
      if (sp > 0.5) return sp > 0.8 ? kWhite : c[math.min(7, k + 2)];
      return c[clampInt(k, 2, 6)];
    }
    if ((sx - xr).abs() <= rw + 0.05) {
      final cphi = xr < 0.0 ? -s : s;
      final nx = cphi * ct;
      final ny = sy / r;
      final nz = -cphi * st;
      final lVal = (nx * lx + ny * ly + nz * lz) / ln;
      final groove = ((math.asin((sy / r).clamp(-1.0, 1.0)) + math.pi) * 9.0)
              .floor() %
          2;
      final edge = (sx - xr).abs() > rw - 0.6 &&
          (sx - xr).sign == (xc - xr).sign;
      return c[clampInt(
        (2.6 + lVal * 2.6).round() - groove + (edge ? 1 : 0),
        1,
        6,
      )];
    }
    return 0;
  });
  l.outline(c[1]).draw(p, 0, 0, a);
  return flash;
}