Jump to content
  • Advertisement
phil67rpg

C# hex grid

Recommended Posts

well I am able to draw a hexagon but I want to draw a whole screen of hexagons. here is my working code

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
                Graphics g = e.Graphics;
                Pen whitePen = new Pen(Color.White, 1);
                float height = 50.0f;
                float width = (float)(4 * (height / 2 / Math.Sqrt(3)));
                float y = height / 2;
                float x = 0;
                float row = 0.0f;
                y += row * height;
                float col = 0.0f;
                if (col % 2 == 1)
                {
                    y += height / 2;
                }
                x += col * (width * 0.75f);
                PointF pt1 = new PointF(x, y);
                PointF pt2 = new PointF(x + width * 0.25f, y - height / 2);
                PointF pt3 = new PointF(x + width * 0.75f, y - height / 2);
                PointF pt4 = new PointF(x + width, y);
                PointF pt5 = new PointF(x + width * 0.75f, y + height / 2);
                PointF pt6 = new PointF(x + width * 0.25f, y + height / 2);
                g.DrawLine(whitePen, pt1, pt2);
                g.DrawLine(whitePen, pt2, pt3);
                g.DrawLine(whitePen, pt3, pt4);
                g.DrawLine(whitePen, pt4, pt5);
                g.DrawLine(whitePen, pt5, pt6);
                g.DrawLine(whitePen, pt6, pt1);
                whitePen.Dispose();
                g.Dispose();
        }

 

Share this post


Link to post
Share on other sites
Advertisement

well I solved my problem

        private float HexWidth(float height)
        {
            return (float)(4 * (height / 2 / Math.Sqrt(3)));
        }

        private PointF[] HexToPoints(float height, float row, float col)
        {
            // Start with the leftmost corner of the upper left hexagon.
            float width = HexWidth(height);
            float y = height / 2;
            float x = 0;

            // Move down the required number of rows.
            y += row * height;

            // If the column is odd, move down half a hex more.
            if (col % 2 == 1) y += height / 2;

            // Move over for the column number.
            x += col * (width * 0.75f);

            // Generate the points.
            return new PointF[]
                {
            new PointF(x, y),
            new PointF(x + width * 0.25f, y - height / 2),
            new PointF(x + width * 0.75f, y - height / 2),
            new PointF(x + width, y),
            new PointF(x + width * 0.75f, y + height / 2),
            new PointF(x + width * 0.25f, y + height / 2),
                };
        }

        public void DrawHexGrid(Graphics gr, Pen pen,
        float xmin, float xmax, float ymin, float ymax,
        float height)
        {
            // Loop until a hexagon won't fit.
            for (int row = 0; ; row++)
            {
                // Get the points for the row's first hexagon.
                PointF[] points = HexToPoints(height, row, 0);

                // If it doesn't fit, we're done.
                if (points[4].Y > ymax) break;

                // Draw the row.
                for (int col = 0; ; col++)
                {
                    // Get the points for the row's next hexagon.
                    points = HexToPoints(height, row, col);

                    // If it doesn't fit horizontally,
                    // we're done with this row.
                    if (points[3].X > xmax) break;

                    // If it fits vertically, draw it.
                    if (points[4].Y >= ymax)
                    {
                        gr.DrawPolygon(pen, points);
                    }
                }
            }
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            DrawHexGrid(this.CreateGraphics(),new Pen(Color.White),0.0f,500.0f,0.0f,500.0f,50.0f);
        }
    }

 

Share this post


Link to post
Share on other sites

well thanks for all the downvotes,  you guys suck I am  trying to do a new game and all get is downvotes, I might stop posting because I can solve problems all by myself.

Share this post


Link to post
Share on other sites
18 minutes ago, phil67rpg said:

I might stop posting because I can solve problems all by myself.

You learn far more from searching for answers and solving problems yourself. The people that think they are helping you are doing you a disservice.

Seeking help for general programming should be a last resort and almost regarded as a badge of shame "I HAVE FAILED TO ( LEARN THIS / FIGURE THIS OUT ) PLEASE HELP ME" and not a "hey guys I'm trying to do this, is this ok?" mentality.

Share this post


Link to post
Share on other sites
Posted (edited)
15 minutes ago, Lactose said:

Asking for help is not shameful

It should also not be par for the course and continuously met with open arms. Also I said 'almost' shameful and refers to a mindset. Some people seem to enjoy seeking help for general programming and that is not good.

Edited by fleabay

Share this post


Link to post
Share on other sites
11 minutes ago, fleabay said:

It should also not be par for the course and continuously met with open arms. Also I said 'almost' shameful and refers to a mindset. Some people seem to enjoy seeking help for general programming and that is not good.

I strongly disagree. Asking for help is not a shameful mindset, it's acknowledging that other people know more than you in a certain area. It's an awesome way to learn more. It's not always the best approach, but it's certainly valid in many, many situations.

I ask about various programming bits and pieces all the time. I have the luxury of working in development, so I have easy access to people I can ask. Others might not be as lucky.

Asking for help does not mean not doing any research on your own. I do both. As do most people, I would guess.

 

Like I said, for me it's more the part where one ignores advice, questions or suggestions and keeps on expecting handouts to every single little problem it becomes an issue. At some point, one needs to get past that level.

Share this post


Link to post
Share on other sites
Posted (edited)
25 minutes ago, Lactose said:

Asking for help is not a shameful mindset, it's acknowledging that other people know more than you in a certain area.

I do acknowledge that people know more than me in a certain area ( the area we are talking about and that you quoted me as such is general programming.) Those people write books and make tutorials. You learn general programming from books and tutorials (and searching) and then if you have problems that aren't easily solved and can't figure out a solution, then by all means, seek help.

Edited by fleabay

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • 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!