Public
Edited
May 24, 2023
1 fork
6 stars
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
entailments = {
const neighbors = [
[0, -1],
[1, -1],
[1, 0],
[0, 1],
[-1, 1],
[-1, 0]
];
const entailments_1 = [
[0, -6],
[1, 6],
[1, -2],
[2, 2]
];
const entailments = {};
for (let i = 1; i <= 6; i++) {
entailments[i] = entailments[-i] = entailments_1.map(
([position, shape]) => [
(position + i - 1) % 6,
(((Math.abs(shape) - 1) % 6) + 1) * shape < 0 ? -1 : 1
]
);
}
return entailments;
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
hex = new hc.Hex(storage.hatrack.hats[0].coordinates)
Insert cell
storage = new Object()
Insert cell
class Hatrack {
constructor(context, scale, width) {
this.hats = [];
this.context = context;
this.width = width;
this.scale = scale;
this.Hex = hc.defineHex({
dimensions: 1,
orientation: hc.Orientation.FLAT
});
this.grid = new hc.Grid(
this.Hex,
hc.rectangle({ width: 35, height: 35, start: [-6, -6] })
);
}
add_hat(coords, rotation, flip) {
this.hats.push(new Hat(coords, rotation, flip));
}
*add_and_add(radius = 6) {
const spiralTraverser = hc.spiral({ start: [0, 0], radius });
for (let f of this.grid.traverse(spiralTraverser)) {
yield new hc.Hex(f);
}
}
rect() {
const { width, scale } = this;
this.context.strokeRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);
}
draw(context = null) {
const { scale, width } = this;
if (context === null) {
context = this.context;
}
context.clearRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);

for (const hat of this.hats) {
context.save();
hat.draw(context);
context.restore();
}
return context.canvas;
}
collides(coords, rotation, flip) {
const { context, scale, width } = this;
context.clearRect(
-width / (2 * scale),
-width / (2 * scale),
width / scale,
width / scale
);

// Draw existing hats
this.draw(context);

// Get the image data of the existing hats
const existingHatsData = context.getImageData(
0,
0,
width * 2,
width * 2
).data;

// Draw the new hat
const newHat = new Hat(coords, rotation, flip);
newHat.draw(context);

// Get the image data after drawing the new hat
const updatedData = context.getImageData(0, 0, width * 2, width * 2).data;

let collisionsDetected = 0;
for (let i = 3; i < existingHatsData.length; i += 4) {
if (existingHatsData[i] > 0 && updatedData[i] > 129) {
collisionsDetected += 1;
}
}
console.log(collisionsDetected);
return collisionsDetected > 50;
}
}
Insert cell
class Hat {
constructor(hex = [0, 0, 0], rotation = 0, flip = false) {
this.neighbors = [];
this.children = [];
const [q, r, s] = hex;
if (q + r + s !== 0) {
throw new Error("Bad coordinates");
}
this.coordinates = hex;
const size = 2;
this.origin = [
size * ((3 / 2) * q),
size * ((Math.sqrt(3) / 2) * q + Math.sqrt(3) * r)
];

this.rotation = rotation;
this.flip = flip;
}
draw(context) {
context.save();
context.fillStyle = this.flip ? "blue" : "red";
context.translate(...this.origin);
context.rotate(this.rotation * Math.PI);
if (this.flip) {
context.scale(-1, 1);
}
context.beginPath();
for (const [x, y] of hat.geometry.coordinates[0]) {
context.lineTo(x, y);
}
context.closePath();
context.fill();
context.restore();
}
}
Insert cell
hc = require("honeycomb-grid")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more