Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the angle between the hour and minutes hands?

Tags:

python

math

clock

I'm trying to workout this problem, but I am still struggling to understand the logic to solve this problem.

hour degree = 360 / 12 = 30
minutes degree = 360 / 12 / 60 = 0.5

So, according to this, I thought I could formulate the following function in python:

def clockangles(hour, min):
    return (hour * 30) + (min * 0.5)

For the hour, it works fine, as it appears to have a 1=1 mapping. But for the minute there is one problem at least. When it's 0 minutes, the minutes hand points to 12.

For example:

7pm: hands pointing to 7pm and minutes pointing to 12

How do I calculate the minutes properly? Please help me understand the formula.

EDIT: For example, if I call the function above with 7pm, e.g clockangles(7,0) I get the value 210. However, according this link the angle at 7:00 is 150

like image 263
CaseyJones Avatar asked Dec 15 '13 23:12

CaseyJones


People also ask

What is the angle between an hour hand and a minute hand at 10 30?

Hence our required angle between the hands of a clock as 10:30 is 135∘. Since 135∘ is greater than 90∘ so it is an obtuse angle.

What is the angle between clock hands?

The entire clock measures 360°. As the clock is divided into 12 sections, the distance between each number is equivalent to 30° (360/12). The distance between the 2 and the 3 on the clock is 30°.

How do you find the angle between hour and minute hand at 2 30?

∴∴ Angle between Hour Hand and Minute Hand = 165 - 60 = 105∘


1 Answers

In the following solution, the variable m refers to minutes, and the variable h to hours.

Let's separate the problem into its components.

  1. Find the angle of the minute hand from 12 o'clock.
  2. Find the angle of the hour hand from 12 o'clock.
  3. Find the absolute value of their difference.

Now, let's start solving each component.

  1. The minute hand makes a full cycle every hour, or 60 minutes. Therefore, we can get the completed percentage of the cycle of the minute hand by (m / 60). Since there are 360 degrees, we can get the angle of the minute hand from 12 o'clock by (m / 60) * 360.
  2. The hour hand makes a full cycle every 12 hours. Since there are 24 hours in a day, we need to normalize the hour value to 12 hours. This is accomplished by (h % 12), which returns the remainder of the hour value divided by 12.

    Now, as the minute hand makes its cycle, the hour hand does not just remain at the exact value of (h % 12). In fact, it moves 30 degrees between (h % 12) and (h % 12) + 1. The amount by which the hour hand deviates from (h % 12) can be calculated by adding to (h % 12) the completed percentage of the cycle of the minute hand, which is (m / 60). Overall, this gives us (h % 12) + (m / 60).

    Now that we have the exact position of the hour hand, we need to get the completed percentage of the cycle of the hour hand, which we can get by ((h % 12) + (m / 60)) / 12. Since there are 360 degrees, we can get the angle of the hour hand from 12 o'clock by (((h % 12) + (m / 60)) / 12) * 360.

  3. Now that we have both the angle of the minute and hour hand from 12 o'clock, we simply need to find the difference between the two values, and take the absolute value (since the difference can be negative).

    So overall, we have abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360).

Below is a python function that calculates this value. It will return whichever value of the angle that is the shortest.

def find_angle(h, m):
    if abs(((((m/60)+(h%12))/12)-(m/60))*360) > 180:
        return 360 - abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360)
    return abs(((((h % 12) + (m / 60)) / 12) - (m / 60)) * 360)
like image 55
Lynn Samson Avatar answered Nov 02 '22 12:11

Lynn Samson