Published
Edited
Apr 18, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function generatePermutations(n, r = n.length) {
if (r > n.length) return [];
else if (r == 1) return n.map((d) => [d]);
return n.flatMap((d) =>
generatePermutations(
n.filter((a) => a !== d),
r - 1
).map((item) => [d, ...item])
);
}
Insert cell
Insert cell
permutations = {
if (safe)
return generatePermutations(
d3.range(vertexCount * polygonCount + slotCount),
slotCount
);
}
Insert cell
Insert cell
filtered = permutations.filter((d) => {
let flag = true;
for (let slot = 0; slot < slotCount; slot++) {
if (
d3
.range(slotCount)
.filter((e) => e !== slot)
.includes(d[slot])
) {
flag = false;
}
}
return flag;
})
Insert cell
Insert cell
Insert cell
parsed = filtered.map((d, i) => {
return d3.range(slotCount).map((e) => {
return emptySlots.includes(parseInt(d[e]))
? {
poly: null,
orientation: null,
boardID: i,
polyID: null
}
: {
poly: parseInt((parseInt(d[e]) - slotCount) / vertexCount),
orientation: (parseInt(d[e]) - slotCount) % vertexCount,
boardID: i,
polyID: parseInt(d[e]) - slotCount
};
});
})
Insert cell
Insert cell
uniqued = parsed.filter((d) => {
const polyIDs = d.map((e) => e.poly).filter((e) => Number.isInteger(e));
return new Set(polyIDs).size == polyIDs.length;
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
colors = [
//these are used in order from top to bottom...
d3.interpolateHsl("black", "#00FFFF"), //to make custom gradients
d3.interpolateHsl("black", "#00FF00"),
d3.interpolateHsl("black", "#CCCCCC"),
d3.interpolateBlues, //or use built-in D3 gradients
d3.interpolatePurples,
d3.interpolateOranges,
d3.interpolateGreys,
d3.interpolateGreens,
d3.interpolateReds,
d3.interpolatePuRd,
d3.interpolateYlGn,
d3.interpolateYlOrBr,
d3.interpolatePuBu
]
.map((c) => {
return d3.range(vertexCount).map((d) => c(d / vertexCount));
})
.flat()
Insert cell
Insert cell
Insert cell
Insert cell
canvas = {
const context = DOM.context2d(canvasWidth, canvasHeight);
context.fillStyle = backgroundColor;
context.fillRect(0, 0, canvasWidth, canvasHeight);

boards.forEach((board) => {
const slots = board.slots.forEach((slot, offset) => {
const xPos = board.x + offset * pixel;
const yPos = board.y;
context.fillStyle = Number.isInteger(slot.poly)
? colors[slot.polyID]
: emptySlotColor;
context.fillRect(xPos, yPos, pixel, pixel);
});
});

return context.canvas;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more