krisajenkins / elm-astar / AStar

The A-Star pathfinding algorithm.


type alias Position =
( Basics.Int, Basics.Int )

A position is just a pair of (x,y) coordinates.


type alias Path =
List Position

A path is a List of Positions.

findPath : (Position -> Position -> Basics.Float) -> (Position -> Set Position) -> Position -> Position -> Maybe Path

Find a path between the start and end Positions. You must supply a cost function and a move function.

The cost function must estimate the distance between any two positions. It doesn't really matter how accurate this estimate is, as long as it never overestimates.

The move function takes a Position and returns a Set of possible places you can move to in one step.

If this function returns Nothing, there is no path between the two points. Otherwise it returns Just a List of steps from start to end.

Example usage.

 import AStar exposing (..)


 type World =
     ...your custom code...


 movesFrom : World -> Position -> Set Position
 movesFrom world (x,y) =
     ...your custom code...


 findPath
     straightLineCost
     (movesFrom currentWorld)
     ( 0, 0 ) ( 2, 0 )
 --> Just [ ( 1, 0 ), ( 2, 0 ) ]

straightLineCost : Position -> Position -> Basics.Float

A simple costing algorithm. Think of it as the number of moves a rook/castle would have to make on a chessboard.

pythagoreanCost : Position -> Position -> Basics.Float

An alternative costing algorithm, which calculates pythagorean distance.