diff --git a/src/dijkstragraph.c b/src/dijkstragraph.c index eb2a318..01ae6a7 100644 --- a/src/dijkstragraph.c +++ b/src/dijkstragraph.c @@ -147,6 +147,17 @@ struct dijkstra_path *dijkstra_path_dup_shallow(struct dijkstra_path *path) return dup; } +dijkstra_cost dijkstra_get_weight_from_distance(struct dijkstra_node *node_a, + struct dijkstra_node *node_b) +{ + float dist; + + dist = sqrt(pow(node_a->position.x - node_b->position.x, 2) + + pow(node_a->position.y - node_b->position.y, 2)); + + return (dijkstra_cost) dist; +} + /*! * \brief dijkstra_path_new creates a new dijkstra_path between two nodes. * \param name the name of the path (pointer is not copied and must stay valid!) @@ -162,7 +173,11 @@ struct dijkstra_path *dijkstra_path_new(char *name, path = malloc(sizeof(*path)); if (path) { path->name = name; - path->weight = weight; + if (path->weight == DIJKSTRA_WEIGHT_AUTO) + path->weight = dijkstra_get_weight_from_distance(source, + destination); + else + path->weight = weight; dijkstra_connect_nodes_to_path(source, destination, path); } diff --git a/src/dijkstragraph.h b/src/dijkstragraph.h index 3bcdf64..0c68831 100644 --- a/src/dijkstragraph.h +++ b/src/dijkstragraph.h @@ -3,6 +3,8 @@ #include +#define DIJKSTRA_WEIGHT_AUTO (-1) + typedef int dijkstra_cost; struct dijkstra_solver { diff --git a/src/geojson.c b/src/geojson.c index 8ef396d..0c66072 100644 --- a/src/geojson.c +++ b/src/geojson.c @@ -83,7 +83,8 @@ int add_geojson_to_dijkstra(struct dijkstra_solver *solver, dijkstra_path_new_to_list(solver, x_last, y_last, x, y, - name, 1); + name, + DIJKSTRA_WEIGHT_AUTO); } x_last = x;