mhh here’s the solution I found - I wonder if I could cleanup the code a bit and make just 1 loop instead of 2 without the j%2 statement…
The two blocks of code for j%2 change only for Vector c and f respectively the other stayed unchanged - so it it possible to improve it?
xpos = side; and ypos=0.866*side; in this manner I force the grid to be of regular hexagons.
I execute outside the for loops a purgeLines() function to delete lines that share equal start and end points coordinates - so get rid of duplicates.
As Philfrei mentioned the other approach would be to the create a hexagon object itself and then replicate over a grid, and then purge duplicates points at joining hexagons and not at the edge of grid. Never used hashmaps but is something I would really like to harness when Ill get sometime.
float side = w / (numCols);
particles = new Particle[numCols][numRows][2];
for (int i=0; i<numCols; ++i){
for(int j=0; j<numRows; ++j) {
float xpos,ypos;
if (j%2!=0 ){
xpos = side*(i-0.5f);
ypos = (0.866f*side) * j;
} else{
xpos = side*(i);
ypos = (0.866f*side) *j;
}
vertices[i][j][0] = new Vector(xpos,
ypos, 1.0f);
}
}
for (int j =0; j<numRows-2; ++j){
if (j%2 ==0){
for (int i=j%2*2; i<numCols-2; i+=3){
Vector a = vertices[i][j][0];
Vector b = vertices[i+1][j][0];
Vector c = vertices[i+2][j+1][0];
Vector d = vertices[i+1][j+2][0];
Vector e = vertices[i][j+2][0];
Vector f = vertices[i][j+1][0];
draw.line(a,b);
draw.line(b,c);
draw.line(c,d);
draw.line(d,e);
draw.line(e,f);
draw.line(f,a);
}
} else{
for(int i=j%2*2; i<numCols-2; i+=3){
Vector a = vertices[i][j][0];
Vector b = vertices[i+1][j][0];
Vector c = vertices[i+1][j+1][0];
Vector d = vertices[i+1][j+2][0];
Vector e = vertices[i][j+2][0];
Vector f = vertices[i-1][j+1][0];
draw.line(a,b);
draw.line(b,c);
draw.line(c,d);
draw.line(d,e);
draw.line(e,f);
draw.line(f,a);
}
}
}
http://www.designedbyenergy.com/site/wp-content/uploads/misc/1.jpg