Bixel's Dev Blog

This is where I post all my programming stuff.


About Me  
Been wanting to create procedural map generation and after reading this post http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/ I decided to start learning how to do Voronoi Diagrams as a start.

I quickly realized this was extremely complicated and I didn’t feel like re-inventing the wheel so I searched for some code that was already completed and see if I could do something with it. Fortunately this guy > http://www.codeproject.com/Articles/11275/Fortune-s-Voronoi-algorithm-implemented-in-C did most of the work for me in C#. Now I just needed to figure out his code and then draw it.

Originally I was just going to use a Windows Form with a picturebox in that and do all the drawing there. But the picturebox does not like to add multiple images easily so instead I added in some XNA references and placed a handy XNA window inside the form. Now I can draw everything much much more easier.

The biggest hurdle was interpreting his code as it has almost 0 comments or documentation, additionally I also had to do a lot vector calculations and point placement since his code could not generate proper line data if there was less than 4 points. Some of the values were double.PositiveInfinity so I had to draw something but you cant draw Infinity, so what to do? Well luckily data included the direction as well as origin data, so…


        public static Vector2 PolarToRectangular(float Distance, float Angle)
        {
            return new Vector2((float)(Distance * Math.Cos(Angle)), (float)(Distance * Math.Sin(Angle)));
        }

I just put in a distance I felt could cover the small window (300) and that would replace the double.PositiveInfinity value. In the window, the Green points are where I had clicked on the screen with my mouse. The Red points are the Voronoi edge origins, and the lines are draw with vertex data.

Now that this is working I can hopefully do something cool with it.

Been wanting to create procedural map generation and after reading this post http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/ I decided to start learning how to do Voronoi Diagrams as a start.

I quickly realized this was extremely complicated and I didn’t feel like re-inventing the wheel so I searched for some code that was already completed and see if I could do something with it. Fortunately this guy > http://www.codeproject.com/Articles/11275/Fortune-s-Voronoi-algorithm-implemented-in-C did most of the work for me in C#. Now I just needed to figure out his code and then draw it.

Originally I was just going to use a Windows Form with a picturebox in that and do all the drawing there. But the picturebox does not like to add multiple images easily so instead I added in some XNA references and placed a handy XNA window inside the form. Now I can draw everything much much more easier.

The biggest hurdle was interpreting his code as it has almost 0 comments or documentation, additionally I also had to do a lot vector calculations and point placement since his code could not generate proper line data if there was less than 4 points. Some of the values were double.PositiveInfinity so I had to draw something but you cant draw Infinity, so what to do? Well luckily data included the direction as well as origin data, so…


public static Vector2 PolarToRectangular(float Distance, float Angle)
{
return new Vector2((float)(Distance * Math.Cos(Angle)), (float)(Distance * Math.Sin(Angle)));
}

I just put in a distance I felt could cover the small window (300) and that would replace the double.PositiveInfinity value. In the window, the Green points are where I had clicked on the screen with my mouse. The Red points are the Voronoi edge origins, and the lines are draw with vertex data.


Now that this is working I can hopefully do something cool with it.

Notes

  1. nbixel posted this