Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active September 18, 2021 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/10c52ff8c45875602a3953668662da80 to your computer and use it in GitHub Desktop.
Save KrabCode/10c52ff8c45875602a3953668662da80 to your computer and use it in GitHub Desktop.
// see https://www.redblobgames.com/grids/hexagons/
float size = 1000/24;
float w = size*2;
float h = sqrt(3)*size;
float xStep = w*(3/4f);
float yStep = h;
int xCount;
int yCount;
void setup() {
size(1000, 1000);
xCount = floor(width/xStep+2);
yCount = floor(height/yStep+2);
}
void draw() {
background(0);
noFill();
stroke(255);
strokeWeight(4);
drawHexagonGrid();
}
void drawHexagonGrid() {
for (float xi = -xCount/2; xi <= xCount/2; xi++) {
for (float yi = -yCount/2; yi <= yCount/2; yi ++) {
float x = width/2 + xi*xStep;
float y = height/2 + yi*yStep-yStep/2f;
if (xi % 2 ==0) {
y += yStep/2f;
}
pushMatrix();
translate(x, y);
drawHexagon();
popMatrix();
}
}
}
void drawHexagon() {
beginShape();
for (int i = 0; i < 6; i++) {
float theta = map(i, 0, 6, 0, TAU);
vertex(size*cos(theta), size*sin(theta));
}
endShape(CLOSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment