coordinates - Find the x and y of a case in a maze ( in C) -
i want find distance between case(n) , case(m), n!=m how can find x0, x1, y0 , y1 using case number, height , width in maze ? there formule ?
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); }
example of maze :
<----------- width -------------> case0 | case1 | case2 | case3 | case4 | case5 | case6 | case7 | height case8 | case9 | case10 | case11 |
first calculate indexes:
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);
make sure perform index calculations int
. int-division truncates decimals. modulo operation returns remainder of division. x % width
yields value in range 0 .. width-1
.
Comments
Post a Comment