'Make a path straighter
I'm working on a C++ project whose goal is to find a path in an image that represents a circuit.

After successfully implementing the A* algorithm I get this plot.

I would have liked to make my path straighter like that but I have no idea how to do it.

So I would like some help in finding a solution to this problem.
About the code, here is the function that will draw the path on the image. The plot is represented by a vector of integer pairs (x and y).
void AStar::tracePath(Pair &d) {
std::vector<Pair> path;
int i = d.first, j = d.second;
Pair next_node = this->nodeDetails[j][i].parent;
do {
path.push_back(next_node);
next_node = this->nodeDetails[j][i].parent;
i = next_node.first;
j = next_node.second;
} while (this->nodeDetails[j][i].parent != next_node);
path.emplace_back(i, j);
path.push_back(d);
// The algorithm to optimize the path
// optimizePath(path);
// save path on image
for (Pair p: path) {
const unsigned char color_mag[] = {0, 255, 0};
imageObject.draw_point(p.first, p.second, color_mag);
}
imageObject.save("output.png");
}
And this is to check if the point is not in a "wall" (grid is the image converted to black (0) and white (1))
bool AStar::isUnBlocked(const Pair &point) const {
return isValid(point) && grid[point.first][point.second] == 1;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
