astar

package module
v0.0.0-...-b38c70a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2012 License: BSD-2-Clause Imports: 5 Imported by: 1

README

astar

A small package that implements the A* (A star) pathfinding algorithm.

Example

import (
	"fmt"
	"github.com/sperre/astar"
)

func main() {
	// If your map is something more than 2D matrix then you might want to modify adjacentNodes
	map_data := astar.GetMapFromImage(test.png)

	// Returns a list of nodes from start to stop avoiding all, obstacles if possible
	startx, starty :=  0,  0
	stopx, stopy   := 99, 99
	dir8 := false // Use 4 directions, not 8
	shortest_path := astar.Astar(map_data, startx, starty, stopx, stopy, dir8)
}

Origin

This library was developed for Eurobot-NTNU 2012, based on code from: https://github.com/humanfromearth/gopathfinding

Compared to the original library, our version was benchmarked to run approximately 27x faster.

Documentation

Index

Constants

View Source
const (
	LAND = 1 << iota
	WALL
)

Tile information

View Source
const (
	COST_STRAIGHT = 1000
	COST_DIAGONAL = 1414
)

Tile movement cotsts

Variables

This section is empty.

Functions

func Heuristic

func Heuristic(tile, stop *Node) (h int)

Diagonal/Chebyshev distance is used.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Start, stop nodes and a slice of nodes

func NewGraph

func NewGraph(map_data MapData) *Graph

Return a Graph from a map of coordinates (those that are passible)

func (*Graph) Node

func (g *Graph) Node(x, y int) *Node

Get or create a *Node based on x, y coordinates. Avoids duplicated nodes!

type MapData

type MapData [][]int

func GetMapFromImage

func GetMapFromImage(filename string) MapData

func NewMapData

func NewMapData(rows, cols int) MapData

Return a new MapData by value given some dimensions

func (MapData) Clone

func (m MapData) Clone() MapData

type Node

type Node struct {
	X, Y int
	// contains filtered or unexported fields
}

X and Y are coordinates, parent is a link to where we came from. cost are the estimated cost from start along the best known path. h is the heuristic value (air line distance to goal).

func Astar

func Astar(map_data MapData, startx, starty, stopx, stopy int, dir8 bool) []*Node

A* search algorithm. See http://en.wikipedia.org/wiki/A*_search_algorithm

func NewNode

func NewNode(x, y int) *Node

Create a new Node

func (*Node) String

func (n *Node) String() string

Return string representation of the node

type PriorityQueue

type PriorityQueue []*Node

A PriorityQueue implements heap.Interface and holds Items.

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

sort.Interface

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) PopNode

func (pq *PriorityQueue) PopNode() *Node

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

heap.interface

func (*PriorityQueue) PushNode

func (pq *PriorityQueue) PushNode(n *Node)

func (*PriorityQueue) RemoveNode

func (pq *PriorityQueue) RemoveNode(n *Node)

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL