add paths.py

This commit is contained in:
Michael Fogleman 2017-01-07 16:23:15 -05:00
parent 9f4f79eb64
commit a2928bb9c7
1 changed files with 26 additions and 0 deletions

26
axi/paths.py Normal file
View File

@ -0,0 +1,26 @@
from spatial import Index
def sort_paths(paths, reversable=True):
first = paths[0]
paths.remove(first)
result = [first]
points = []
for path in paths:
x1, y1 = path[0]
x2, y2 = path[-1]
points.append((x1, y1, path, False))
if reversable:
points.append((x2, y2, path, True))
index = Index(points)
while index.size > 0:
x, y, path, reverse = index.search(result[-1][-1])
x1, y1 = path[0]
x2, y2 = path[-1]
index.remove((x1, y1, path, False))
if reversable:
index.remove((x2, y2, path, True))
if reverse:
result.append(list(reversed(path)))
else:
result.append(path)
return result