🎨 A* 示例测试实现

This commit is contained in:
kercylan98 2023-06-20 11:17:40 +08:00
parent 3e07449c87
commit 46c262574b
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package astar_test
import (
"fmt"
"github.com/kercylan98/minotaur/utils/astar"
"github.com/kercylan98/minotaur/utils/geometry"
)
type Graph struct {
geometry.FloorPlan
}
func (slf Graph) Neighbours(point geometry.Point[int]) []geometry.Point[int] {
neighbours := make([]geometry.Point[int], 0, 4)
for _, direction := range geometry.DirectionUDLR {
np := geometry.GetDirectionNextWithCoordinateArray(direction, point)
if slf.IsFree(np) {
neighbours = append(neighbours, np)
}
}
return neighbours
}
func ExampleFind() {
graph := Graph{
FloorPlan: geometry.FloorPlan{
"===========",
"X XX X X",
"X X XX X",
"X XX X",
"X XXX X",
"X XX X X",
"X XX X X",
"===========",
},
}
paths := astar.Find[geometry.Point[int], int](graph, geometry.NewPoint(1, 1), geometry.NewPoint(8, 6), func(a, b geometry.Point[int]) int {
return geometry.CalcDistance(geometry.DoublePointToCoordinate(a, b))
}, func(a, b geometry.Point[int]) int {
return geometry.CalcDistance(geometry.DoublePointToCoordinate(a, b))
})
for _, path := range paths {
graph.Put(path, '.')
}
fmt.Println(graph)
// Output:
// ===========
// X.XX X X
// X. X XX X
// X.XX......X
// X.... XXX.X
// X XX X ..X
// X XX X . X
// ===========
}