Create triangular mesh from vertex coordinates
You can use scipy.spatial.Delaunay. Here is an example from the
import numpy as nppoints = np.array([[-1,1],[-1.3, .6],[0,0],[.2,.8],[1,.85],[-.1,-.4],[.4,-.15],[.6,-.6],[.9,-.2]])from scipy.spatial import Delaunaytri = Delaunay(points)import matplotlib.pyplot as pltplt.triplot(points[:,0], points[:,1], tri.simplices)plt.plot(points[:,0], points[:,1], 'o')plt.show()
Here is the result on an input similar to yours:
The triangles are stored in the simplices
attribute of the Delaunay object which reference the coordinates stored in the points
attribute:
>>> tri.pointsarray([[-1. , 1. ], [-1.3 , 0.6 ], [ 0. , 0. ], [ 0.2 , 0.8 ], [ 1. , 0.85], [-0.1 , -0.4 ], [ 0.4 , -0.15], [ 0.6 , -0.6 ], [ 0.9 , -0.2 ]])>>> tri.simplicesarray([[5, 2, 1], [0, 3, 4], [2, 0, 1], [3, 0, 2], [8, 6, 7], [6, 5, 7], [5, 6, 2], [6, 3, 2], [3, 6, 4], [6, 8, 4]], dtype=int32)
If you are looking for which vertices are connected, there is an attribute containing that info also:
>>> tri.vertex_neighbor_vertices(array([ 0, 4, 7, 12, 16, 20, 24, 30, 33, 36], dtype=int32), array([3, 4, 2, 1, 5, 2, 0, 5, 1, 0, 3, 6, 0, 4, 2, 6, 0, 3, 6, 8, 2, 1, 6, 7, 8, 7, 5, 2, 3, 4, 8, 6, 5, 6, 7, 4], dtype=int32))
You can try scipy.spatial.Delaunay. From that link:
points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])from scipy.spatial import Delaunaytri = Delaunay(points)plt.triplot(points[:,0], points[:,1], tri.simplices)plt.plot(points[:,0], points[:,1], 'o')plt.show()
Output: