poly2tri/python/poly2tri.py

61 lines
2.0 KiB
Python
Raw Normal View History

2009-11-12 20:52:23 +01:00
#!/usr/bin/env python2.6
2009-11-17 20:21:30 +01:00
from framework import Game, draw_polygon, reset_zoom
from seidel import Triangulator
2009-11-12 20:52:23 +01:00
class Poly2Tri(Game):
screen_size = 800.0, 600.0
def __init__(self):
2009-11-17 20:21:30 +01:00
super(Poly2Tri, self).__init__(*self.screen_size)
# Load point set
2009-11-19 16:48:56 +01:00
file_name = "../data/nazca_monkey.dat"
2009-11-18 19:58:59 +01:00
self.points = self.load_points(file_name)
2009-11-17 20:21:30 +01:00
# Triangulate
t1 = self.time
2009-11-18 19:58:59 +01:00
seidel = Triangulator(self.points)
dt = (self.time - t1) * 1000.0
2009-11-17 20:21:30 +01:00
self.triangles = seidel.triangles()
2009-11-18 19:58:59 +01:00
self.trapezoids = seidel.trapezoids
#self.trapezoids = seidel.trapezoidal_map.map
print "time (ms) = %f , num triangles = %d" % (dt, len(self.triangles))
2009-11-17 20:21:30 +01:00
2009-11-12 20:52:23 +01:00
self.main_loop()
def update(self):
pass
def render(self):
2009-11-19 18:27:34 +01:00
reset_zoom(7, (0, 0), self.screen_size)
2009-11-17 20:21:30 +01:00
red = 255, 0, 0
for t in self.triangles:
draw_polygon(t, red)
2009-11-18 19:58:59 +01:00
green = 0, 255, 0
2009-11-19 18:27:34 +01:00
draw_polygon(self.points, green)
2009-11-18 19:58:59 +01:00
'''
yellow = 255, 255, 0
for t in self.trapezoids:
#verts = self.trapezoids[key].vertices()
verts = t.vertices()
draw_polygon(verts, yellow)
'''
2009-11-17 20:21:30 +01:00
def load_points(self, file_name):
infile = open(file_name, "r")
points = []
while infile:
line = infile.readline()
s = line.split()
if len(s) == 0:
break
points.append((float(s[0]), float(s[1])))
return points
2009-11-19 18:27:34 +01:00
bad = (544.80998999999997, 579.86046999999996), (544.80998999999997, 450.57477), (594.09569999999997, 450.57477), (643.38142000000005, 450.57477), (643.38142000000005, 525.26486999999997), (643.38142000000005, 599.95487000000003), (603.67391999999995, 654.55056999999999), (563.96655999999996, 709.14621999999997), (554.38819999999998, 709.14621999999997), (544.80998999999997, 709.14621999999997)
2009-11-12 20:52:23 +01:00
if __name__ == '__main__':
demo = Poly2Tri()