p5(s => {
let gridSize = 100;
s.setup = function () {
s.createCanvas(400,400);
s.background(255);
s.stroke(0);
s.strokeWeight(2);
for (let x = 0; x < s.width; x += gridSize) {
for (let y = 0; y < s.height; y += gridSize) {
drawCell(x, y, gridSize, gridSize, s.random(5,15), s.random(Math.PI * 2));
}
}
}
function drawCell(x, y, w, h, gap, a) {
let xStart = x + s.random(w), yStart = y + s.random(h);
let slope = Math.tan(a);
let c = yStart - slope * xStart;
let l1, l2;
let i = 0;
while( l1 !== null || l2 !== null){
let x0 = x - w/2, x1 = x + w + w/2;
let y0 = slope * x0 + c + i * gap / Math.cos(a), y1 = slope * x1 + c + i * gap / Math.cos(a);
l1 = clipLine({x:x0,y:y0},{x:x1,y:y1}, {x:x, y:y}, w, h);
if (l1 !== null) s.line(l1[0].x, l1[0].y, l1[1].x, l1[1].y);
x0 = x - w/2, x1 = x + w + w/2;
y0 = slope * x0 + c - i * gap / Math.cos(a), y1 = slope * x1 + c - i * gap / Math.cos(a);
l2 = clipLine({x:x0,y:y0},{x:x1,y:y1}, {x:x, y:y}, w, h);
if (l2 !== null) s.line(l2[0].x, l2[0].y, l2[1].x, l2[1].y);
i++;
}
}
})