gwg/pathfinding
2
0
Fork
You've already forked pathfinding
0

Add heuristic functions #2

Open
opened 2026年04月27日 16:32:14 +02:00 by NNB · 0 comments
Owner
Copy link
importgleam/floatimportgleam/intimportgleam/listimportvec/vec2importvec/vec2i.{typeVec2i}constsqrt2minus1=0.41421356237309515/// The [Euclidean heuristic](https://wikipedia.org/wiki/Euclidean_distance).
///
pubfnheuristic_euclidean(a:Vec2i,b:Vec2i)->Float{vec2i.distance(a,b)}/// The [Manhattan heuristic](https://wikipedia.org/wiki/Taxicab_geometry).
///
/// Note that this heuristic is intended to be used with 4-side orthogonal
/// movements.
///
pubfnheuristic_manhattan(a:Vec2i,b:Vec2i)->Float{vec2i.subtract(a,b)|>vec2i.absolute_value|>vec2.to_list|>int.sum|>int.to_float}/// The Octile heuristic.
///
pubfnheuristic_octile(a:Vec2i,b:Vec2i)->Float{letassert[shortest,..rest]=vec2i.subtract(a,b)|>vec2i.absolute_value|>vec2i.to_vec2f|>vec2.to_list|>list.sort(float.compare)[shortest*.sqrt2minus1,..rest]|>float.sum}/// The [Chebyshev heuristic](https://wikipedia.org/wiki/Chebyshev_distance).
///
pubfnheuristic_chebyshev(a:Vec2i,b:Vec2i)->Float{letassertOk(score)=vec2i.subtract(a,b)|>vec2i.absolute_value|>vec2.to_list|>list.max(int.compare)score|>int.to_float}
```gleam import gleam/float import gleam/int import gleam/list import vec/vec2 import vec/vec2i.{type Vec2i} const sqrt2minus1 = 0.41421356237309515 /// The [Euclidean heuristic](https://wikipedia.org/wiki/Euclidean_distance). /// pub fn heuristic_euclidean(a: Vec2i, b: Vec2i) -> Float { vec2i.distance(a, b) } /// The [Manhattan heuristic](https://wikipedia.org/wiki/Taxicab_geometry). /// /// Note that this heuristic is intended to be used with 4-side orthogonal /// movements. /// pub fn heuristic_manhattan(a: Vec2i, b: Vec2i) -> Float { vec2i.subtract(a, b) |> vec2i.absolute_value |> vec2.to_list |> int.sum |> int.to_float } /// The Octile heuristic. /// pub fn heuristic_octile(a: Vec2i, b: Vec2i) -> Float { let assert [shortest, ..rest] = vec2i.subtract(a, b) |> vec2i.absolute_value |> vec2i.to_vec2f |> vec2.to_list |> list.sort(float.compare) [shortest *. sqrt2minus1, ..rest] |> float.sum } /// The [Chebyshev heuristic](https://wikipedia.org/wiki/Chebyshev_distance). /// pub fn heuristic_chebyshev(a: Vec2i, b: Vec2i) -> Float { let assert Ok(score) = vec2i.subtract(a, b) |> vec2i.absolute_value |> vec2.to_list |> list.max(int.compare) score |> int.to_float } ```
Sign in to join this conversation.
No Branch/Tag specified
main
v1.0.2
v1.0.1
v1.0.0
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
gwg/pathfinding#2
Reference in a new issue
gwg/pathfinding
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?