-1

in python 2.7

trying to get the correct time ( convert decimal minutes to default time HH:MM:SS )

first i got the distance from 2 different points (hexagon map) then multiply the distance by 13.3 ( the speed of unit per minutes )

import time
import math

from datetime import timedelta

def calculateDistance(x1,y1,x2,y2):
    dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
    return dist

dist = calculateDistance(16345,16318,16345,16314)
print dist

minutes = (13.3) * dist
print minutes

time = timedelta(minutes=minutes)
print(time)

the result is: 4.0 << the distance

53.2 << the time in minutes ( distance * 13.3 )

0:53:12 <<< the time but it's incorrect!

the result should be 0:53:20

any suggestions?

thank you

abdulla-alajmi
  • 491
  • 1
  • 9
  • 17
  • Isn't 53.2 minutes 53 minutes and 12 seconds? – Tony Nov 03 '14 at 08:01
  • I don't know! the result should be 0:53:20 I don't know what mistake i made! – abdulla-alajmi Nov 03 '14 at 08:36
  • 30 seconds is half a minute. A half is 0.5, so 30 seconds is 0.5 minutes. In the same way, 12 seconds is 0.2 minutes. But if 12 seconds is 0.2 minutes, 0.2 minutes is 12 seconds. So 53 minutes + 0.2 minutes _is_ 53 minutes and 12 seconds. The time is correct. – Tony Nov 03 '14 at 08:45
  • ok lets change the coordinates (16345,16318,16348,16301) the result will be 3:49:35 but it's not correct! the correct result is 3:46:40 – abdulla-alajmi Nov 03 '14 at 08:51
  • The correct result is 3:49:35 (rounding down to the nearest second). That's 3 hours = 10,800 seconds, 49 minutes = 2,940 seconds, and 35 more seconds. 10,800 + 2,940 seconds + 35 seconds = 13,775 seconds = 229.6 minutes (approx) = 13.3 * 17.3 (approx), which aligns with the distance (17.3 units). When you say the "correct result is 3:46:40" how are you calculating that, or are you just reading it off a solution sheet? – Tony Nov 03 '14 at 09:12
  • I'm really sorry, i forgot to mention that i'm using a hexagon map! and i think this is why i have this problem – abdulla-alajmi Nov 03 '14 at 09:50
  • You should amend your question to indicate that you are using a hexagonal map, and explain what a hexagonal map is and how it might affect distance measurements. – Tony Nov 03 '14 at 10:01
  • this is the hexagon map http://en.wikipedia.org/wiki/Hex_map this is what i'm talking about http://www.maths-forum.com/calcul-distance-130046.php – abdulla-alajmi Nov 03 '14 at 10:45

3 Answers3

0

The time is correct, since 0.2 minutes is 12 seconds.

Rob
  • 3,418
  • 1
  • 19
  • 27
  • I'm sure about the coordinates, and the CalculateDistance() should give me the correct distance between 2 points and the result is 4 which is correct! but the time is not correct! what do you think the problem is? – abdulla-alajmi Nov 03 '14 at 08:19
0

You are using a time representation in base 10 for the seconds. When you convert it to seconds it is normal that you get 53.12 (60*0.20= 12 seconds)

jyvet
  • 2,021
  • 15
  • 22
  • I'm sure about the coordinates, and the CalculateDistance() should give me the correct distance between 2 points and the result is 4 which is correct! but the time is not correct! what do you think the problem is? – abdulla-alajmi Nov 03 '14 at 08:18
  • Are you sure that 13.3 is correct (it should be in minutes per unit of distance) ? If you use 13.3333... you get something close to 0:53:20. I suggest you to work with seconds first, then convert the seconds to minutes in the end to avoid mistakes. – jyvet Nov 03 '14 at 09:27
  • yes i'm very sure about the expected result and the speed (13.3), and i forgot to mention that i'm using a hexagon map! – abdulla-alajmi Nov 03 '14 at 09:53
0

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

  1. It is assumed that underneath everything there is a bog-standard rectangular co-ordinate system. Your x1, y1 etc. are expressed in that system.

  2. 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.

"Hexagonal distance" from centre of hexagon to a point B

  1. 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.

Tony
  • 1,645
  • 1
  • 10
  • 13
  • the last thing, how can i get the time like this HH:MM:SS instead of HH:MM:SS.MMMMMM , MMMMMM = microseconds – abdulla-alajmi Nov 13 '14 at 11:09
  • Hi. Try [this answer](http://stackoverflow.com/questions/18470627/python-timedelta-remove-microseconds) to remove the microseconds. – Tony Nov 13 '14 at 21:16