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!
CodePudding user response:
The simplest way to do this would be to calculate the differences between the two points.
I would think of the problem as a graph where you have positive and negative X and Y axis with the center being 0,0 (you stationary point in the middle).
then you can for example: if you have you top middle point A and you second moving point B.
A = (0, 0) B = (0, 1)
# if A[0] - B[0] < 0 then we know that we are on the left side of the clock and thus will need a value between 6 and 12
# if A[1] - B[1] < 0 then we know that its the horizontal plane and that we should be on the bottom side of the clock.
# Then you can say that if the difference in X is 0 you know that the clock is 12 or 6.
CodePudding user response:
# 坐标夹角转换为时间
# 计算两点间的夹角
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)