2013-11-15 75 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

非常感謝,這非常有用 –