**Sjaak Priester**on

**December 28th, 2004**

For one of my projects, I needed the so-called Delaunay triangulation of a set of points.

Loosely put, the Delaunay triangulation is the most efficient way to draw triangles between pairs of points. Each point is connected by lines to its closest neighbours, in such a way that all line parts form triangles, and do not intersect otherwise. No triangles overlap; in fact, the surface is completely covered with one nice layer of different triangular tiles.

The Delaunay triangulation was invented in 1934 by, and named after, the Russian mathematician Boris Nikolaevich Delaunay (1890-1980). It has a lot of applications in science and computer graphics. It is often used in the graphic representation of geometrically irregularly distributed data—think weather maps or altitude maps. Its 3D-variant is important in creating virtual worlds for video games, among many other things.

Although at first glance, obtaining the Delaunay triangulation seems to be almost trivial, in fact it's a quite complicated task, the more so if you want to do it efficiently for greater numbers of points. Quite a lot of ingenious algorithms have been devised, and the field is the subject of ongoing research.

I searched the Internet for Delaunay triangulation (and the closely related Voronoi or Direchlet diagrams), and found loads of algorithms. However, most of them were unsatisfactory. Some consisted of hundreds or even thousands of lines of incomprehensive spaghetti code. Some were much too heavy, sporting lots of options to create triangulations with special constraints. Others were optimized in a way that obfuscated the underlying method completely.

So, I ended up in creating my own implementation. I present it here because, perhaps, someday someone else will be in need of a Delaunay triangulation. But also because I think it's a nice and intriguing problem in itself. Moreover, it shows some nice tricks with the Standard Template Library (STL).

### The Algorithm

The overall algorithm leans heavily on an important property of Delaunay triangulations, that is: *Apart from the vertices, there are no other points on or inside the circumscribed circle of any triangle*. In other words, *all circumscribed circles are empty*.

Knowing that, and thinking hard, you can devise the following way to add one vertex to an already existing triangulation (in pseudo-code).

add_vertex(vertex) { for (eachtriangle) { if (vertexis insidetriangle's circumscribed circle) { storetriangle's edges inedgebufferremovetriangle} } remove all double edges fromedgebuffer, keeping only unique ones for (eachedgeinedgebuffer) { form a new triangle betweenedgeandvertex} }

In pictures, it looks like this:

To insert a new vertex... | ... first, find which triangles have a circumscribed circle encompassing the vertex. |

Remove those triangles, but remember their edges. | Remove double edges, keeping only the unique ones. |

Form new triangles between the remaining edges and the new vertex... | ... and, finally, put those new triangles back. |

Now, you just have to find a way to start the whole process. Somehow, you should create a valid Delaunay triangulation to begin with. You then successively add all your vertices.

This valid initial triangulation is easy to find. You just make a big "super triangle" that encompasses all your vertices. Of course, it means that superfluous triangles will be formed, but they can be removed afterwards easily.

The complete algorithm thus amounts to the following in pseudo-code.

triangulate() { createsupertriangleand add it to the triangulation for (eachvertex) { add_vertex(vertex) } for (eachtriangle) { if (one or more vertices stem fromsupertriangle) { removetriangle} } }

### Optimizations

The algorithm can be optimized by pre-sorting the vertices along the horizontal axis. It is then possible to successively render triangles "completed," as soon as their circumscribed circles are completely to the left of the current vertex. I implemented this optimization. Its effect is hardly noticeable for small numbers of vertices, but for larger numbers it is dramatic. I do the sorting simply by using the *std::set* container, which keeps its object sorted automatically.

I also pre-calculated the center and the radius of the circumscribed circles. These optimizations are less important, because they don't enhance the fundamental algorithm. However, they do speed it up considerably.

### Not Perfect

The algorithm is not perfect. In particular, it doesn't handle situations with more than three vertices lying on one circle very well. I found it out the hard way, after investigating the comment of Roger Labbe, below. Admittedly, I made a mistake in the first version by incorrectly assuming that no two triangles could have the same circumcircle. I corrected that, but there is still a problem in the unlikely situation that a lot of vertices happen to lie on the same circle.

