-5

I want to find the distance between case(n) and case(m), with n!=m, using A* algorithm. How can i find x0, x1, y0 and y1 by using case number, height and width in a maze ? Are there a formule for that ?

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 a maze :

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

case0 | case1 | case2  | case3  |

case4 | case5 | case6  | case7  |     height  

case8 | case9 | case10 | case11 |
Roberto Rossi
  • 11
  • 1
  • 8
  • Something ike the [A*](http://en.wikipedia.org/wiki/A*_search_algorithm) algorithm? The [Manhattan method](http://www.policyalmanac.org/games/aStarTutorial.htm) may work well; it's a search-algorithm, but it deals with path-finding, lengths, etc – newfurniturey Dec 05 '14 at 20:34
  • I fixed `case10` and `case11`. feel free to revert if it wasn't a typo... – Sylvain Leroux Dec 05 '14 at 21:48

4 Answers4

1

First calculate the 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 to perform the index calculations with int. The int-division truncates the decimals. The modulo operation returns the remainder of the division. x % width yields a value in the range 0 .. width-1.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

From your example, you can get the row and the column of the element, like this:

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

Do the same to find the coordinates of case(m) and then apply the Pythagorean theorem.

RockOnRockOut
  • 751
  • 7
  • 18
0

Assuming you want:

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) |

You can find the x and y of a given case by

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;
}
David
  • 757
  • 1
  • 7
  • 17
0
float x0 = (start % width);
float x1 = (  end % width);
float y0 = (start / width);
float y1 = (  end / width);

As start/end in effect wraps around at width to the next row, the y is just the number of widths which fits into the start/end. The x is what's left over when you subtract the y.

Chris
  • 2,655
  • 2
  • 18
  • 22