Skip to content

Instantly share code, notes, and snippets.

@HerbertV
Last active December 14, 2015 23:59
Show Gist options
  • Save HerbertV/5169966 to your computer and use it in GitHub Desktop.
Save HerbertV/5169966 to your computer and use it in GitHub Desktop.
Sample Hexagon Shape for processing.js
/**
* =========================================================================
* Hexagon
* =========================================================================
* simple hexagon class for processing.js
*/
class Hexagon
{
// center of the hexagon
float centerX;
float centerY;
// outer Radius => center to vertex;
float outerRadius;
// inner radius = h/2 (middle between two vertices;
float innerRadius;
// vertex coordinates
float[] verticesX;
float[] verticesY;
// appearance
color cFill = #FFFFFF;
color cStroke = #000000;
/**
* Constructor
*/
Hexagon(
float x,
float y,
float rad
)
{
this.verticesX = new float[6];
this.verticesY = new float[6];
this.centerX = x;
this.centerY = y;
this.outerRadius = rad;
this.innerRadius = rad * cos(30 *PI / 180);
this.setupVertices();
}
/**
* @private
*/
void setupVertices()
{
for( int i=0; i<6; i++ )
{
this.verticesX[i] = this.centerX
+ (this.outerRadius * cos( (60*i) /180 * PI));
this.verticesY[i] = this.centerY
+ (this.outerRadius * sin( (60*i) /180 * PI));
}
}
void draw()
{
fill(this.cFill);
stroke(this.cStroke);
beginShape();
for( int i=0; i<6; i++ )
vertex( this.verticesX[i], this.verticesY[i] );
endShape(CLOSE);
}
void setPosition(float x, float y)
{
this.centerX = x;
this.centerY = y;
this.setupVertices();
}
void setFillColor(color c)
{
this.cFill = c;
}
void setStrokeColor(color c)
{
this.cStroke = c;
}
float getInnerRadius()
{
return this.innerRadius;
}
float getOuterRadius()
{
return this.outerRadius;
}
float getCenterX()
{
return this.centerX;
}
float getCenterY()
{
return this.centerY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment