Providing an answer to what I think you are really asking, which is:
How do I calculate "distance" from point A to point B, where point A
is at the centre of concentric hexagons and the distance from A to B
is defined as the length of the apothem* of the hexagon on which B lies.
(* The apothem is is the length of the perpendicular from the centre of
the hexagon to an edge).
If this is what you mean, please update your question. There is nothing wrong in your code with the time conversion, only the distance measurement.
The answer...
It is assumed that underneath everything there is a bog-standard rectangular co-ordinate system. Your x1
, y1
etc. are expressed in that system.
When the angle between the horizontal and the line AB is between 60 and 120 degrees, the hexagon that B lies on is the one with apothem length equal to the y co-ordinate of B (assuming A is at the origin). This is based on the information on the 5th post down on the link you provided in a comment.
It may be easier to see pictorially: B is on the hexagon of apothem 10, so the distance from A to B is 10. The y-coordinate of B is also 10.

- When the angle between the horizontal and the line AB is some other value, the co-ordinates need to be rotated to get the angle to between 60 and 120. Then you can use (2).
In the code below, dist(x1, y1, x2, y2)
should provide the distance in units.
import math
def dtor(deg):
return deg * math.pi/180
def eucl_dist(x1, y1, x2, y2):
return math.sqrt((x2-x1)**2 + (y2-y1)**2)
def ang(x, y):
return math.atan2(y,x)
def rot(x,y,ang):
newx = x*math.cos(ang)-y*math.sin(ang)
newy = x*math.sin(ang)+y*math.cos(ang)
return (newx, newy)
def hexdist(x, y):
alpha = ang(x, y) # always top-right quadrant
if dtor(60) <= alpha < dtor(120):
dist = y
else:
if dtor(120) <= alpha < dtor(180):
newcoords = rot(x,y,dtor(-60))
elif dtor(0)<= alpha < dtor(60):
newcoords = rot(x,y,dtor(60))
elif dtor(-60)<= alpha < dtor(0):
newcoords = rot(x,y,dtor(120))
elif dtor(-120)<= alpha < dtor(-60):
newcoords = rot(x,y,dtor(180))
elif dtor(-180)<= alpha < dtor(-120):
newcoords = rot(x,y,dtor(120))
dist = hexdist(newcoords[0],newcoords[1])
return dist
def dist(x1,y1,x2,y2):
return hexdist(x2-x1, y2-y1)
Note that the results still do not match your numbers.
The first set of coordinates (16345,16318,16345,16314
) produces 53.2 minutes, which is logical given that the x
coordinate doesn't change. As others have explained, this is definitely 53 minutes and 12 seconds.
The other set of coordinates (in the comments, 16345,16318,16348,16301
) gives 3:46:06, which is close to your estimate of 3:46:40 but not exactly the same. Could this be due to rounding of the co-ordinates?
This is definitely one of the weirdest questions I have seen here! I do wonder why the good old Euclidean metric was unsuitable.