# optimize a path?

This topic is 4698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Try looking up the A* algorithm (A star).

##### Share on other sites
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".

##### Share on other sites
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;

##### Share on other sites
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).

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
but i only asked for a algorythm to optimize these paths, i'd really like to learn A* but it is impossible to search for it on google (doesn't search for the star character).

1. 1
2. 2
3. 3
Rutin
19
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632628
• Total Posts
3007515
• ### Who's Online (See full list)

There are no registered users currently online

×