Skip to content

Commit

Permalink
Docs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
eskil committed Feb 4, 2024
1 parent b298cd8 commit 8557f59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
33 changes: 26 additions & 7 deletions Quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ There's four steps to do a 2D polygon map path search
1. Extend the graph with your start and stop
1. Use Astar to find a path

You can try out the code below in iex.

```shell
iex -S mix
```

```elixir
alias Scurry.PolygonMap
alias Scurry.Vector
alias Scurry.Astar

## Step 1
## Step 1 - define the world

# The world is defined by a boundary box plus holes inside the box.

# World boundary
polygon = [{0, 0}, {100, 0}, {100, 100}, {0, 100}]
Expand All @@ -25,21 +33,25 @@ holes = [
[{60, 30}, {70, 30}, {70, 70}, {60, 70}],
]

## Step 2
## Step 2 - convert world to graph

# The cost function for the graph, euclidian distance
cost_fun = fn a, b -> Vector.distance(a, b) end
# To compute a path, we need to convert the boundary & holes into
# a graph of all corners.

# The heuristic function for A-star, euclidian distance as well
heur_fun = fn a, b -> Vector.distance(a, b) end
# The cost function for the graph, this uses basic euclidian distance
# as a basic example. This is used to compute the graph of the world.
cost_fun = fn a, b -> Vector.distance(a, b) end

# Find the reachable vertices
vertices = PolygonMap.get_vertices(polygon, holes)

# Make a graph of them using an optional cost_fun
graph = PolygonMap.create_graph(polygon, holes, vertices, cost_fun)

## Step 3
## Step 3 - extend graph with actor start/stop

# For an actor to move from a to b in the world, we first extend the
# graph with start and stop

# Define our start & stop
{start, stop} = {{10, 10}, {90, 90}}
Expand All @@ -51,6 +63,13 @@ graph = PolygonMap.create_graph(polygon, holes, vertices, cost_fun)

## Step 4

# Given the new graph, finally compute the path in the world.

# The heuristic function for A-star, this uses euclidian distance as well.
# This is used as the cost of each edge traversal in the graph during a-star
# path finding. It can be the same as cost_fun but it does not have to be.
heur_fun = fn a, b -> Vector.distance(a, b) end

# Astar search
astar = Astar.search(search_graph, start, stop, heur_fun)
path = Astar.path(astar)
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $ mix deps.get

## Internals

There's better API documentation than this see [the hexdocs](https://hexdocs.pm/scurry).
*There's better API documentation than this see [the hexdocs](https://hexdocs.pm/scurry), especially see the [quickstart](Quickstart.md).*

```
mix docs
Expand Down Expand Up @@ -183,7 +183,10 @@ determine if two vertices should have an edge. This is currently not
configurable or passed as a function.

The default `cost_fun` and `heur_fun` is the euclidean distance been
the two points.
the two points. The difference between the two is, `cost_fun` is used
while computing the graph and `heur_fun` while computing the
path. Typically they will be the same but that is dependent on use
case.

```elixir
cost_fun = fn a, b -> Vector.distance(a, b) end
Expand Down

0 comments on commit 8557f59

Please sign in to comment.