fractures function
Cracks racing out from (x, y) through a shape.
Implementation
List<List<(double x, double y, double d)>> fractures(
Rng r,
bool Function(double x, double y) inside,
double x,
double y,
int n, {
(double, double) step = const (1.6, 2.6),
int len = 18,
double fork = 0.16,
double? a0,
double spread = tau,
}) {
final out = <List<(double, double, double)>>[];
final startAngle = a0 ?? r.range(0, tau);
void walk(double x, double y, double d, double a, int len, int depth) {
final pts = <(double, double, double)>[(x, y, d)];
for (var s = 0; s < len; s++) {
a += r.range(-0.55, 0.55);
final st = r.range(step.$1, step.$2);
final nx = x + math.cos(a) * st;
final ny = y + math.sin(a) * st;
if (!inside(nx, ny)) break;
x = nx;
y = ny;
d += st;
pts.add((x, y, d));
if (depth < 2 && pts.length > 2 && r.chance(fork)) {
walk(
x,
y,
d,
a + r.sign() * r.range(0.6, 1.2),
(len * 0.5).ceil(),
depth + 1,
);
}
}
if (pts.length > 1) out.add(pts);
}
for (var i = 0; i < n; i++) {
walk(
x,
y,
0.0,
startAngle + (i / n) * spread + r.range(-0.3, 0.3) * math.min(1.0, spread / tau),
len,
0,
);
}
return out;
}