Skip to content

Instantly share code, notes, and snippets.

@josePhoenix
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save josePhoenix/cce3698f7ae1e28d8db7 to your computer and use it in GitHub Desktop.

Select an option

Save josePhoenix/cce3698f7ae1e28d8db7 to your computer and use it in GitHub Desktop.
class Hexagon { // Unlike your generic polygon, I assumed six sides to simplify things
float cx, cy, r, theta; // r is radius, theta is orientation. I tried to figure out whether positive theta is clockwise or counterclockwise, but I forget. Basically, you can change the orientation of your hexagon from 0 to 2pi
PVector radiusvec, center_to_center; // radiusvec is a vector where magnitude is r
Hexagon(float _cx, float _cy, float _r, float _theta) {
// see above for these
cx = _cx;
cy = _cy;
r = _r;
theta = _theta;
radiusvec = new PVector(r, 0);
// below is the center-to-center distance we talked about
// we compute it as a vector because we want to eventually have the
// center to center distance in six different directions (for six tesselation tiles)
center_to_center = new PVector(1, 0);
center_to_center.setMag(2 * (float)Math.sqrt(3) / 2.0);
center_to_center.mult(r);
center_to_center.rotate(theta + PI / 6.0); // go through midpoint of a side (one side is pi / 3, pi/6 is halfway through a side)
}
void display() {
pushMatrix(); // we don't know what drawing context was set up, and we don't care so reset it
translate(cx, cy); // set origin to cx, cy. point(0,0) will now draw at cx,cy
rotate(theta); // set orientation of axes (with respect to the +x is right, +y is down defaults) to theta. line(0, 0, 100, 0) will now go from cx, cy to somewhere 100 units from cx on the inclined axes
fill(cx, cy, (cx + cy) / 2.0); // color fun, not important
beginShape(); // now we start drawing the vertices of our hexagon
PVector vtx = radiusvec.get(); // copy the original radius vector because we're going to be changing it (not strictly necessary as long as you're going 2pi around every draw iteration... if not you'll get out of sync)
for (int vtxid = 0; vtxid < 6; vtxid++) { // for six vertices...
vertex(vtx.x, vtx.y); // ... set a vertex vtx.x, vtx.y from the origin (remember, the origin is now cx, cy, and the axes themselves are inclined with respect to the pixel grid) ...
vtx.rotate(TWO_PI / 6.0); // ... then advance our vector by 2 pi / 6 (one sixth of a complete rotation, because we have six sides)
}
endShape(CLOSE); //
popMatrix(); // forget about the origin being at cx, cy and the axes being inclined by theta for any draw calls outside this method
// THIS IS JUST FOR THE DIRECTION MARKER
// bc the direction marker needs absolute rotation (nothing pre-applied)
pushMatrix();
translate(cx, cy);
PVector orientation_mark = center_to_center.get();
orientation_mark.mult(0.3);
PVector orientation_mark2 = orientation_mark.get();
orientation_mark2.rotate(HALF_PI);
orientation_mark2.mult(0.25);
PVector orientation_mark3 = orientation_mark.get();
orientation_mark3.rotate(-1.0*HALF_PI);
orientation_mark3.mult(0.25);
triangle(
orientation_mark.x, orientation_mark.y,
orientation_mark2.x, orientation_mark2.y,
orientation_mark3.x, orientation_mark3.y
);
popMatrix();
// ALL RIGHT THAT'S ENOUGH OF THAT
}
Hexagon[] tesselate() {
Hexagon[] tesselations = new Hexagon[6];
PVector tesselcenter = center_to_center.get();
for (int side_id = 0; side_id < 6; side_id++) {
tesselations[side_id] = new Hexagon(cx + tesselcenter.x, cy + tesselcenter.y, r, theta + side_id * TWO_PI / 6.0 + PI);
tesselcenter.rotate(TWO_PI / 6.0);
}
return tesselations;
}
}
Hexagon myhex;
Hexagon myhex2;
Hexagon[] tesselhexen;
Hexagon[] tesselhexen2;
void setup() {
size(800, 400);
frameRate(15);
myhex = new Hexagon(200, 200, 50.0, PI / 4.0);
tesselhexen = myhex.tesselate();
myhex2 = new Hexagon(600, 200, 50.0, PI / 2.0);
tesselhexen2 = myhex2.tesselate();
}
PVector tracktransform;
void draw() {
background(128, 128, 128);
myhex.display();
for (int i = 0; i < tesselhexen.length; i++) {
tesselhexen[i].display();
}
myhex2.display();
for (int i = 0; i < tesselhexen2.length; i++) {
tesselhexen2[i].display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment