Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 6th, 2003, 09:00 AM
Maldor's Avatar
Maldor Maldor is offline
Muhhnnn !!
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2003
Posts: 1,530 Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 7 h 38 m 2 sec
Reputation Power: 83
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);


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


Reply With Quote
  #2  
Old June 6th, 2003, 07:02 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
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.

Reply With Quote
  #3  
Old June 7th, 2003, 04:22 AM
Maldor's Avatar
Maldor Maldor is offline
Muhhnnn !!
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2003
Posts: 1,530 Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level)Maldor User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 7 h 38 m 2 sec
Reputation Power: 83
mmmh.. thanks =)

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

Reply With Quote
  #4  
Old June 7th, 2003, 05:26 AM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 8
(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.

Reply With Quote
  #5  
Old July 2nd, 2003, 05:08 PM
palebear palebear is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Magrath, Alberta CANADA
Posts: 2 palebear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > hexagonal board drawing


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT