function drawPeano(cb, x, y, w, h, direction, randomize) {
if (w <= 2 && h <= 2) {
tiles[direction][w][h](cb, x, y);
return;
}
const { LT, TR, BL, RB } = directions;
const { round } = Math;
const w0 = round(w / 3);
const w1 = round((w * 2) / 3) - w0;
const w2 = w - w0 - w1;
const h0 = round(h / 3);
const h1 = round((h * 2) / 3) - h0;
const h2 = h - h0 - h1;
const x1 = x + w0;
const x2 = x + w0 + w1;
const x3 = x + w - 1;
const y1 = y + h0;
const y2 = y + h0 + h1;
const y3 = y + h - 1;
const splitHorizontal =
randomize && w === h ? Math.random() > 0.5 : h > 2 && h > w;
if (splitHorizontal) {
let d0, d1, d2;
if (direction === LT) {
cb.line(x3, y1, x3, y1 - 1, true);
cb.line(x, y2, x, y2 - 1, true);
d0 = LT;
d1 = TR;
d2 = LT;
} else if (direction === TR) {
cb.line(x, y1, x, y1 - 1, true);
cb.line(x3, y2, x3, y2 - 1, true);
d0 = TR;
d1 = LT;
d2 = TR;
} else if (direction === BL) {
cb.line(x, y1 - 1, x, y1, true);
cb.line(x3, y2 - 1, x3, y2, true);
d0 = BL;
d1 = RB;
d2 = BL;
} else {
cb.line(x3, y1 - 1, x3, y1, true);
cb.line(x, y2 - 1, x, y2, true);
d0 = RB;
d1 = BL;
d2 = RB;
}
drawPeano(cb, x, y, w, h0, d0, randomize);
drawPeano(cb, x, y1, w, h1, d1, randomize);
drawPeano(cb, x, y2, w, h2, d2, randomize);
} else {
let d0, d1, d2;
if (direction === LT) {
cb.line(x1, y3, x1 - 1, y3, true);
cb.line(x2, y, x2 - 1, y, true);
d0 = LT;
d1 = BL;
d2 = LT;
} else if (direction === TR) {
cb.line(x1 - 1, y, x1, y, true);
cb.line(x2 - 1, y3, x2, y3, true);
d0 = TR;
d1 = RB;
d2 = TR;
} else if (direction === BL) {
cb.line(x1, y, x1 - 1, y, true);
cb.line(x2, y3, x2 - 1, y3, true);
d0 = BL;
d1 = LT;
d2 = BL;
} else {
cb.line(x1 - 1, y3, x1, y3, true);
cb.line(x2 - 1, y, x2, y, true);
d0 = RB;
d1 = TR;
d2 = RB;
}
drawPeano(cb, x, y, w0, h, d0, randomize);
drawPeano(cb, x1, y, w1, h, d1, randomize);
drawPeano(cb, x2, y, w2, h, d2, randomize);
}
}