Click here to Skip to main content
Licence CPOL
First Posted 18 Nov 2008
Views 36,587
Downloads 1,094
Bookmarked 29 times

Visualization of the 2D Voronoi Diagram and the Delaunay Triangulation

By | 18 Nov 2008 | Article
An add-on for the Fortune's algorithm.

Introduction

This example uses a good implementation of the Fortune's algorithm performed by BenDi (see here). The goal of this application is the visualization of the Voronoi diagram.

Background

For more information, see these articles on Wikipedia:

Using the code

The solution for the visualization problem is very easy. We add two static methods on the Fortune class:

/// <summary>
/// Visualization of 2D Voronoi map.
/// </summary>
/// <param name="weight">Weight of result image.</param>
/// <param name="height">Height of result image.</param>
/// <param name="Datapoints">Array of data points.</param>
/// <returns>Result bitmap.</returns>
public static Bitmap GetVoronoyMap(int weight, int height, IEnumerable Datapoints)
{
    Bitmap bmp = new Bitmap(weight, height);
    VoronoiGraph graph = Fortune.ComputeVoronoiGraph(Datapoints);
    Graphics g = Graphics.FromImage(bmp);
    foreach (object o in graph.Vertizes)
    {
        Vector v = (Vector)o;
        g.DrawEllipse(Pens.Black, (int)v[0]-2, (int)v[1]-2, 4, 4);
    }
    foreach (object o in Datapoints)
    {
        Vector v = (Vector)o;
        g.DrawEllipse(Pens.Red, (int)v[0]-1, (int)v[1]-1, 2, 2);
    }
    foreach (object o in graph.Edges)
    {
        VoronoiEdge edge = (VoronoiEdge)o;
        try
        {
            g.DrawLine(Pens.Brown, (int)edge.VVertexA[0], 
              (int)edge.VVertexA[1], (int)edge.VVertexB[0], 
              (int)edge.VVertexB[1]);
        }
        catch { }
    }
    return bmp;
}

/// <summary>
/// Visualization of Delaunay Triangulation
/// </summary>
/// <param name="weight">Weight of result image.</param>
/// <param name="height">Height of result image.</param>
/// <param name="Datapoints">Result bitmap.</param>
/// <returns></returns>
public static Bitmap GetDelaunayTriangulation(int weight, 
              int height, IEnumerable Datapoints)
{
    Bitmap bmp = new Bitmap(weight, height);
    VoronoiGraph graph = Fortune.ComputeVoronoiGraph(Datapoints);
    Graphics g = Graphics.FromImage(bmp);
    foreach (object o in Datapoints)
    {
        Vector v = (Vector)o;
        g.DrawEllipse(Pens.Red, (int)v[0] - 1, (int)v[1] - 1, 2, 2);
        foreach (object obj in graph.Edges)
        {
            VoronoiEdge edge = (VoronoiEdge)obj;
            if ((edge.LeftData[0] == v[0])&(edge.LeftData[1] == v[1]))
            {
                g.DrawLine(Pens.Black, (int)edge.LeftData[0], (int)edge.LeftData[1], 
                          (int)edge.RightData[0], (int)edge.RightData[1]);
            }
        }
    }
    return bmp;
}

And now, we have images with diagrams:

delane.JPG

Figure 1.Delaunay triangulation.

voronoi.JPG

Figure 2.Voronoi diagram.

Points of interest

Voronoi diagram is a very useful thing. It has specially interesting applications on terrain generation. I would like to develop a simple terrain generation algorithm based on the Voronoi diagram in future.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Maxim_Barsuk

Software Developer

Russian Federation Russian Federation

Member

Hello! My name is Maxim Subbotin.
 
Now I work in sphere of web-development. I'm interesting researches in SEO field.
If you interesting, you can see this tool:
 
KeywordCompetitor

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy when I draw 2 points I don't see a line Pinmemberseb.4923:49 30 May '12  
NewsThis algorithm fails when presented with a large number of points with obtuse angles PinmemberTomcat_101010101016:05 14 Sep '10  
GeneralRays PinmemberProphetLOD11:01 26 Jun '09  
GeneralRe: Rays PinmemberMaxim_Barsuk19:13 28 Jun '09  
GeneralRe: Rays PinmemberProphetLOD5:35 3 Jul '09  
GeneralInteresting vision PinmemberFDemers22:13 11 May '09  
GeneralRe: Interesting vision PinmemberMaxim_Barsuk23:34 11 May '09  
Hello! Dynamic geography - its new discipline for me. I must think about finding methods that can help in solving this problem. Please, give me links to material about this problem. Smile | :)
GeneralRe: Interesting vision PinmemberFDemers23:52 11 May '09  
GeneralRe: Interesting vision PinmemberMaxim_Barsuk1:19 12 May '09  
GeneralRe: Interesting vision PinmemberFDemers1:28 12 May '09  
QuestionWPF? Pinmembersotona4:45 20 Nov '08  
AnswerRe: WPF? PinmemberMaxim_Barsuk6:20 20 Nov '08  
GeneralDelaunay PinmemberGünther M. FOIDL4:32 19 Nov '08  
GeneralRe: Delaunay PinmemberMaxim_Barsuk19:08 19 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120604.1 | Last Updated 19 Nov 2008
Article Copyright 2008 by Maxim_Barsuk
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid