 # Delaunay Triangles 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 (each triangle)
{
if (vertex is inside triangle's circumscribed circle)
{
store triangle's edges in edgebuffer
remove triangle
}
}
remove all double edges from edgebuffer,
keeping only unique ones
for (each edge in edgebuffer)
{
form a new triangle between edge and vertex
}
}
```

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()
{
create supertriangle and add it to the triangulation
for (each vertex)
{
}
for (each triangle)
{
if (one or more vertices stem from supertriangle)
{
remove triangle
}
}
}
```

### 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: