Jump to content

Distance between two points on a matrix


Jessica

Recommended Posts

I have a 5x5 grid, stored in a multidimensional array. I have some objects in various spots on the grid. Imagine like a chessboard, each spot in the matrix is a square. I want to find how many "moves" basically between two spots.

 

What I have now is the row and column numbers for each object, and I can figure out how many straight moves (up, down, left, right) it would be. But I want to account for diagonal. So if an object is in 3,3 and another in 5,5 that's only 2 moves. But I can't seem to figure out how to account for this in the code.

 

If we have 3,3 and 4,5 - that's down one, over two, so I can see that's 3 moves. Or, 2 moves if we can use diagonal. Having trouble when to subtract moves for diagonal moves.

 

Send me in the right direction? This is more a theory problem than actual code, but if you need code I can post something.

Link to comment
Share on other sites

Actually I did it differently than I thought a minute ago.

 

This looks like it works as far as I can test it now. Anyone see a problem?

 

//Distance between start and end in rows and columns
$rowDist = abs($row-$dr);
$colDist = abs($col-$dc);
//the diagonal moves needed is the smallest difference of these two
$diagNeeded = min($rowDist,$colDist);
$distance = $rowDist+$colDist-$diagNeeded;

 

It works perfectly for all the spots I have in my matrix, I'll post again if I find it doesn't work later.

Link to comment
Share on other sites

Actually I did it differently than I thought a minute ago.

 

This looks like it works as far as I can test it now. Anyone see a problem?

 

//Distance between start and end in rows and columns
$rowDist = abs($row-$dr);
$colDist = abs($col-$dc);
//the diagonal moves needed is the smallest difference of these two
$diagNeeded = min($rowDist,$colDist);
$distance = $rowDist+$colDist-$diagNeeded;

 

Yeah, that was different than I thought too, but you know, I think the problem is even simpler than that. Just find the max horizontal or vertical distance! The shorter distance will be completed first, but you still need to complete the longer distance.  :facepalm:  It wasn't until I saw your code above that it dawned on me.

 

//Total moves needed between start and end in rows and columns
$distance = max(abs($row-$dr), abs($col-$dc));

 

By the way, why so long since your last post?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.