poly2tri/python/poly2tri.py

70 lines
2.5 KiB
Python
Raw Normal View History

2009-11-12 20:52:23 +01:00
#!/usr/bin/env python2.6
2009-11-20 18:13:19 +01:00
from framework import Game, draw_polygon, reset_zoom, draw_line
2009-11-17 20:21:30 +01:00
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-20 18:13:19 +01:00
file_name = "../data/star.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-20 18:13:19 +01:00
#self.trapezoids = seidel.trapezoids
self.trapezoids = seidel.trapezoidal_map.map
self.edges = seidel.edge_list
2009-11-18 19:58:59 +01:00
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-20 18:13:19 +01:00
reset_zoom(2.1, (400, 100), self.screen_size)
2009-11-17 20:21:30 +01:00
red = 255, 0, 0
2009-11-20 18:13:19 +01:00
yellow = 255, 255, 0
green = 0, 255, 0
2009-11-17 20:21:30 +01:00
for t in self.triangles:
draw_polygon(t, red)
2009-11-20 18:13:19 +01:00
2009-11-18 19:58:59 +01:00
'''
for t in self.trapezoids:
2009-11-20 18:13:19 +01:00
verts = self.trapezoids[t].vertices()
#verts = t.vertices()
2009-11-18 19:58:59 +01:00
draw_polygon(verts, yellow)
'''
2009-11-20 18:13:19 +01:00
for e in self.edges:
p1 = e.p.x, e.p.y
p2 = e.q.x, e.q.y
draw_line(p1, p2, green)
#draw_polygon(self.points, green)
2009-11-18 19:58:59 +01:00
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
2009-11-20 18:13:19 +01:00
spam = (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)
eggs = [474.80999000000003, 555.15656999999999], [474.80999000000003, 530.87086999999997], [509.09570000000002, 530.87086999999997], [543.38142000000005, 530.87086999999997], [543.38142000000005, 555.15656999999999], [543.38142000000005, 579.44227000000001], [509.09570000000002, 579.44227000000001], [474.80999000000003, 579.44227000000001]
2009-11-12 20:52:23 +01:00
if __name__ == '__main__':
demo = Poly2Tri()