Advertisement Jump to content
Sign in to follow this  

Please Help! coordinates of a hexagonal grid by math?

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi community, i am trying to get the following to work: I have a hexfield like this: I now like to have that, everytime i click on a field a mathematical method returns the X and Y values that this field would have in my array hexfields[rows,cols] (so the value that is printed in the field rigth now on the screenshot). I already know how to catch the Mouse-Coordinates, which are supposed to feed the mathfunction. I already have an implementation that uses a colored bitmap, but i am not satisified with this solution. (i can post/send if anybody wants a proof that i have done some homework already...). I also know that i could use "mousemaps" but i dislike this implementation too. But since i am really, really bad in math i don't understand how to do that (yes i read the gamedev articles about that, but they could also have been in chinese...), if anybody could help me how such a function should look in my case, that would be help me really a lot :) This is the GameDev article i am writing about: This is the code to generate the field in the screenshot:
public class hexfield
		const int iBaseX      = 21;
   		const int iBaseY      = 12;
   		const int iCols       = 15;
   		const int iRows       = 15;
   		const int iWidth      = iCols*2*iBaseX+iBaseX+1;
   		const int iHeight     = iRows*3*iBaseY+iBaseY+1;
   		private Pen blackPen;
   		Font _font   = new Font ("Arial", 9);
	  	private Brush      _brushText;
  protected Point GetHexagonPoint (int iX, int iY)
      int iOffsetX;
      int iOffsetY;
      iOffsetX = iX*2*iBaseX;
      iOffsetY = iY*3*iBaseY;
      if (iY % 2 == 1) {
         iOffsetX += iBaseX;

      return new Point (iOffsetX, iOffsetY);

   protected void DrawHexagon (Graphics g, int iX, int iY)
      Point pt;

      int iOffsetX;
      int iOffsetY;

      pt = GetHexagonPoint (iX, iY);
      iOffsetX = pt.X;
      iOffsetY = pt.Y;

      Point[] apt =
         new Point (iOffsetX + 0*iBaseX, iOffsetY + 1*iBaseY),
         new Point (iOffsetX + 1*iBaseX, iOffsetY + 0*iBaseY),
         new Point (iOffsetX + 2*iBaseX, iOffsetY + 1*iBaseY),
         new Point (iOffsetX + 2*iBaseX, iOffsetY + 3*iBaseY),
         new Point (iOffsetX + 1*iBaseX, iOffsetY + 4*iBaseY),
         new Point (iOffsetX + 0*iBaseX, iOffsetY + 3*iBaseY),
         new Point (iOffsetX + 0*iBaseX, iOffsetY + 1*iBaseY)

	blackPen     = Pens.Black;	
      // Draw the hex
	g.DrawPolygon(blackPen, apt);      
      // Make [x,x] fieldstring
      String strText;
      SizeF sizef;
	  _brushText     = Brushes.Black;
      strText = String.Format ("[{0},{1}]", iX, iY);
      sizef = g.MeasureString (strText, _font);



   public void DrawMap(Graphics g)
   	 for (int iX = 0; iX < iCols; ++iX) {
         for (int iY = 0; iY < iRows; ++iY) {
            DrawHexagon (g, iX, iY);
1000 Thanks in advantage! [Edited by - code_nerd on September 13, 2005 5:07:45 AM]

Share this post

Link to post
Share on other sites
My advice is to go through the GDnet articles here at GD again and really try to understand its content. I think that if you want to make games you should improve your maths skills since making games is mostly maths and logical thinking.

Edit: I hope that didnt sound unfriendly, but I truly belive that if you dont like maths, you probably won't like game programming either. It is usually not so hard to understands mathematical papers if you carefully convinces yourself step by step.

[Edited by - Enselic on September 14, 2005 12:28:22 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!