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
|
|
|
|
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
|
|
|
|
file_name = "../data/star.dat"
|
|
|
|
points = self.load_points(file_name)
|
|
|
|
|
|
|
|
# Triangulate
|
|
|
|
t1 = self.time
|
|
|
|
seidel = Triangulator(points)
|
|
|
|
self.triangles = seidel.triangles()
|
|
|
|
dt = self.time - t1
|
|
|
|
print "time = %f , num triangles = %d" % (dt, len(self.triangles))
|
|
|
|
|
2009-11-12 20:52:23 +01:00
|
|
|
self.main_loop()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def render(self):
|
2009-11-17 20:21:30 +01:00
|
|
|
reset_zoom(1.0, (0,0), self.screen_size)
|
|
|
|
red = 255, 0, 0
|
|
|
|
for t in self.triangles:
|
|
|
|
draw_polygon(t, red)
|
|
|
|
|
|
|
|
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-12 20:52:23 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
demo = Poly2Tri()
|