root/trunk/RBRapier/RBRapier/Tools/Geometry/ConvexPolygonTesselation.py

Revision 366, 2.2 kB (checked in by sholloway, 6 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #~ Definitions
28 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
30 def TesselateConvexPolygon(Indexes):
31     """
32     >>> TesselateConvexPolygon(range(7))
33     [[0, 1, 6], [2, 6, 1], [6, 2, 5], [3, 5, 2], [5, 3, 4]]
34     >>> TesselateConvexPolygon(range(6))
35     [[0, 1, 5], [2, 5, 1], [5, 2, 4], [3, 4, 2]]
36     """
37     count = len(Indexes)
38     order = range(1,count)
39     order.append(0)
40     if count & 1: count = count//2
41     else: count = count//2-1
42     order0, order1 = order[count:], order[:count]
43     order0.reverse()
44     order = [x for y in map(None, order0, order1) for x in y if x is not None]
45
46     result = []
47     for i in xrange(len(order)-2):
48         result.append([Indexes[idx] for idx in order[i:i+3]])
49         if i & 1: result[-1].reverse()
50     return result
51
52 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53 #~ Title
54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
56 try: import psyco
57 except ImportError: pass
58 else:
59     psyco.bind(TesselateConvexPolygon)
60
61 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62 #~ Testing
63 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64
65 if __name__=='__main__':
66     print "Testing..."
67     import doctest
68     import ConvexPolygonTesselation as _testmod
69     doctest.testmod(_testmod)
70     print "Test complete."
71
Note: See TracBrowser for help on using the browser.