1

I found a code which was very useful for me in implementing A*. but I face one problem. I need to calculate Manhattan distance, I try to implement one but it's not working. this code provide the calculation for Euclidean distance.

public Node(Node parentNode, Node goalNode, int gCost,int x, int y)
    {

        this.parentNode = parentNode;
        this._goalNode = goalNode;
        this.gCost = gCost;
        this.x=x;
        this.y=y;
        InitNode();
    }

    private void InitNode()
    {
        this.g = (parentNode!=null)? this.parentNode.g + gCost:gCost;
        this.h = (_goalNode!=null)? (int) Euclidean_H():0;
    }

    private double Euclidean_H()
    {
        double xd = this.x - this._goalNode .x ;
        double yd = this.y - this._goalNode .y ;
        return Math.Sqrt((xd*xd) + (yd*yd));
    }

the code used c#. thank you very much.

reem.abd
  • 41
  • 10
  • 1
    First search I did - there is an exact same question - http://stackoverflow.com/questions/4532528/manhattan-heuristic-function-for-a-star-a with a great solution.. – Yosi Dahari Nov 15 '13 at 10:08
  • I really searched, but I couldn't find. thank you – reem.abd Nov 15 '13 at 10:27

1 Answers1

4

Manhattan distance between A and B (a.k.a L1-norm) is

Sum(abs(a[i] - b[i]))

When Euclidian one between A and B (a.k.a L2-norm) is

Sqrt(Sum((a[i] - b[i])**2))

where a[i], b[i] are corresponding coordinates of A and B points. So the code could be

private double Manhattan_H()
{
  double xd = this.x - this._goalNode.x;
  double yd = this.y - this._goalNode.y;

  return Math.Abs(xd) + Math.Abs(yd);
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215