Search code examples
pythonturtle-graphicspython-turtle

What exactly is this part of code in this analog clock doing to move clock hands?


I'm not sure if this type of questions get answered here but here it goes anyway.

# moving hour hand
def moveHourHand():
   currentHourInternal = datetime.datetime.now().hour
   degree = (currentHourInternal - 15) * -30
   currentMinuteInternal = datetime.datetime.now().minute
   degree = degree + -0.5 * currentMinuteInternal
   hourHand.setheading(degree)
   wn.ontimer(moveHourHand, 60000)


# moving minute hand
def moveMinuteHand():
    currentMinuteInternal = datetime.datetime.now().minute
    degree = (currentMinuteInternal - 15) * -6
    currentSecondInternal = datetime.datetime.now().second
    degree = degree + (-currentSecondInternal * 0.1)
    minuteHand.setheading(degree)
    wn.ontimer(moveMinuteHand, 1000)

# moving second hand
def moveSecondHand():
    currentSecondInternal = datetime.datetime.now().second
    degree = (currentSecondInternal - 15) * -6
    secondHand.setheading(degree)
    wn.ontimer(moveSecondHand, 1000)

This is a part of a analog clock built by turtle in python. (not mine. This is the source) This part is creating functions for the movement of hands.

I'm creating a clock of my own and I'm searching for different ways to get the hands to move. This one seems to be working the best but I can't really understand what's exactly going on. I know it's getting a value from datetime library but everything after that is confusing. especially the part with 'degree'. and where is '15' coming from.

So I'd appreciate it if someone can explain it in a way that I can understand.


Solution

  • # moving hour hand
    def moveHourHand():
       currentHourInternal = datetime.datetime.now().hour #Gets current local hour
       degree = (currentHourInternal - 15) * -30 #Hour degree, look below
       currentMinuteInternal = datetime.datetime.now().minute #Gets minutes for moving the hour hand between hours 
       degree = degree + -0.5 * currentMinuteInternal #Minute degree, also, look below
       hourHand.setheading(degree) #This might send the degree to the function that move the hand
       wn.ontimer(moveHourHand, 60000) #Im not sure about this one but i think that tells that function what and when to be called again, in milli seconds (60000ms = 60s = 1m)
    

    Hour degree:

    -In a circle, normally, 0 degrees is the most right point, but in a clock 0 degrees is the top point, as seen in the image. To compesate that 15 are romoved. Between each hour has 30 degrees (360 degrees in a circle / 12 hours). Its negative because adding positive degrees goes counter clock ways.

    circle with degreesclock

    e.g. (hour - 15) * -30
    12 hours -> (12 - 15) * -30 = 90 degrees. Look the images, 90 degrees corresponds to 12h
    3 hours -> (3 - 15) * -30 = 360 degrees
    6 hours -> (6 - 15) * -30 = 270 degrees
    9 hours -> (9 - 15) * -30 = 180 degrees
    

    Minute degree:

    Works similar to the hour, multiplies by -0.5 because each hour has 30 degrees and 60 minutes, do the math, (30 degrees / 60 minutes) 1/2 degrees by minute. The nagative as before is because positive degrees go counter clock ways, so negative goes clock ways. Then its added to the total degrees for moving the hour hand between the numbers.

    e.g. (minutes * -0.5)
    0 minutes:  0 degrees
    15 minutes: -7.5 degrees
    30 minutes: -15 degrees
    45 minutes: -22.5 degrees