2013-11-15 230 views
1

我发现了一个对我来说在实现A *时非常有用的代码。但我面临一个问题。 我需要计算曼哈顿距离,我试图实现一个,但它不工作。此代码提供欧几里德距离的计算。从欧几里得距离转换为曼哈顿距离c#

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)); 
    } 

使用c#代码。 非常感谢。

+1

首先搜索我没有 - 没有一个确切的同样的问题 - http://stackoverflow.com/questions/4532528/manhattan-heuristic-function-for-a-star-a与一个很好的解决方案.. –

+0

我真的搜索,但我找不到。谢谢 –

回答

3
(又名 L1范数)A和B之间

曼哈顿距离是

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

当A和B之间的欧几里得一个(又名L2-范数)是

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

其中a [i],b [i]是A点和B点的对应坐标。 因此,代码可能是

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); 
} 
+0

非常感谢,这非常有用 –