Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 6th, 2003, 09:00 AM
 Maldor
Muhhnnn !!

Join Date: Apr 2003
Posts: 1,530
Time spent in forums: 1 Week 1 Day 7 h 38 m 2 sec
Reputation Power: 88
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);

__________________
"The ultimate knowledge is reached when it does not bring new questions..."
-- Usaphdas encyclopedia XV.4

#2
June 6th, 2003, 07:02 PM
 epl
Contributing User

Join Date: Mar 2001
Location: Dublin
Posts: 413
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
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 07:06 PM.

#3
June 7th, 2003, 04:22 AM
 Maldor
Muhhnnn !!

Join Date: Apr 2003
Posts: 1,530
Time spent in forums: 1 Week 1 Day 7 h 38 m 2 sec
Reputation Power: 88
mmmh.. thanks =)

It seems that complexity level of drawing the whole grid of each hexagon independently is quite the same...

#4
June 7th, 2003, 05:26 AM
 epl
Contributing User

Join Date: Mar 2001
Location: Dublin
Posts: 413
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
(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
July 2nd, 2003, 05:08 PM
 palebear
Junior Member

Join Date: Jul 2003
Posts: 2
Time spent in forums: < 1 sec
Reputation 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

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > hexagonal board drawing