utils.py 1005 B

12345678910111213141516171819202122232425262728293031323334353637
  1. ANGLES = {
  2. 'N': (-23, 22),
  3. 'NO': (292, 337),
  4. 'O': (247, 292),
  5. 'SO': (202, 247),
  6. 'S': (157, 202),
  7. 'SE': (112, 157),
  8. 'E': (67, 112),
  9. 'NE': (22, 67)
  10. }
  11. ORIENTATIONS = ANGLES.keys()
  12. def merge_intervals(l, wrap=360):
  13. """Merge a list of intervals, assuming the space is cyclic. The
  14. intervals should already by sorted by start value."""
  15. if l == []:
  16. return []
  17. result = list()
  18. # Transform the 2-tuple into a 2-list to be able to modify it
  19. result.append(list(l[0]))
  20. for (start, stop) in l:
  21. current = result[-1]
  22. if start > current[1]:
  23. result.append([start, stop])
  24. else:
  25. result[-1][1] = max(result[-1][1], stop)
  26. if len(result) == 1:
  27. return result
  28. # Handle the cyclicity by merging the ends if necessary
  29. last = result[-1]
  30. first = result[0]
  31. if first[0] <= last[1] - wrap:
  32. result[-1][1] = max(result[-1][1], first[1] + wrap)
  33. result.pop(0)
  34. return result