functions.go 733 B

1234567891011121314151617181920212223242526272829303132
  1. package astar0
  2. import "math"
  3. // ManhattanFunc 曼哈顿
  4. func ManhattanFunc(node, end *Node) int32 {
  5. x := Abs(node.x - end.x)
  6. y := Abs(node.y - end.y)
  7. return (x + y) * straightCost
  8. }
  9. // DiagonalFunc 对角线
  10. func DiagonalFunc(node, end *Node) int32 {
  11. x := Abs(node.x - end.x)
  12. y := Abs(node.y - end.y)
  13. value := Min(x, y)
  14. return value*diagonalCost + Abs(x-y)*straightCost
  15. }
  16. // EuclideanFunc 欧几里得
  17. func EuclideanFunc(node, end *Node) int32 {
  18. x := Abs(node.x - end.x)
  19. y := Abs(node.y - end.y)
  20. v := float64(x)*float64(x) + float64(y)*float64(y)
  21. return int32(math.Sqrt(v) * float64(straightCost))
  22. }
  23. func IsWalkableFunc() func(x, y, value int32) bool {
  24. return func(x, y, value int32) bool {
  25. return value == 0
  26. }
  27. }