Skip to content

Instantly share code, notes, and snippets.

@dresswithpockets
Created December 18, 2016 21:26
Show Gist options
  • Save dresswithpockets/f582fa4e3000101d79c44264b894f314 to your computer and use it in GitHub Desktop.
Save dresswithpockets/f582fa4e3000101d79c44264b894f314 to your computer and use it in GitHub Desktop.
class Hexagon {
float x = 0;
float y = 0;
float size = 30;
HexTriangle[] triangles;
Hexagon(float x, float y, float size)
{
this.x = x;
this.y = y;
this.size = size;
float s = (size/sqrt(3));
float s_half = s/2;
float t_height = sqrt(pow(s, 2) - pow(size/2, 2));
print(t_height);
this.triangles = new HexTriangle[6];
triangles[0] = new HexTriangle(x - s_half, y + size/2, x + s_half, y + size/2, x, y);
triangles[1] = new HexTriangle(x - (s_half + t_height), y, x - s_half, y + size/2, x, y);
triangles[2] = new HexTriangle(x - s_half, y - size/2, x - (s_half + t_height), y, x, y);
triangles[3] = new HexTriangle(x - s_half, y - size/2, x + s_half, y - size/2, x, y);
triangles[4] = new HexTriangle(x + s_half, y - size/2, x + (s_half + t_height), y, x, y);
triangles[5] = new HexTriangle(x + (s_half + t_height), y, x + s_half, y + size/2, x, y);
}
void draw() {
stroke(128, 43, 226);
for (int i = 0; i < 6; i++) {
triangles[i].draw();
}
}
}
class HexTriangle {
float x1, x2, x3, y1, y2, y3;
HexTriangle(float x1, float y1, float x2, float y2, float x3, float y3) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
void draw() {
stroke(204, 153, 255);
fill(204, 153, 255);
triangle(x1, y1, x2, y2, x3, y3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment