xxxxxxxxxx
76
let cellRadius = 30;
let wallThickness = 5;
let cellSpacing = 2 * cellRadius * Math.cos(Math.PI / 6);
let dAngle = Math.PI / 3;
let lightAngle = -Math.PI / 6;
let lightCos = Math.cos(lightAngle - Math.PI / 2);
let lightSin = Math.sin(lightAngle - Math.PI / 2);
function setup()
{
createCanvas(800, 450);
background(5, 20, 60);
strokeWeight(wallThickness);
let columnSpacing = 1.5 * cellRadius;
let cellsPerColumn = height / (cellSpacing) + 1;
let columnCount = width / columnSpacing + 1;
let filledProbability = 0.15;
let filledPower = 8;
let emptyPower = 2;
for (let i = 0; i < columnCount; ++i)
{
let x0 = i * columnSpacing;
let y0 = cellSpacing * (i % 2) / 2;
CreateColumn(x0, y0, cellsPerColumn, filledProbability, filledPower, emptyPower);
}
}
function CreateColumn(x0, y0, n, filledProbability, filledPower, emptyPower)
{
for (let i = 0; i < n; ++i)
{
let y = y0 + i * cellSpacing;
CreateCell(x0, y, cellRadius, filledProbability, filledPower, emptyPower);
}
}
function CreateCell(x, y, r, filledProbability, filledPower, emptyPower)
{
let filled = random(0, 1) < filledProbability;
let a = filled ? 255 : CurvedRandom(10, 50, emptyPower, false);
let topStroke = color(250, 230, 5);
let bottomStroke = color(40, 35, 1);
let topFill = color(250, 220, 2, a);
let bottomFill = color(250, 220, 2, a / 4);
let interpolant = (x * lightCos + y * lightSin) / (width * lightCos + height * lightSin);
let strokeColor = lerpColor(topStroke, bottomStroke, interpolant);
let fillColor = lerpColor(topFill, bottomFill, interpolant);
stroke(strokeColor);
fill(fillColor);
beginShape();
for (let i = 0; i < 6; ++i)
{
let angle = dAngle * i;
let vx = x + cos(angle) * r;
let vy = y + sin(angle) * r;
vertex(vx, vy);
}
endShape(CLOSE);
}
function CurvedRandom(min, max, power, flip)
{
if (flip) return (max - min) * (1.0 - (random(0.0, 1.0) ** power)) + min;
return (max - min) * (random(0.0, 1.0) ** power) + min;
}