heuristic.py 667 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. import math
  3. from .util import SQRT2
  4. def null(dx, dy):
  5. """
  6. special heuristic for Dijkstra
  7. return 0, so node.h will always be calculated as 0,
  8. distance cost (node.f) is calculated only from
  9. start to current point (node.g)
  10. """
  11. return 0
  12. def manhatten(dx, dy):
  13. """manhatten heuristics"""
  14. return dx + dy
  15. def euclidean(dx, dy):
  16. """euclidean distance heuristics"""
  17. return math.sqrt(dx * dx + dy * dy)
  18. def chebyshev(dx, dy):
  19. """ Chebyshev distance. """
  20. return max(dx, dy)
  21. def octile(dx, dy):
  22. f = SQRT2 - 1
  23. if dx < dy:
  24. return f * dx + dy
  25. else:
  26. return f * dy + dx