Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Created August 10, 2014 11:37
Show Gist options
  • Save FLamparski/c6a20a742d392ec4c359 to your computer and use it in GitHub Desktop.
Save FLamparski/c6a20a742d392ec4c359 to your computer and use it in GitHub Desktop.
Using SciPy, generate a Delaunay-triangulated mesh between randomly distributed points, and display it using pyglet.
#!/usr/bin/env python
# vim: et:ts=4:sw=4
import random, pyglet
from pyglet.gl import *
from numpy import array
from scipy.spatial import Delaunay
N_POINTS = 200;
SCALE = 2;
def get_tris():
point_list = [(random.randint(0, N_POINTS * SCALE), random.randint(0, N_POINTS * SCALE)) for _ in range(N_POINTS)]
point_array = array(point_list) # Arrays are numpy/C types. They are fast but immutable.
tri = Delaunay(point_array)
tri_verts = []
for simplex in tri.simplices:
vert1 = tri.points[simplex[0]]
vert2 = tri.points[simplex[1]]
vert3 = tri.points[simplex[2]]
tri_verts.append(vert1)
tri_verts.append(vert2)
tri_verts.append(vert3)
return tri_verts
window = pyglet.window.Window()
tris = get_tris()
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glBegin(GL_TRIANGLES)
for tri in tris:
glVertex2f(*tri)
glEnd()
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment