< index < 13. Path finding < 13.1 Creating a path | ===================================== 13.2 Computing the path ===================================== | > 13.3 Reading path information

#### 13.2.1. Computing an A* path

Once you created a TCODPath object, you can compute the path between two points:

    C++ : bool TCODPath::compute(int ox, int oy, int dx, int dy)
    C   : bool TCOD_path_compute(TCOD_path_t path, int ox,int oy, int dx, int dy)
    Py  : path_compute(path, ox, oy, dx, dy)

Parameter | Description
--- | ---
path | In the C version, the path handler returned by a creation function.
ox,oy | Coordinates of the origin of the path.
dx,dy | Coordinates of the destination of the path.

Both points should be inside the map, and at a walkable position. The function returns false if there is no possible path.
Example :

    C++ : TCODMap *myMap = new TCODMap(50,50);
          TCODPath *path = new TCODPath(myMap); // allocate the path
          path->compute(5,5,25,25); // calculate path from 5,5 to 25,25
    C   : TCOD_map_t my_map=TCOD_map_new(50,50);
          TCOD_path_t path = TCOD_path_new_using_map(my_map);
          TCOD_path_compute(path,5,5,25,25);
    Py  : my_map=libtcod.map_new(50,50)
          path = libtcod.path_new_using_map(my_map)
          libtcod.path_compute(path,5,5,25,25)

#### 13.2.2. Computing a Dijkstra path

##### 13.2.2.1. Analysing the map

In case of Dijkstra, this works in a slightly different way. In order to be able to compute a path, Dijkstra must first analyse the distances from the selected root (origin) node to all other nodes:

    C++ : void TCODDijkstra::compute(int rootX, int rootY)
    C   : void TCOD_dijkstra_compute(TCOD_dijkstra_t dijkstra, int root_x, int root_y)
    Py  : dijkstra_compute(dijkstra, root_x, root_y)

Parameter | Description
--- | ---
dijkstra | In the C version, the path handler returned by a creation function.
root_x,root_y | Coordinates of the root node (origin) of the path.

The coordinates should be inside the map, at a walkable position. Otherwise, the function's behaviour will be undefined.

##### 13.2.2.2. Laying out a path

After the map is analysed and all the distances from the root node are known, an unlimited number of paths can be set, all originating at the root node, using:

    C++ : bool TCODDijkstra::setPath(int toX, int toY)
    C   : bool TCOD_dijkstra_path_set(TCOD_dijkstra_t dijkstra, int to_x, int to_y)
    Py  : dijkstra_path_set(dijkstra, to_x, to_y)

Parameter | Description
--- | ---
dijkstra | In the C version, the path handler returned by a creation function.
to_x,to_y | Coordinates of the destination node of the path.

The path setting function will return true if there's a path from the root node to the destination node. Otherwise, it will return false.
Example:

    C++ : TCODMap *myMap = new TCODMap(50,50);
          TCODDijkstra *dijkstra = new TCODDijkstra(myMap); // allocate the path
          dijkstra->compute(25,25); // calculate distance from 25,25 to all other nodes
          dijkstra->setPath(5,5); // calculate a path to node 5,5
          dijkstra->setPath(45,45); //calculate another path from the same origin
    C   : TCOD_map_t my_map=TCOD_map_new(50,50);
          TCOD_dijkstra_t dijkstra = TCOD_dijkstra_new(my_map);
          TCOD_dijkstra_compute(dijkstra,25,25);
          TCOD_dijkstra_path_set(dijkstra,5,5);
          TCOD_dijkstra_path_set(dijkstra,45,45);
    Py  : my_map=libtcod.map_new(50,50)
          dijkstra = libtcod.dijkstra_new(my_map)
          libtcod.dijkstra_compute(dijkstra,25,25)
          libtcod.dijkstra_path_set(dijkstra,5,5)
          libtcod.dijkstra_path_set(dijkstra,45,45)