Click here to Skip to main content
6,935,947 members and growing! (19,877 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate License: The Code Project Open License (CPOL)

Visualization of the 2D Voronoi Diagram and the Delaunay Triangulation

By Maxim_Barsuk

An add-on for the Fortune's algorithm.
C# (C#1.0, C#2.0, C#3.0), Windows (WinXP), .NET (.NET2.0, .NET3.0), Win32, Visual-Studio (VS2005, VS2008), GDI, WinForms, Dev
Posted:18 Nov 2008
Views:15,885
Bookmarked:26 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.56 Rating: 4.23 out of 5
1 vote, 8.3%
1

2
1 vote, 8.3%
3
1 vote, 8.3%
4
9 votes, 75.0%
5

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


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
Occupation: Software Developer
Location: Russian Federation Russian Federation

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralRays PinmemberProphetLOD12:01 26 Jun '09  
GeneralRe: Rays PinmemberMaxim_Barsuk20:13 28 Jun '09  
GeneralRe: Rays PinmemberProphetLOD6:35 3 Jul '09  
GeneralInteresting vision PinmemberFDemers23:13 11 May '09  
GeneralRe: Interesting vision PinmemberMaxim_Barsuk0:34 12 May '09  
GeneralRe: Interesting vision PinmemberFDemers0:52 12 May '09  
GeneralRe: Interesting vision PinmemberMaxim_Barsuk2:19 12 May '09  
GeneralRe: Interesting vision PinmemberFDemers2:28 12 May '09  
GeneralWPF? Pinmembersotona5:45 20 Nov '08  
GeneralRe: WPF? PinmemberMaxim_Barsuk7:20 20 Nov '08  
GeneralDelaunay PinmemberGünther M. FOIDL5:32 19 Nov '08  
Hi, great article (5)!

Comment:
Delaunay triangulation is very useful in computational fluid methods. So this is a good point to start visualization of these computations.

Cheers
Günther
GeneralRe: Delaunay PinmemberMaxim_Barsuk20:08 19 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by Maxim_Barsuk
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project