orderPaths.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding:utf-8 -*-
  2. ##
  3. # This file is part of connectSVG.
  4. #
  5. # connectSVG is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License,
  8. # or (at your option) any later version.
  9. #
  10. # connectSVG is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with connectSVG. If not, see <https://www.gnu.org/licenses/>.
  17. ##
  18. import logging
  19. log = logging.getLogger("connectsvg")
  20. def orderPaths(paths, attributes) :
  21. orderedPaths = []
  22. orderedAttributes = []
  23. last = 0j
  24. while len(paths) > 0 :
  25. minDist = 1000000000000
  26. place = -1
  27. for i in range(len(paths)) :
  28. path = paths[i]
  29. dist = abs(path.start.real-last.real) + abs(path.start.imag-last.imag)
  30. if minDist > dist :
  31. minDist = dist
  32. place = i
  33. last = paths[place].start
  34. orderedPaths.append(paths.pop(place))
  35. orderedAttributes.append(attributes.pop(place))
  36. return orderedPaths, orderedAttributes