whoward69
DLL Minion
As I could find no reference to calculating the distance between two tiles on a hex grid, I though I'd share the results of my "JFGI"
Hope this helps someone else
W
Code:
-- Calculate the distance between two plots
--
-- See http://www-cs-students.stanford.edu/~amitp/Articles/HexLOS.html
-- Also http://keekerdc.com/2011/03/hexagon-grids-coordinate-systems-and-distance-calculations/
--
function distanceBetween(pPlot1, pPlot2)
local mapX, mapY = Map.GetGridSize()
-- Need to work on a hex based grid
local hex1 = PlotToHex(pPlot1)
local hex2 = PlotToHex(pPlot2)
-- Calculate the distance between the x and z co-ordinate pairs
-- allowing for the East-West wrap, (ie shortest route may be by going backwards!)
local deltaX = math.min(math.abs(hex2.x - hex1.x), mapX - math.abs(hex2.x - hex1.x))
local deltaZ = math.min(math.abs(hex2.z - hex1.z), mapX - math.abs(hex2.z - hex1.z))
-- Calculate the distance between the y co-ordinates
-- there is no North-South wrap, so this is easy
local deltaY = math.abs(hex2.y - hex1.y)
-- Calculate the distance between the plots
local distance = math.max(deltaX, deltaY, deltaZ)
-- Allow for both end points in the distance calculation
return distance + 1
end
function PlotToHex(pPlot)
local hex = ToHexFromGrid(Vector2(pPlot:GetX(), pPlot:GetY()))
-- X + y + z = 0, hence z = -(x+y)
hex.z = -(hex.x + hex.y)
return hex
end
Hope this helps someone else
W