drawSpike function
void
drawSpike(])
A faceted stone spike standing on (x, base): h tall, w wide at the base, its tip lean px over.
Implementation
void drawSpike(
Painter p,
double x,
double base,
double h,
double w,
double lean,
SpikeLook sp,
List<int> rampColors, [
SpikeDrawOpts opts = const SpikeDrawOpts(),
]) {
if (h < 1.5) return;
final a = opts.a;
final white = opts.white;
final glint = opts.glint;
final clip = opts.clip;
final floorY = opts.floor;
double hw(double u, int side) {
var v = (w / 2.0) * math.pow(math.max(0.0, 1.0 - u), 0.85) + 0.3;
for (final n in sp.notches) {
if (n.side == side && u > n.u && u < n.u + 0.07) {
v -= n.d * (1.0 - u * 0.5);
}
}
return math.max(0.0, v);
}
final half = opts.half;
final y0 = (base - h).floor() - 2;
final nr = math.min(_rows, base.ceil() - y0 + 2);
for (var k = 0; k < nr; k++) {
final u = (base - (y0 + k + 0.5)) / h;
_ru[k] = u;
if (u < 0 || u > 1 || y0 + k + 0.5 > floorY) {
_rl[k] = double.infinity;
_rr[k] = double.negativeInfinity;
} else {
_rl[k] = x + lean * u - hw(u, -1);
_rr[k] = x + lean * u + hw(u, 1);
}
}
bool inside(int px, int py) {
final k = py - y0;
final fx = px + 0.5;
if (k < 0 || k >= nr || fx < _rl[k] || fx > _rr[k]) return false;
if (clip < 1 && _ru[k] > clip + (rand(px, 77) - 0.5) * 0.07) return false;
if (half == 0) return true;
final q = ((fx - _rl[k]) / math.max(0.5, _rr[k] - _rl[k])) * 2 - 1;
return half < 0 ? q < -0.18 : q >= -0.18;
}
final x0 = (x - w / 2 - lean.abs() - 2).floor();
final x1 = (x + w / 2 + lean.abs() + 2).ceil();
for (var py = (base - h).floor() - 1; py < base.ceil(); py++) {
for (var px = x0; px <= x1; px++) {
if (!inside(px, py)) {
if (py + 0.5 <= floorY &&
(inside(px - 1, py) ||
inside(px + 1, py) ||
inside(px, py + 1) ||
(inside(px, py - 1) && py < base - 1))) {
p.dot(px, py, rampColors[0], a);
}
continue;
}
if (white) {
p.dot(px, py, kWhite, a);
continue;
}
final k0 = py - y0;
final u = _ru[k0];
final c = x + lean * u;
final l = _rl[k0];
final r = _rr[k0];
final fx = px + 0.5;
var band = 0;
var st = false;
for (final s in sp.strata) {
final d = u * h - s * h - sp.slope * (fx - c);
if (d > 0) band++;
if (d.abs() < 0.5) st = true;
}
final jog = (rand(band, sp.seed) - 0.5) * 0.3;
final q = ((fx - l) / math.max(0.5, r - l)) * 2 - 1;
final qr = -0.18 + jog;
int idx;
if (u > 0.93) {
idx = q < qr + 0.3 ? 7 : 5;
} else if ((q - qr).abs() < math.max(0.09, 0.55 / math.max(1.0, r - l))) {
idx = 7;
} else if (q < qr) {
idx = fx - l < 1 ? 7 : (q < -0.5 + jog ? 6 : 5);
} else {
idx = r - fx < 1 ? 3 : (q < 0.38 + jog ? 4 : 2);
}
if (st && idx < 7) idx -= 1;
final fleck = rand(px * 7 + 3, py * 13 + 5);
if (fleck < 0.035) {
idx = q < qr ? 7 : idx - 1;
} else if (fleck > 0.965) {
idx -= 1;
}
if (glint >= 0 && q < qr + 0.1) {
final g = (u - glint).abs();
if (g < 0.035) {
idx = 8;
} else if (g < 0.07) {
idx = math.max(idx, 7);
}
}
p.dot(
px,
py,
idx >= 8 ? kWhite : rampColors[clampInt(idx, 0, rampColors.length - 1)],
a,
);
}
}
if (opts.cracks != null && opts.crack > 0) {
for (final path in opts.cracks!) {
final m = math.max(1, (path.length * opts.crack).floor());
for (var k = 1; k < m; k++) {
final (u0, v0) = path[k - 1];
final (u1, v1) = path[k];
final ax = x + lean * u0 + v0 * hw(u0, v0 < 0 ? -1 : 1);
final ay = base - u0 * h;
final bx = x + lean * u1 + v1 * hw(u1, v1 < 0 ? -1 : 1);
final by = base - u1 * h;
if (u1 > clip) break;
if (!inside(ax.floor(), ay.floor()) || !inside(bx.floor(), by.floor())) {
continue;
}
final hot = opts.heat;
final gl = opts.glowRamp;
if (hot > 0.05 && gl != null) {
p.line(ax + 1, ay, bx + 1, by, rampColors[0], a);
p.line(
ax,
ay,
bx,
by,
hot > 0.7
? gl[4]
: hot > 0.4
? gl[3]
: hot > 0.2
? gl[2]
: gl[1],
a,
);
if (k % 2 == 0) p.glow(bx, by, 7, gl[2], 0.35 * hot);
} else {
p.line(ax, ay, bx, by, rampColors[0], a);
p.dot(bx + 1, by - 1, rampColors[6], a * 0.7);
}
}
}
}
}