[FreecivDev] Hex map implementation
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
I've started playing with hex map. For now just class in java, generic
enough it could be used for some wargame if freeciv will not choose
hexes after all.
I've decided to use vertically flattened hexes. They fit into square  a
lot simplier to make calculations/repaint etc. Normal hex should have
proportions of 2 x sqrt(3) and they have just 2 x 2.
If somebody thinks that hexes are very simple, here are two routines
that given the point on screen (actually on map, but it's not important)
return Point with tile coordinates.
For simplicity I'll ignore negative/too large tile coordinates (as they
depend on wrapping etc)
First one for normal square tiles:

return new Point(
(x/singleTileWidth),
(y/singleTileHeight)
);

and now for hex map (sh4 is width of hex, sh, sh2 and sh3 are 1/4, 2/4
and 3/4 of it respectively). This is for map with rows being correct
direction and columns zigzaging, with first row (index 0) being moved
left. If somebody can come with simplier routine, please tell me.

int y1 = y/sh;
boolean differ = ((y1%3) == 0); // are we in hex connection microrow
int ycell1,ycell2,xcell1,xcell2;
ycell1 = y1/3;
ycell2 = differ ? ycell11: ycell1;
if ( (ycell1%2) == 0 ) // row moved left
{
xcell1 = x/sh4;
xcell2 = (xsh2)/sh4;
}
else
{
xcell1 = (xsh2)/sh4;
xcell2 = x/sh4;
}
if ( !differ )
return new Point(xcell1,ycell1);
int xoffs,yoffs;
yoffs = y/sh3;
assert(y < sh);
if ( (ycell2%2) == 0 ) // upper row moved left
xoffs = (x+sh2)%sh4; // it is opposite to condition on purpose
else
xoffs = x%sh4;
// add check for negative and too large cells (wrap or return null)
if (yoffs  (Math.abs(xoffssh2)/2) > 0 )
return new Point(xcell2,ycell2);
else
return new Point(xcell1,ycell1);

Now imagine that hexes would not have been flattened ... floats
everywhere I'm afraid.
Artur
 [FreecivDev] Hex map implementation,
Artur Biesiadowski <=