### Implementation

I implemented the whole thing in a C++ class *Delaunay*, that can be used in MFC and non-MFC environments. To keep my code clean and well structured, I used several features of the STL, such as function objects and seldom-used algorithms such as *std::remove_copy_if()*.

I won't guarantee that it will fit seamlessly in any conceivable project, but it certainly will be easier to use than some of the other implementations around. Also, please notice that this isn't the best implementation possible. In particular, it doesn't deal with contours and holes in the set of vertices. As such, it is only a starting point.

### Demo

The demo is nothing special. It's an MFC application, displaying a Delaunay triangulation of random points. Just for fun, I used some of those nifty STL function objects in the demo code as well.

### References

As stated, there is much information about Delaunay triangulation on the Internet. Much of it, though, is of an academic nature, exploring the many interesting mathematical properties. Some of the more accessible sites are:

- Triangulate, by Paul Bourke.
- Delauny triangulation, college notes by Leila De Floriani (PDF).
- Geometry in action. Many links to other sites.

## Comments

## Improvement for convex result

Posted byairprojecton04/09/2013 12:52pmAs some already noticed, there is an issue with this algorithm. In some particular cases of vertices positions, the resulting set of triangles might create concave structure. The cause of such behaviour is the initial supertriangle. In this implementation, it is reduced to the size that is just enough to enclose all vertices. This makes it highly probable to have a significant triangles formed with the supertriangle vertices, and have them deleted in the finalizing step. If the supertriangle was much bigger than the spread of the input vertces (dx, dy), the algorithm would create the triangles in a convex fashion. Simple solution found in Gilles Dumoulin's implementation is to initialize the supertriangle as follows: REAL dMax = (dx dy) ? dx : dy; REAL xMid = (xMax + xMin) / 2.0; REAL yMid = (yMax + yMin) / 2.0; vSuper[0] = Vertex(xMid - 20 * dMax, yMid - dMax); vSuper[1] = Vertex(xMid, yMid + 20 * dMax); vSuper[2] = Vertex(xMid + 20 * dMax,yMid - dMax); This mod should sort out the convexity issue.

Reply## artist

Posted byDenyse Le Blancon01/05/2013 06:23pmOne day while just rinsing my eyes in admiration with Robert Delaunay, I came upon Delaunay triangulation. Could you send me a download to triangulate pictures? thank you Denyse

Reply## Worked great

Posted byJan Ekholmon08/22/2012 07:12amI implemented the algorithm just to test how the Delaunay algorithm works and it seems to work perfectly. The only small issue is the need for the "super triangle(s)". You do not always have those bounding triangles when you start. Anyway, the visualization is really great and made it absolutely trivial to implement!

Reply## performance time

Posted byposthumuson06/29/2012 10:09amhey the time displayed at the bottom-left corner of the screen...... is it the time of the -- total process - ( triangulation + display) or only triangulation

ReplyFrancois Bilodeauon04/08/2012 07:10pmReal great and so fast. I compile it with Visual Studio and it worked But I want to integrate Delaunay.cpp with a project in QT I am using the QT4.8 sdk by nokia and compiling with minGW Everything goes well but for 2 lines using the same function remove_if in you code around line 273: tIterator itEnd = remove_if(workset.begin(), workset.end(), triangleIsCompleted(itVertex, output, vSuper)); I get this message from minGW: c:\qtsdk\mingw\bin\..\lib\gcc\mingw32\4.4.0\include\c++\bits\stl_algo.h:-1: In function '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::_Rb_tree_const_iterator, _Predicate = triangleIsCompleted]': c:\qtsdk\mingw\bin\..\lib\gcc\mingw32\4.4.0\include\c++\bits\stl_algo.h:1161: erreur : passing 'const triangle' as 'this' argument of 'triangle