optimize a path?
I have a basic path finding algorythm, and it generates path like the one on the left, blue is wall, green is path found and red is area that was scanned for path but didn't end being part of the path, you can see the final path isn't what would be expected, i illustrated how the path should look on the right side of the picture:
I guess i need to optimize the path somehow, could someone point me to a algorythm to do this?
Projects: Top Down City: http://mathpudding.com/
Yeah, that looks like a depth-first algorithm, which is inefficient and doesn't guarantee a shortest path. Breadth-first and A* will fare better in finding an optimized path in the first place. For path optimization on a continuous euclidean state space, BTW, google "string pulling".
but this is "Breadth-first" algorythm (i think), this is what i use:
function TForm1.findPathDepth(start, endp: Tpoint): boolean;varx, y: integer;beginresult:= false; // is this the end of the path? if ((start.x = endp.x) and (start.y = endp.y)) then begin path[pathlen]:= start; pathlen:= pathlen +1; result:= true; exit; end; // you can't go through walls you know if map[start.y, start.x] or map[endp.y, endp.x] <> 0 then begin result:= false; exit; end; // oh no, outside of world? if ((start.x < 0) or (start.y < 0)) or ((start.x > mwidth - 1) or (start.y > mheight - 1)) then begin result:= false; exit; end; // already cheched this block? if ((map[start.y, start.x] = 1) or (mapBeen[start.y][start.x] = true)) then begin result:= false; exit; end; // this block was processed mapBeen[start.y, start.x]:= true;// noteslist.lines.add(format('SEARCHING: , %d %d', [start.x, start.y])); // search for neighbour cubes for y:= -1 to 1 do for x:= -1 to 1 do begin if (y = 0) and (x = 0) then continue; // don't search same cell again if (mapBeen[start.y + y, start.x + x] = false) and (map[start.y + y, start.x + x] = 0) then begin // found a node if findPathDepth(Point(start.x + x, start.y + y), endp) = true then begin path[pathlen]:= start; pathlen:= pathlen +1; result:= true; exit; end; end; end;end;
Projects: Top Down City: http://mathpudding.com/
Nope, that's depth-first (look at the name of the function). Also note that DF is usually implemented via recursion, as this function is, while BF is usually implemented via iteration (often with a priority queue).
i haven't found a decent fast example of A* or breadth yet, and i think this depth first works pretty well, all i need to do is optimise the path.
Projects: Top Down City: http://mathpudding.com/
No, depth-first doesn't work well. Its complexity balloons up in larger state spaces, and there's no good way to optimize the paths it produces. Take the time to actually learn how A* works, and then code it yourself. You can't always count on other people writing code for you.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement