Grid distance

Sorry Robert but the last one does not work. All hexes tested were at 3 how distance.
the first code:

3320	3317	3
3320	3418	3
3320	3518	3
3320	3619	3
3320	3620	3
3320	3621	3
3320	3622	3
3320	3522	3
3320	3423	3
3320	3323	3
3320	3223	3
3320	3122	3
3320	3022	3
3320	3021	3
3320	3020	3
3320	3019	3
3320	3118	3
3320	3218	3

the “more efficient”

3320 3317 3 3320 3418 3 3320 3518 3 3320 3619 4 3320 3620 3 3320 3621 3 3320 3622 4 3320 3522 3 3320 3423 3 3320 3323 3 3320 3223 3 3320 3122 3 3320 3022 4 3320 3021 3 3320 3020 3 3320 3019 4 3320 3118 3 3320 3218 3

That is interesting. Thanks for feedback. The old saying: [quote]better is the enemy of good.[/quote]

One more try and I will let this rest. Again an attempt to be more efficient. There is one line of code added to the working code previously submitted. I am throwing this back up because Enric you seem to have written the code to test these things.
NEW LINE

// the diagonals If Abs(rowA - rowB) = Abs(colA - colB) \\ 2 Then Return jump + Abs(colA - colB)

COMPLETE CODE

[code]Public Function CheckDistance(fromHex As String, toHex As String) As Integer
Var hexA, hexB As Integer
hexA = fromHex.Val
hexB = toHex.Val

Var colA, colB, rowA, rowB As Integer

colA = hexA \ 100
colB = hexB \ 100
rowA = hexA Mod 100
rowB = hexB Mod 100

Var jump As Integer = 0

Do
loopCount = loopCount + 1
If colA = colB And rowA = rowB Then Return jump + 0
If colA = colB Then Return jump + Abs(rowA - rowB)
If rowA = rowB Then Return jump + Abs(colA - colB)
If colA Mod 2 = 0 And rowA = rowB + 1 Then Return jump + Abs(colA - colB)
If colA Mod 2 = 1 And rowA = rowB - 1 Then Return jump + Abs(colA - colB)

// the diagonals
If Abs(rowA - rowB) = Abs(colA - colB) \ 2 Then Return jump + Abs(colA - colB)

jump = jump + 1
If colA Mod 2 = 0 And rowA > rowB And colA > colB Then
rowA = rowA - 1
colA = colA - 1
Continue
End If
If colA Mod 2 = 0 And rowA > rowB And colA < colB Then
rowA = rowA - 1
colA = colA + 1
Continue
End If
If colA Mod 2 = 1 And rowA < rowB And colA < colB Then
rowA = rowA + 1
colA = colA + 1
Continue
End If
If colA Mod 2 = 1 And rowA < rowB And colA > colB Then
rowA = rowA + 1
colA = colA - 1
Continue
End If

If Abs(colA - colB) > Abs(rowA - rowB) Then
// change column
If colA < colB Then colA = colA + 1 Else colA = colA - 1
Else
// change row
If rowA < rowB Then rowA = rowA + 1 Else rowA = rowA - 1
End If

Loop[/code]

Yes Robert, with the last added line of code the method works.
I cannot appreciate any difference in execution time but, as you said, is more efficient.
Again, thank you.

I couldn’t get this algorithm question out of my mind. My solution was clever but still a little elaborate. I have thought more about the issue.

[code]Public Function CheckDistance(fromHex As String, toHex As String) As Integer
Var hexA, hexB As Integer
hexA = fromHex.Val
hexB = toHex.Val

Var colA, colB, rowA, rowB As Integer

colA = hexA \ 100
colB = hexB \ 100
rowA = hexA Mod 100
rowB = hexB Mod 100

Var colDelta, rowDelta As Integer
colDelta = Abs(colA - colB)
rowDelta = Abs(rowA - rowB)

Return colDelta + Max(0, (rowDelta - (colDelta + 1) \ 2))[/code]

Thanks Robert, it seems to work and it’s quite more efficient.

Well, Enric pointed out to me later that it (my May 1 response) was incorrect for many situations. This one is more tested. :slight_smile:

[code]Public Function CheckDistance(fromHex As String, toHex As String) As Integer
Var hexA, hexB As Integer
hexA = fromHex.Val
hexB = toHex.Val

Var colA, colB, rowA, rowB As Integer

colA = hexA \ 100
colB = hexB \ 100
rowA = hexA Mod 100
rowB = hexB Mod 100

Var deltaCol, deltaRow As Integer
deltaCol = Abs(colA - colB)
deltaRow = Abs(rowA - rowB)

// Special cases
If colA = colB Then Return deltaRow
If rowA = rowB Then Return deltaCol
If colA Mod 2 = 1 Then // odd column

If rowA > rowB Then
Return Max(0, deltaRow - (deltaCol \ 2)) + deltaCol
Else
Return Max(0, deltaRow - ((deltaCol + 1) \ 2)) + deltaCol
End If

Else

If rowA > rowB Then
Return Max(0, deltaRow - ((deltaCol + 1) \ 2)) + deltaCol
Else
Return Max(0, deltaRow - (deltaCol \ 2)) + deltaCol
End If

End If[/code]

Thanks again Robert, will check it.