GithubHelp home page GithubHelp logo

christofferqa / dart-a-star Goto Github PK

View Code? Open in Web Editor NEW

This project forked from levi-lesches/dart-a-star

0.0 3.0 0.0 138 KB

path finding in Dart with A*

License: Other

Dart 57.81% HTML 5.09% JavaScript 37.10%

dart-a-star's Introduction

A* path finding with Dart

A simple A* algorithm implemented in Dart. An example of path finding.

Last updated 2013-11.

The original 2D algorithm was ported from this JavaScript example. No effort has been made to optimize it. A more generic A* algorithm was added in November 2013. That one is fairly optimized.

See LICENSE file for license details.

See running example at http://sethladd.github.io/dart-a-star/deploy/

Example

There are two separate A* algorithms in this package. One of them, aStar2D, is specific to 2D grid maps. The usage can be as simple as:

import 'package:a_star/a_star_2d.dart';
main() {
  String textMap = """
        sooooooo
        oxxxxxoo
        oxxoxooo
        oxoogxxx      
        """;
  Maze maze = new Maze.parse(textMap);
  Queue<Tile> solution = aStar2D(maze);
}

The second algorithm is generic and works on any graph (e.g. 3D grids, mesh networks). The usage is best explained with an example (details below):

import 'package:a_star/a_star.dart';

class TerrainTile extends Object with Node {
  // ...
}
class TerrainMap implements Graph<TerrainTile> {
  // Must implement 4 methods.
  Iterable<T> get allNodes => /* ... */
  num getDistance(T a, T b) => /* ... */
  num getHeuristicDistance(T a, T b) => /* ... */
  Iterable<T> getNeighboursOf(T node) => /* ... */
}

main() {
  var map = new TerrainMap();
  var pathFinder = new AStar(map);
  var start = /* ... */
  var goal = /* ... */
  pathFinder.findPath(start, goal)
  .then((path) => print("The best path from $start to $goal is: $path"));
}

Explanation: Here, we have a TerrainMap of TerrainTile nodes. The only requirements are that TerrainMap implements Graph (4 methods) and TerrainTile is extended with the Node mixin (no additional work). Then, we can simply instantiate the A* algorithm by new AStar(map) and find paths between two nodes by calling the findPath(start, goal) method. Normally, we would only create the AStar instance once and then reuse it throughout our program. This saves performance.

You can also use findPathSync(start, goal) if you don't need to worry about blocking.

All the three classes (AStar, Graph and Node) are well documented in lib/a_star.dart. For a complete example, see the minimal unit tests or one of the two generalized benchmarks (benchmark.dart or benchmark_generalized_2d).

Reporting bugs

Please file bugs at https://github.com/sethladd/dart-a-star/issues

Contributors

dart-a-star's People

Contributors

algobardo avatar christofferqa avatar pedersenthomas avatar sethladd avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.