Manhattan Distance between tiles in a hexagonal grid

前端 未结 6 925
北海茫月
北海茫月 2020-12-08 07:57

For a square grid the euclidean distance between tile A and B is:

distance = sqrt(sqr(x1-x2)) + sqr(y1-y2))

For an actor constrained to mo

6条回答
  •  情深已故
    2020-12-08 08:40

    If you want the straight-line distance:

    double dy = y2 - y1;
    double dx = x2 - x1;
    // if the height is odd
    if ((int)dy & 1){
        // whether the upper x coord is displaced left or right
        // depends on whether the y1 coordinate is odd
        dx += ((y1 & 1) ? -0.5 : 0.5);
    }
    double dis = sqrt(dx*dx + dy*dy);
    

    What I'm trying to say is, if dy is even, it's just a rectangular space. If dy is odd, the position of the upper right corner is 1/2 unit to the left or to the right.

提交回复
热议问题