Advertisement Jump to content
Sign in to follow this  
code_nerd

Keeping track of Hexagons in a Hexgrid / BoundingBox (GDI+/C#)

This topic is 4899 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 folks! Again a most likely simple question, i have my HexGrid now, made with GDI+. My actual problem is still how i best can keep track of my hexagons, combined with a "OnClick" Feature, so that everytime i click with my mouse on a hexagon, it's "name" (3,4 for example) will be shown in a TextBox or so. The function that creates my HexGrid:
void MakeHexagonMatrix(int ColumnCount, int RowCount, int EdgeSize, Graphics graphics)
{
// This function will draw a hexagon matrix onto the given graphics object.  Each edge of
// a hex will be EdgeSize long


Pen blackPen =  new Pen(Color.Black, 1);
int diagVDist = (int)(EdgeSize * 0.866025403784);// sin(PI/3) = sqrt(3)/2

int diagHDist = EdgeSize / 2;// cos(PI/3) = 0.5
int xOffset, yOffset;

// Make RowCount rows
for(int row = 0; row < RowCount; row++)
{
yOffset = (2 * diagVDist / 2) * row + diagVDist;

// Make ColumnCount individual hexes in each row
for(int col = 0; col < ColumnCount / 2; col++)
{
xOffset = (col * 3 * EdgeSize) + ((row % 2) * (diagHDist + EdgeSize));

Point leftMid = new Point(xOffset, yOffset);
Point topLeft = new Point(leftMid.X + diagHDist, leftMid.Y - diagVDist);
Point topRight = new Point(topLeft.X + EdgeSize, topLeft.Y);
Point rightMid = new Point(topRight.X + diagHDist, topRight.Y + diagVDist);
Point bottomRight = new Point(rightMid.X - diagHDist, rightMid.Y + diagVDist);
Point bottomLeft = new Point(bottomRight.X - EdgeSize, bottomRight.Y);

Point[] points = new Point[10];
points[0] = leftMid;
points[1] = topLeft;

points[2] = topLeft;
points[3] = topRight;

points[4] = topRight;
points[5] = rightMid;

points[6] = rightMid;
points[7] = bottomRight;

points[8] = bottomRight;
points[9] = bottomLeft;

// Draw the hex
graphics.DrawPolygon(blackPen, points);
}
}
}
Should i save the "points" array to another array and define BoundingBoxes somehow? Could someone please post a small example that could help me? An ideal solution would be (IMHO) to somehow index the hexagons, so that i can easily access them like "hexagons[1,1].XXXX" Thanks for your time!

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!