在迷宫中找到案例的x和y(在C中)

问题描述 投票:-5回答:4

我想使用A *算法找到case(n)和case(m)之间的距离,其中n!= m。如何通过迷宫中的案例编号,高度和宽度找到x0,x1,y0和y1?那是否有一个公式?

float distance(Maze* maze, int start, int end)
{
  float x0 = ..??.. 

  float x1 = ..??..

  float y0 = ..??..

  float y1 = ..??..

  float dx = x0 - x1;
  float dy = y0 - y1;

  return sqrtf(dx*dx+dy*dy);
}

迷宫的例子:

<----------- width ------------->  

case0 | case1 | case2  | case3  |

case4 | case5 | case6  | case7  |     height  

case8 | case9 | case10 | case11 |
c coordinates distance a-star maze
4个回答
1
投票

首先计算指数:

int x0 = m % width; // % = modulo operation
int y0 = m / width;

int x1 = n % width;
int y1 = n / width;

int dx = x1 - x0;
int dy = y1 - y0;

return sqrtf(dx*dx + dy*dy);

确保使用int执行索引计算。 int-division截断小数。模运算返回除法的余数。 x % width产生0 .. width-1范围内的值。


0
投票

从您的示例中,您可以获取元素的行和列,如下所示:

//for case(n)
int row_of_n = n / number_of_columns;
int column_of_n = n % number_of_columns;

同样找到case(m)的坐标然后应用毕达哥拉斯定理。


0
投票

假设你想:

1:(0,0) | 2:(1,0) | 3:(2,0) | 4:(3,0) |
5:(0,1) | 6:(1,1) | 7:(2,1) | 8:(3,1) |
9:(0,2) | 10:(1,2) | 11:(2,2) | 12:(3,2) |

你可以找到给定案例的x和y

void find_coordinates(int case_number, &x, &y) {
    *x = case_number % 4 - 1;
    *y = case_number / 4;
    if(*x==-1) *x=3;
    if(*x==0) *y=*y-1;
}

0
投票
float x0 = (start % width);
float x1 = (  end % width);
float y0 = (start / width);
float y1 = (  end / width);

当开始/结束实际上在宽度处缠绕到下一行时,y只是适合开始/结束的宽度数。当你减去y时,x就是剩下的。

© www.soinside.com 2019 - 2024. All rights reserved.