xxxxxxxxxx
140
let colors = []
let canvasS = 600
let moves;
let hexR = 30
let hexA;
let triR;
let sqrS;
let sqrR;
function setup() {
createCanvas(canvasS, canvasS);
noStroke()
colors[0] = color(255, 237, 38);
colors[1] = color(255, 12, 96)
colors[2] = color(38, 211, 255)
hexA = hexR / 2 * sqrt(3);
triR = (hexR / 3) * sqrt(3);
sqrS = hexR;
sqrR = sqrS * sqrt(2) / 2;
let move1Mag = hexA + sqrS / 2;
let move2Mag = sqrS / 2 + triR / 2;
let move3Mag = sqrS / 2 + hexA;
moves = {
right: createVector(move1Mag, 0),
down: createVector(0, hexR + triR * 1.5 + sqrS * 0.5),
up: createVector(0, -(triR * 0.5 + sqrS * 0.5)),
sqrDown: createVector(0, triR * 0.5 + sqrS * 0.5),
hexDown: createVector(0, hexR + triR),
downRight: vecByAngle(30, move2Mag, "right", "down"),
// downRightTri: vecByAngle(30, move2Mag, "right", "down"),
upRight30: vecByAngle(30, move2Mag, "right", "up"),
upRight60: vecByAngle(60, move3Mag, "right", "up")
// upRightTri: vecByAngle(30, move2Mag, "right", "up"),
};
}
////////////////////////////////////////////////////////////////////////
function draw() {
background(220);
let tilingS = createVector(
20,
10);
let place = createVector(0, 0);
for (let r = 0; r <= tilingS.y; r++) {
let turn2 = (r % 2);
place.x = -turn2 * (hexA + 0.5 * sqrS);
place.y = moves["down"].y * r;
for (let c = 0; c <= tilingS.x; c++) {
let turn = (c % 2);
if (turn == 0) {
fill(colors[0])
polygon(place.x, place.y, sqrR, 4, 45)
place.add(moves["sqrDown"]);
fill(colors[2])
polygon(place.x, place.y, triR, 3, -30)
place.add(moves["downRight"]);
fill(colors[0])
polygon(place.x, place.y, sqrR, 4, -15);
place.add(moves["upRight60"]);
} else if (turn == 1) {
fill(colors[1])
polygon(place.x, place.y, hexR, 6, 30)
place.add(moves["hexDown"]);
fill(colors[2])
polygon(place.x, place.y, triR, 3, 30)
place.add(moves["upRight30"]);
fill(colors[0])
polygon(place.x, place.y, sqrR, 4, 15);
place.add(moves["upRight30"]);
place.add(moves["up"]);
}
}
}
}
//////FUNCTIONS/////////////////////////////////////////////////////////////
function vecByAngle(angle, magnitude, dirH, dirV) {
let sind = sin(angle * PI / 180) * magnitude;
let cosd = cos(angle * PI / 180) * magnitude;
if (dirH == "right") {
cosd = cosd;
} else if (dirH == "left") {
cosd = -cosd;
}
if (dirV == "up") {
sind = -sind;
} else if (dirV == "down") {
sind = sind;
}
return createVector(cosd, sind)
}
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);
}