Hi.
I’m creating an Hexagonal grid
I have the points for creating it already(edges). The method cell calculates the creation of one hexagon and then it is passed to the method grid where the grid is created. The problem is I cannot get the values of all the edges(x,y) created within the grid method. I tried using an ArrayList to save all the edges but I´m getting incomplete values.
I need the edges of the complete grid in order to use them in a more general class.
I know the grid is calculated correctly because I can draw it if I uncomment the code from lines 31 to 35.
Any suggestion would be greatly appreciated
Thanks
Hexagon h;
void setup() {
size(500, 500);
h= new Hexagon();
h.grid();
}
class Hexagon {
int sides;
PVector[]edges;
int size;
Hexagon() {
size=30;
sides=6;
edges= new PVector[sides];
}
void cell(float cenX, float cenY) {
for (int i=0; i<sides; i++) {
float angle_deg=60*i-30;
float angle_rad= PI/180*angle_deg;
float edX= cenX+size*cos(angle_rad);
float edY= cenY+size*sin(angle_rad);
edges[i]= new PVector(edX, edY);
}
//DRAWING HEXAGONAL GRID TEST
//for (int i =0; i<sides-1;i++){
//line(edges[i].x, edges[i].y, edges[i+1].x, edges[i+1].y);
//line(edges[5].x, edges[5].y, edges[0].x, edges[0].y);
//}
}
ArrayList<PVector> grid() {
ArrayList <PVector> gridPts= new ArrayList <PVector>();
float h=2*size;
float w=(sqrt(3))*size;
float vertCent=h*0.75;
int row=0;
for (float cenY=h/2; cenY<250; cenY+=vertCent)
{
float offX= ((row%2)==0) ? w:w/2;
for (float cenX=offX+50; cenX<250; cenX+=w)
{
cell(cenX, cenY);
gridPts.add(new PVector(cenX, cenY));
}
row++;
}
println(gridPts.get(0)); /////INCOMPLETE VALUES
return gridPts;
}
}