Reputation: 11
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int n = 0; n < 13; n++)
{
double hexCentX = x/2+(3*u*n*Math.cos(Math.PI/3));
double hexCentY = y/2+(u*n*Math.sin(Math.PI/3));
Polygon sn = new Polygon();
for (int i = 0; i < 6; i++)
sn.addPoint((int) (hexCentX + u * Math.cos(i * Math.PI / 3)),
(int) (hexCentY + u * Math.sin(i * Math.PI / 3)));
g.drawPolygon(sn);
g.drawString(Integer.toString(n), (int)hexCentX, (int)hexCentY);
}
}
I'm trying to script something together to automatically build a grid of Hexagons. Hexagons are of arbitrary size u, and Hexagon'0' should be in the centre of a window x by y with sequential ones added in rings around it.
In theory, I think, my maths should be sound, but something is going drastically wrong somewhere, because it does this instead.
https://www.dropbox.com/s/suj282lnkmxn0g1/hexagons.bmp
They just go in a diagonal downwards line. Apologies for the low-def image!
Would anyone be able to help me fix my code and/or point out the glaring failure in mathematics? Will provide the entire program if needed!
Upvotes: 1
Views: 1299
Reputation: 4330
From the code, all the centers of your hexagon indeed lie on the line C(t) = (x/2+3*u*t*c, y/2+u*t*s)
. In your outer loop, you need to generate hexagon centers coordinates that actually lie on a spiral.
Upvotes: 1