Last active
September 18, 2021 09:30
-
-
Save KrabCode/10c52ff8c45875602a3953668662da80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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