Advertisement

hex grid

Started by April 19, 2019 02:17 AM
39 comments, last by JTippetts 5 years, 10 months ago

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();
        }

 

why did down vote I am working a new project.

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);
        }
    }

 

Obviously this isn't something you've written yourself.

In case someone searches and stumbles onto this thread, the solution source (copied wholesale in the above post), alongside explanations for the logic, can be found here: http://csharphelper.com/blog/2015/10/draw-a-hexagonal-grid-in-c/.

Hello to all my stalkers.

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.

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.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement
17 minutes ago, fleabay said:

badge of shame

Asking for help is not shameful, and should not be even hinted as being so.

 

That said, doing a bit of research on your own, and paying attention to advice that's been given to you for almost a decade should, at some point, be viewed as a requirement.

Hello to all my stalkers.

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.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

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.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement