krisajenkins/elm-astar/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

AStar

The A* pathfinding algorithm.

type alias Position =
( Int, Int )

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

type alias Path =
List Position

A path is a List of Positions.

(Position -> Position -> 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 underestimates.

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 an Array 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
     horizontalCost
     (movesFrom  currentWorld)
     ( 0, 0 ) ( 2, 0 )
 --> Just [ ( 1, 0 ), ( 2, 0 ) ]
horizontalCost : Position -> Position -> Float

A simple costing algorithm. Think of it as the number of moves a rook/castle would have to make on a chessboard. Even if your piece can move diagonally it will still work! A* only requires that the cost function never underestimates.

pythagoreanCost : Position -> Position -> Float

An alternative costing algorithm, which calculates pythagorean distance.