Python triangle library

20250106 · active · verified Fri Apr 17

The `triangle` library provides Python bindings to Jonathan Shewchuk's robust C library for 2D Delaunay triangulation and constrained Delaunay triangulation, as well as high-quality mesh generation. It processes inputs like sets of points, segments, and holes to generate triangulations and meshes. The current version is 20250106, with a release cadence that appears to be on an as-needed basis, indicated by its date-based versioning.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Delaunay triangulation of a square region with a central hole. It defines the boundary using vertices and segments, specifies the hole's location, and then uses `triangle.triangulate` with quality and area constraints to generate a mesh. The output includes refined vertices, the triangle connectivity, and potentially refined segments.

import triangle
import numpy as np

# Define vertices for a square
points = np.array([
    [0.0, 0.0],
    [1.0, 0.0],
    [1.0, 1.0],
    [0.0, 1.0]
])

# Define segments (edges) connecting the vertices to form the boundary of the square
# Indices refer to the `points` array (0-indexed)
segments = np.array([
    [0, 1],
    [1, 2],
    [2, 3],
    [3, 0]
])

# Define a hole by a point within the region to be excluded from triangulation
holes = np.array([[0.5, 0.5]])

# Triangulate the region.
# The second argument is a string of options for the C library:
# 'p': Planar straight line graph (PSLG) triangulation
# 'q30': Quality mesh generation, minimum angle 30 degrees
# 'a0.01': Maximum triangle area attribute, 0.01
tri = triangle.triangulate({'vertices': points, 'segments': segments, 'holes': holes}, 'pq30a0.01')

print("--- Input ---")
print("Points:\n", points)
print("Segments:\n", segments)
print("Holes:\n", holes)
print("\n--- Output ---")
print("Vertices (may be refined):\n", tri['vertices'])
print("Triangles (indices into output vertices):\n", tri['triangles'])
print("Segments (may be refined):\n", tri['segments'])

view raw JSON →