xxxxxxxxxx
var radius = 20;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0, 114, 183);
strokeWeight(1);
stroke(255);
noFill();
var hexHeight = radius * 2;
var hexWidth = sqrt(3) / 2 * hexHeight;
for (var i = 0; i < height / radius; i++) {
for (var j = 0; j < width / radius; j++) {
var xSpacing = hexWidth * j;
var ySpacing = hexHeight * 0.75 * i;
//drawing two hex every time
if ((i % 2) == 0) {
hexagon(xSpacing, ySpacing, radius);
} else {
hexagon(xSpacing + hexWidth / 2, ySpacing, radius);
}
}
}
}
function hexagon(cx, cy, r) {
var angle = TWO_PI / 6;
//vertical
d = 0.5;
beginShape();
for (var i = 0; i <= 6; i++) {
var x = cx + cos(angle * i + d) * r;
var y = cy + sin(angle * i + d) * r;
vertex(x, y);
}
endShape();
}