Skip to content

Instantly share code, notes, and snippets.

@douglasjacobsen
Created March 11, 2014 15:44
Show Gist options
  • Save douglasjacobsen/9488470 to your computer and use it in GitHub Desktop.
Save douglasjacobsen/9488470 to your computer and use it in GitHub Desktop.
Make a random delaunay triangulation and write it to files. http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html
import sys, os, glob, shutil, numpy
from random import *
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
nCells = 500
points = numpy.zeros((nCells, 2))
seed()
for i in numpy.arange(0,nCells):
x = random()
y = random()
points[i,0] = x * 2.0 - 1.0
points[i,1] = y * 2.0 - 1.0
tri = Delaunay(points)
plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
plt.plot(points[:,0], points[:,1], 'o')
plt.show()
triangulation = open('triangles.dat', 'w+')
for i in numpy.arange(0, len(tri.simplices)):
triangulation.write("%d %d %d\n"%(tri.simplices[i,0]+1, tri.simplices[i,1]+1, tri.simplices[i,2]+1))
triangulation.close()
cells = open('cells.dat', 'w+')
for i in numpy.arange(0, nCells):
cells.write("%f %f 0.0\n"%(points[i,0], points[i,1]))
cells.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment