xxxxxxxxxx
let tileSize = 60;
let hexSize = tileSize / 2;
let rotationAngle = 0;
function setup() {
createCanvas(400, 400);
rectMode(CENTER);
noLoop();
}
function draw() {
background(255);
translate(tileSize * 0.75, tileSize * sqrt(3) / 2);
strokeWeight(2);
drawTessellation();
}
function drawTessellation() {
for (let x = 0; x < width; x += tileSize * 1.5) {
for (let y = 0; y < height; y += tileSize * sqrt(3)) {
drawHexagon(x, y);
drawConnections(x, y);
}
}
}
function drawHexagon(x, y) {
push();
translate(x, y);
rotate(rotationAngle);
fill(random(255), random(255), random(255)); // Random fill color
beginShape();
for (let i = 0; i < 6; i++) {
let angle = TWO_PI / 6 * i;
let xOffset = hexSize * cos(angle);
let yOffset = hexSize * sin(angle);
vertex(xOffset, yOffset);
}
endShape(CLOSE);
pop();
}
function drawConnections(x, y) {
stroke(0);
for (let i = 0; i < 6; i++) {
let angle = TWO_PI / 6 * i;
let xOffset = hexSize * cos(angle);
let yOffset = hexSize * sin(angle);
let newX = x + xOffset;
let newY = y + yOffset;
for (let j = 0; j < 6; j++) {
let nextAngle = TWO_PI / 6 * j;
let nextXOffset = hexSize * cos(nextAngle);
let nextYOffset = hexSize * sin(nextAngle);
let nextX = newX + nextXOffset;
let nextY = newY + nextYOffset;
line(newX, newY, nextX, nextY);
}
}
}
function keyPressed() {
if (keyCode === UP_ARROW) {
rotationAngle += PI / 6; // Rotate by 30 degrees (PI/6)
} else if (keyCode === DOWN_ARROW) {
rotationAngle -= PI / 6; // Rotate by -30 degrees (PI/6)
}
redraw();
}