{
const numRows = 16;
const numCols = 32;
const triangleSize = 20;
const horizontalSpacing = triangleSize * Math.sqrt(3) / 2;
const verticalSpacing = triangleSize / 3;
const width = numCols * horizontalSpacing;
const height = numRows * verticalSpacing * 3;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background", "white");
function getTrianglePoints(x, y, size, up) {
const h = size * Math.sqrt(3) / 2;
if (up) {
return [
[x, y - h / 2],
[x - size / 2, y + h / 2],
[x + size / 2, y + h / 2]
];
} else {
return [
[x, y + h / 2],
[x - size / 2, y - h / 2],
[x + size / 2, y - h / 2]
];
}
}
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const x = col * horizontalSpacing;
const y = row * verticalSpacing * 3;
const isUp = (row + col) % 2 === 0;
const points = getTrianglePoints(x, y, triangleSize, isUp);
const strokeColor = isUp ? "blue" : "red";
const fillColor = isUp ? "yellow" : "none";
svg.append("path")
.attr("d", `M${points[0][0]},${points[0][1]} L${points[1][0]},${points[1][1]} L${points[2][0]},${points[2][1]} Z`)
.attr("fill", fillColor)
.attr("stroke", strokeColor)
.attr("stroke-width", 1.2);
}
}
return svg.node();
}