Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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