hex grid

Started by
35 comments, last by JTippetts 4 years, 12 months ago

You have not actually posted a question or otherwise indicated what you are having problems with.

I don't have experience working with hexagon grids and it isn't something I can invest the time investigating to be able to provide any suggestions. In general, I would expect it to be much more difficult than a square tile based project. But if you just wanted to print out a grid, you have code to do that which you can examine to try and understand what's going on.

Advertisement

There is nothing wrong with re-inventing wheels for educational purposes, but please make sure YOU re-invent it rather than getting it from someone else. There is no substitute for learning than trying and experiencing it yourself, it's really the best way.

Sure it's slow and you will fail to find the answer a few times, but that is normal when you're trying to do something you never did before.

I have been programming for nearly 40 years, and I still make wrong turns at times, and fail to find a solution. However, a failure teaches me where I made a mistake, so I can try again, and avoid making that same mistake a second time.

Eventually, you get to know all pitfalls to avoid, and then Eureka, it works.!

1 hour ago, kseh said:

You have not actually posted a question or otherwise indicated what you are having problems with. 

I don't have experience working with hexagon grids and it isn't something I can invest the time investigating to be able to provide any suggestions. In general, I would expect it to be much more difficult than a square tile based project. But if you just wanted to print out a grid, you have code to do that which you can examine to try and understand what's going on.

I am trying to draw a whole screen of hexes , I am able to draw only one hex  here is my already posted 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 = 1.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();
        }

 

Well, as I'm sure you know, if you want to do something multiple times, you use a loop. Use a loop to paint your hexagon and adjust its location as the loop processes.

should I use a for or a while loop?

Personally, I use for loops when the number of iterations through the loop is known or can be fairly easily calculated and stored in a variable. I use while loops when I want to stop iterating through the loop on some particular condition.

I am  going to use a for loop

wow you guys are sure critical about downvotes, all I am trying to do is draw a screen of hexes.

What have you tried, how did you expect it work, what happened in reality and why didn't it do what you thought it would?

 

All code is an experiment, you expect it to work in some way, and when it does, you're happy (obviously). When it doesn't you try to learn from it, ie what did it do and why, what new thing did you learn from the experiment?

That new knowledge is what you can apply on the next experiment. Can you use your new knowledge to avoid the same mistake the next time?

 

This process of gaining knowledge from a failure seems to be missing here.

This topic is closed to new replies.

Advertisement