Delaunay Triangulation Algorithm in Python

Delaunay Triangulation is an algorithm of conceptual geometry used to create triangulation of different points in a 2D or 3D space. This algorithm is used in multiple fields like computer graphics, image processing, etc. The basic principle of this algorithm is that the triangles in the triangulation must not have any points inside the triangle (circumcircle). It confirms the uniformity of the triangle in shape and size. This algorithm is generally used in image processing, numerical simulation, and image processing.

Delaunay Triangulation is one of the computational geometry algorithms that provides the best quality triangulation compared to other triangulation methods.

Python provides some modules for implementing the Delaunay triangulation to create different triangulations with different points.

Methods and Modules Used for Delaunay Triangulation Algorithm in Python

Python offers the following methods and modules for the Delaunay triangulation:

  • Scipy: The Scipy library of Python is an open-source library that provides different algorithms for optimal solutions for scientific and technical computations. It stands for Scientific Python. It also provides various other functionalities, like image and signal processing, integration, interpolation, etc. It also provides a method for implementing Delaunay triangulation for a set of points.
  • Matplotlib: This package of Python is used to create animated and interactive visualisations. It is used to make interactive and informative bar charts, scatter plots, and 2D and 3D plots. Matplotlib is also used to plot triangulation in 3D space. We will use the plot_trisurf function to plot the triangulation and the show( ) function to display it.

These are some methods provided by these libraries for creating triangulation:

  • tri.Triangulation( ): This method is used for creating triangulation in matplotlib.
  • pyplot.triplot( ): This method is used to create a 2D plot of triangulation.
  • pyplot.show( ): This function is used to display plots in a window.
  • spatial.Delaunay( ): This function takes a set of points as a parameter. It returns the Delaunay triangulation of these points.

Now, let's start implementing Delaunay triangulation in Python:

We will create different interactive triangulations in 3D space.

Step 1: Importing Libraries

We will import the necessary libraries to start creating triangulation.

Code:

Explanation:

We have imported a few libraries like numpy, matplotlib, and Delaunay method from the scipy library. It is a method under the spatial class.

Step 2: Getting the 2D point dataset

We will generate a random dataset of 2D points using the random function of the numpy library.

Code:

Explanation:

We have taken a variable and stored random values in the given range (199, 5).

Step 3: Implementing Delaunay triangulation

We will use the Delaunay method to get the Delaunay triangulation.

Code:

Output:

<scipy.spatial._qhull.Delaunay at 0x1982ebdd410>

Explanation:

We initialised a variable and then performed the Delaunay triangulation with the random values. After printing the variable of the triangulation, we get that the spatial module of the scipy library has made the Delaunay object at some location.

Step 4: Visualisation

Using the matplotlib, we will plot the Delaunay triangulation.

Code:

Output:

Delaunay Triangulation Algorithm in Python

Explanation:

We have plotted a triplot of the Delaunay triangulation points. As an output, it will give a combination of different triangles made according to the points.

Let's understand the Delaunay triangulation with the help of examples.

Example 1:

We will use different Python libraries and methods to make Delaunay triangulation with different sets of 2D points.

Code:

Output:

Delaunay Triangulation Algorithm in Python

Explanation:

We have imported all the required libraries. Then, we performed the Delaunay triangulation on the set of 2D array points and visualised it using matplotlib. Along with the triangulation, we have added a scatter plot and displayed it with the show( ) function.

Example 2:

We will use different Python libraries and methods to make Delaunay triangulation with different sets of points. Here, we are using 14 sets of points.

Code:

Output:

Delaunay Triangulation Algorithm in Python

Explanation:

We have imported all the required libraries. Then, we performed the Delaunay triangulation on different sets of points and visualised it using matplotlib. We have used 14 sets of points. Along with the triangulation, we have added a scatter plot and displayed it with the show( ) function.

Example 3:

We will use different Python libraries and methods to make Delaunay triangulation with different sets of points. Here, we are using 9 sets of points.

Code:

Output:

Delaunay Triangulation Algorithm in Python

Explanation:

We have imported all the required libraries. Then, we performed the Delaunay triangulation on different sets of points and visualised it using matplotlib. We have used 9 sets of points. Along with the triangulation, we have added a scatter plot and displayed it with the show( ) function.