Documentation
¶
Overview ¶
Package astar implements the A* shortest path finding algorithm.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CostFunc ¶
A CostFunc is a function that returns a cost for the transition from node a to node b.
type Graph ¶
type Graph[Node any] interface { // Neighbours returns the neighbour nodes of node n in the graph. Neighbours(n Node) []Node }
The Graph interface is the minimal interface a graph data structure must satisfy to be suitable for the A* algorithm.
type Path ¶
type Path[Node any] []Node
A Path is a sequence of nodes in a graph.
func FindPath ¶
func FindPath[Node comparable](g Graph[Node], start, dest Node, d, h CostFunc[Node]) Path[Node]
FindPath finds the shortest path between start and dest in graph g using the cost function d and the cost heuristic function h.
Example ¶
package main import ( "fmt" "image" "math" "github.com/fzipp/astar" ) func main() { // Create a graph with 2D points as nodes a := image.Pt(2, 3) b := image.Pt(1, 7) c := image.Pt(1, 6) d := image.Pt(5, 6) g := newGraph[image.Point]().link(a, b).link(a, c).link(b, c).link(b, d).link(c, d) // Find the shortest path from a to d p := astar.FindPath[image.Point](g, a, d, nodeDist, nodeDist) // Output the result if p == nil { fmt.Println("No path found.") return } for i, n := range p { fmt.Printf("%d: %s\n", i, n) } } // nodeDist is our cost function. We use points as nodes, so we // calculate their Euclidean distance. func nodeDist(p, q image.Point) float64 { d := q.Sub(p) return math.Sqrt(float64(d.X*d.X + d.Y*d.Y)) } // graph is represented by an adjacency list. type graph[Node comparable] map[Node][]Node func newGraph[Node comparable]() graph[Node] { return make(map[Node][]Node) } // link creates a bi-directed edge between nodes a and b. func (g graph[Node]) link(a, b Node) graph[Node] { g[a] = append(g[a], b) g[b] = append(g[b], a) return g } // Neighbours returns the neighbour nodes of node n in the graph. func (g graph[Node]) Neighbours(n Node) []Node { return g[n] }
Output: 0: (2,3) 1: (1,6) 2: (5,6)
Example (Maze) ¶
package main import ( "fmt" "image" "math" "github.com/fzipp/astar" ) func main() { maze := floorPlan{ "###############", "# # # # #", "# ### ### ### #", "# # # # # #", "### # # # ### #", "# # # #", "# # ### ### ###", "# # # # # #", "### # # # # ###", "# # # # #", "# # ######### #", "# # #", "# ### # # ### #", "# # # # #", "###############", } start := image.Pt(1, 13) // Bottom left corner dest := image.Pt(13, 1) // Top right corner // Find the shortest path path := astar.FindPath[image.Point](maze, start, dest, distance, distance) // Mark the path with dots before printing for _, p := range path { maze.put(p, '.') } maze.print() } // distance is our cost function. We use points as nodes, so we // calculate their Euclidean distance. func distance(p, q image.Point) float64 { d := q.Sub(p) return math.Sqrt(float64(d.X*d.X + d.Y*d.Y)) } type floorPlan []string // Neighbours implements the astar.Graph interface func (f floorPlan) Neighbours(p image.Point) []image.Point { offsets := []image.Point{ image.Pt(0, -1), // North image.Pt(1, 0), // East image.Pt(0, 1), // South image.Pt(-1, 0), // West } res := make([]image.Point, 0, 4) for _, off := range offsets { q := p.Add(off) if f.isFreeAt(q) { res = append(res, q) } } return res } func (f floorPlan) isFreeAt(p image.Point) bool { return f.isInBounds(p) && f[p.Y][p.X] == ' ' } func (f floorPlan) isInBounds(p image.Point) bool { return p.Y >= 0 && p.X >= 0 && p.Y < len(f) && p.X < len(f[p.Y]) } func (f floorPlan) put(p image.Point, c rune) { f[p.Y] = f[p.Y][:p.X] + string(c) + f[p.Y][p.X+1:] } func (f floorPlan) print() { for _, row := range f { fmt.Println(row) } }
Output: ############### # # # #.# # ### ### ###.# # # # # #.# ### # # # ###.# # # # .......# # # ###.### ### # # #.# # # ### # #.# # ### # #..... # # # # #.######### # #... # # #.### # # ### # #. # # # # ###############
Click to show internal directories.
Click to hide internal directories.