2013-11-15 3 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
A와 B 사이의 유클리드 온 (일명 L2 놈)이

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

곳인 경우와 B 사이

맨해튼 거리 (일명 L1 놈)는

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

인 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

고맙습니다. 매우 유용했습니다. –