Search code examples
pythonmathgeometryangle

Coordinates on image to clock time


I have an image and a set of coordinates on that image (x and y) and I need to somehow turn them into a clock time. Here's an image showing what I'm trying to do: example

So point B will become 3 o'clock (it'll be rounded) I have no idea where to start, so any help is appreciated!


Solution

  • # 坐标夹角转换为时间
    # 计算两点间的夹角
    import math
    
    
    def calc_angle(other, center=(320, 240)):
        x = center[0] - other[0]
        y = center[1] - other[1]
        z = math.sqrt(x * x + y * y)
        angle = round(math.asin(y / z) / math.pi * 180)
        return angle + 90
    
    
    def angle_to_time(angle):
        second = angle * 120   # one angle = 120 seconds
        hour = second // 3600
        second %= 3600
        minute = second // 60
        second %= 60
        return hour, minute, second
    
    
    print(angle_to_time(calc_angle((330, 240))))
    
    (3, 0, 0)