xxxxxxxxxx
92
let place;
let moves = {};
let colors = [];
let canvasSize = 800
let hexR = 40
let tilingOffset = -canvasSize / 5
function setup() {
createCanvas(canvasSize, canvasSize);
background(150);
noStroke();
colors[0] = color(0, 200, 220);
colors[1] = color(161, 50, 200)
let hexApo = hexR / 2 * sqrt(3);
let triangleR = (hexR / 3) * sqrt(3);
moves = {
downTria: createVector(0, triangleR * 2),
downHex: createVector(0, hexApo * 2),
right: createVector(hexR, 0),
upRight: createVector(
vecByAngle(30, hexApo + triangleR / 2, "cos"),
-vecByAngle(30, hexApo + triangleR / 2, "sin")),
};
place = createVector(tilingOffset, tilingOffset)
let tilingSize = createVector(
ceil(canvasSize / (hexR / 2)),
ceil(canvasSize / (hexR)));
/////////////////////////////////////////////////////////
for (let k = 0; k <= tilingSize.y; k++) {
place.set(tilingOffset, tilingOffset);
place.y = moves["downHex"].y * k;
let ctrl2 = abs(k % 2);
if (ctrl2 == 1) {
place.add(moves["right"]);
}
for (let i = 0; i <= tilingSize.x; i++) {
let ctrl = abs(i % 3);
if (ctrl == 0) {
fill(colors[0]);
polygon(place.x, place.y, hexR, 6, )
place.add(moves["upRight"]);
} else if (ctrl == 1) {
fill(colors[1]);
polygon(place.x, place.y, triangleR, 3, -30)
place.add(moves["downTria"]);
} else if (ctrl == 2) {
fill(colors[1]);
polygon(place.x, place.y, triangleR, 3, 30)
place.add(moves["upRight"]);
}
}
}
}
///////////////////////////////////////////////////////////////
function vecByAngle(angle, magnitude, cosOrSin) {
if (cosOrSin == "sin")
return sin(angle * PI / 180) * magnitude;
else if (cosOrSin == "cos")
return cos(angle * PI / 180) * magnitude;
}
function polygon(x, y, radius, npoints, angleOffset = 0) {
let angle = TWO_PI / npoints;
beginShape();
angleOffset *= PI / 180;
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a + angleOffset) * radius;
let sy = y + sin(a + angleOffset) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
}