1. #### hexagonal board drawing

I'm trying to write out an optimized algorithm to draw a board made of hexagonal shapes.

input being:

hLength : length of an hexagon side.
bWidth : width of the board.
bHeight : Height of the board.

output :

the board is made of a tile of hexagonal shapes.

The base function to use is : Draw_Line(X1, Y1, X2, Y2);

2. No Profile Picture
epl
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2001
Location
Dublin
Posts
413
Rep Power
18
not tested, will try and test it tomorrow:
Code:
```class HexagonTiles
{private double rootThree, halfLength;

public int sideLength;
public int boardWidth;
public int boardHeight;

HexagonTiles (int l,int bW,int bH)
{rootThree=sideLength*Math.sqrt(3);
halfLength=sideLength/2;

sideLength=l;
boardWidth=bW;
boardHeight=bH;
}

public void draw(int originX,int originY)
{int x,y,r,c

//calculate x & y based on origin arguments
//calculate r & c based on board size

// for the moment:
x=y=0;
r=boardHeight/rootThree;
c=boardWidth/(rootThree-halfLength);

for (int i=0;i<r;i++)
{for (int j=0;i<c;j++)
{drawLines(x+i*(rootThree-halfLength),y+j*rootThree);}
if (j%2==0) {y+=rootThree/2;} // j is even
else        {y-=rootThree/2;} // j is odd
} x+=sideLength
}

private void drawLines(int x,int y) // draw a three pointed asterix
{int x0,y0;

x0=x-halfLength;
y0=y-rootThree/2;
Draw_Line(x,y,x0,y0); // line like clock hand at 10:30 (roughly!)

y0+=rootThree;
Draw_Line(x,y,x0,y0); // line like clock hand at 7:30 (roughly!)

x0=x+halfLength;
y0=y;
Draw_Line(x,y,x0,y0); // line like clock hand at 3:00
}
}```
Last edited by epl; June 6th, 2003 at 06:06 PM.
3. mmmh.. thanks =)

It seems that complexity level of drawing the whole grid of each hexagon independently is quite the same...
4. No Profile Picture
epl
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2001
Location
Dublin
Posts
413
Rep Power
18
(i'm not sure i get what you mean, but) when you draw the whole grid by drawing the hexagons one at a time you will draw each line twice - as each line is the border between two hexagons.

that's why above i am drawing three lines, not six, for each hexagon - so yes, the level of complexity above is the same, but the worked is halved.

i can think of other ways of doing it but what you have seems the most straightforward way of going about it.
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Location
Posts
2
Rep Power
0
I was fooling around with drawing more or less of each hex, and I found that that tiling went a bit smoother when I started drawing a 'wrench' shape with each iteration.

BTW anyone have any hard facts on whether two integer additions are faster than a single integer multiplication?

Anyways it went a little bit like this:
Code:
```int rootThree = Math.sqrt(3);
halfLength    = hLength / 2;
int x = 2 * hLength;
int y = rootThree;
int xpre1  = x - halfLength;
int xpre2  = xpre1 - hLength;
int xpre3  = x - ( 2 * hLength );
int xpost1 = x + hLength;
int ypre   = y - rootThree;
int ypost  = y + rootThree;
int xincrement = 3 * hLength;
int yincrement = 2 * rootThree;

while ( xpost <= bWidth ) {
while ( ypost <= bHeight ) {
Draw_Line(xpre2,ypost,xpre3,y);
Draw_Line(xpre3,y,xpre2,ypre);
Draw_Line(xpre2,ypre,xpre1,ypre);
Draw_Line(xpre1,ypre,x,y);
Draw_Line(x,y,xpre1,ypost);
Draw_Line(x,y,xpost,y);

y     += yincrement;
ypre  += yincrement;
ypost += yincrement;
}
Draw_Line(xpre2,ypost,xpre1,ypost);

x      += xincrement;
xpre1  += xincrement;
xpre2  += xincrement;
xpre3  += xincrement;
xpost1 += xincrement;

;maybe insert some cases to draw final partial columns...
}```
-pb