Python 如何计算时针和分针之间的角度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20601427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:49:31  来源:igfitidea点击:

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

pythonmathclock

提问by CaseyJones

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:

所以,据此,我想我可以在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.

一小时内,它工作正常,因为它似乎具有 1=1 映射。但至少现在有一个问题。到 0 分钟时,分针指向 12。

For example:

例如:

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

晚上 7 点:指针指向晚上 7 点,分钟指向 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 linkthe angle at 7:00 is 150

编辑:例如,如果我在晚上 7 点调用上面的函数,例如 clockangles(7,0) 我得到的值为 210。但是,根据此链接,7:00 的角度为 150

采纳答案by roippi

Okay. You are trying to find the angle between the two hands. Then this:

好的。您正在尝试找到两只手之间的角度。然后这个:

minutes degree = 360 / 12 / 60 = 0.5

Is just the number of degrees the hourhand moves per minute. Think about it - the minute hand travels a full 360 each hour. Therefore there are only 60 minutes in a full revolution. 360/60 = 6 degrees per minute for the minute hand.

只是时针每分钟移动的度数。想想看——分针每小时走整整 360 圈。因此,一整圈只有 60 分钟。360/60 = 分针每分钟 6 度。

So, you need to find the difference between the hour and the minute hand. Thus the function now looks like:

所以,你需要找出时针和分针的区别。因此该函数现在看起来像:

def clockangles(hour, minute):
    return (hour * 30 + minute * 0.5) - (minute * 6)

Now, this is valid, so we could stop here. However I should explain that this can give both answers larger than 180 degrees and negative angles. If you don't want those things (and from your comments it appears that you don't), correct for them.

现在,这是有效的,所以我们可以到此为止。但是我应该解释一下,这可以给出大于 180 度和负角的答案。如果您不想要这些东西(并且从您的评论看来您不想要),请更正它们。

def clockangles(hour, minute):
    return abs((hour * 30 + minute * 0.5) - (minute * 6))

Now, no negative angles.

现在,没有负角。

def clockangles(hour, minute):
    ans = abs((hour * 30 + minute * 0.5) - (minute * 6))
    return min(360-ans,ans)

Now, the shorter of the two angles formed by measuring clockwise and counterclockwise.

现在,通过顺时针和逆时针测量形成的两个角度中较短的一个。

回答by Tamim

I have worked on this problem and created an equation:

我已经解决了这个问题并创建了一个等式:

(hr*30)+(min/2)-(min*6) 

or

或者

(min*6)-(hr*30)-(min/2) 

回答by Saurabh

This is not all that difficult if you think about it. Lets consider each hand in isolation first. The minutes hand of the clock rotates 360 degrees in 60 minutes so each minute represents 6 degrees. The hours hand of the clock rotates 360 degrees in 12 hours so we know it moves a total of 30 degrees after each hour, but you need to factor in the advancement of the hours hand between hours. i.e. at 3:30 the minutes hand is on 6 and the hours hand has progressed past 3. We can calculate this advancement simply by (minutes/60) * 30 degrees which is equivalent to minutes/2. So once we know the degrees of each hand we simply find the difference. And formula will be like

如果你考虑一下,这并不是那么困难。让我们首先单独考虑每手牌。时钟的分针在 60 分钟内旋转 360 度,因此每分钟代表 6 度。时钟的时针在 12 小时内旋转 360 度,所以我们知道它在每小时后总共移动 30 度,但您需要考虑小时之间时针的前进。即在 3:30 分针在 6 并且时针已经超过 3。我们可以简单地通过 (minutes/60) * 30 度计算这个进步,相当于分钟/2。所以一旦我们知道每只手的度数,我们就可以简单地找到差异。公式会像

 degrees = Math.Abs(((hour*30.0 + minute/2.0) - minute*6.0) % 360)

回答by Prateek Joshi

Use the algorithm:

使用算法:

1.Minute angle= 360 * minutes / 60

1.分角=360*分/60

2.Hour angle= [ 360 * (hour % 12) / 12 ] + [ 360 * (minutes / 60) * (1 / 12) ]

2.时角= [ 360 * (hour % 12) / 12 ] + [ 360 * (minutes / 60) * (1 / 12) ]

3.Angle between hour and minute= (hour angle - minute angle) % 360

3.时分夹角=(时角-分角)% 360

this reducesto 30 * hours - 5.5 * minutes.

减少30 * 小时 - 5.5 * 分钟

回答by Uchit Patel

  1. Multiply hours by 60 that is convert it into minutes. hours*60=minutes

  2. Now add the given minutes and converted minutes.

    given minutes + converted mintes = total minutes

  3. Now divide the total minutes by 2, that is to find its average. total minutes / 2

  4. Now multiply given minutes by 6. given minutes * 6

  5. Now subtract point 3 from point 4.

  1. 将小时乘以 60,将其转换为分钟。小时*60=分钟

  2. 现在添加给定的分钟数和转换后的分钟数。

    给定分钟数 + 转换分钟数 = 总分钟数

  3. 现在将总分钟数除以 2,即求其平均值。总分钟数 / 2

  4. 现在将给定的分钟数乘以 6。给定的分钟数 * 6

  5. 现在从第 4 点减去第 3 点。

By this method you will get the accurate answer.

通过这种方法,您将得到准确的答案。

回答by Lynn Samson

In the following solution, the variable mrefers to minutes, and the variable hto hours.

在下面的解决方案中,变量m指的是分钟,变量h指的是小时。

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.
  1. 从 12 点钟方向找出分针的角度。
  2. 找出时针从 12 点钟方向的角度。
  3. 求它们的差值的绝对值。

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

  1. 分针每小时或 60 分钟完成一个完整的循环。因此,我们可以通过 得到分针周期的完成百分比(m / 60)。由于有 360 度,所以我们可以通过 12 点来获得分针的角度(m / 60) * 360
  2. 时针每 12 小时完成一个完整的循环。由于一天有 24 小时,我们需要将小时值标准化为 12 小时。这是由 完成的(h % 12),它返回小时值除以 12 的余数。

    现在,当分针循环时,时针不会仅仅停留在 的精确值上(h % 12)。事实上,它在(h % 12)和之间移动了 30 度(h % 12) + 1。时针偏离的量(h % 12)可以通过添加到(h % 12)分针周期的完成百分比来计算,即(m / 60)。总的来说,这给了我们(h % 12) + (m / 60).

    现在我们有了时针的确切位置,我们需要得到时针周期的完成百分比,我们可以通过((h % 12) + (m / 60)) / 12。由于有 360 度,我们可以从 12 点得到时针的角度(((h % 12) + (m / 60)) / 12) * 360

  3. 现在我们有分针和时针从 12 点开始的角度,我们只需要找到这两个值之间的差异,并取绝对值(因为差异可能是负数)。

    所以总的来说,我们有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.

下面是一个计算这个值的python函数。它将返回最短的角度值。

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)

回答by Salamandr

  def clock_angle(time):
    constH = 360 / 12
    constM = 360 / 60
    if ":" in time:
        clock = time.split(":")
        c1 = int(clock[0])
        c2 = int(clock[1])
        if c1 >= 12:
            c1 -= 12
        rc1 = c1*constH + constH*(c2/60)
        rc2 = c2*constM
        if rc1 > rc2:
            result = (rc1-rc2)
            angle2 = 360-rc1+rc2
        else:
            result = (rc2-rc1)
            angle2 = 360-rc2+rc1
        if angle2 < result:
            result = angle2
        return result
    return 0

回答by MIIK7

h = int(input())
m = int(input())
angle = float(abs(11 /2 * m - 30 * h))
print(" {}:{} makes the following angle {}°".format(h, m, angle))

回答by Manoj Selvin

Note :

60min = 360deg

1min = 6deg

注:

60 分钟 = 360 度

1 分钟 = 6 度

eg:-consider time as 2:20(ie. 2hr 20min)

abs((2hr*5) min - (20)min) = 10min

例如:-将时间视为2:20(即 2 小时 20 分钟)

abs((2hr*5) min - (20)min) = 10min

angle_1 = 10min x 6deg = 60deg

angle_2 = 360deg - 60deg = 300deg (means the angle on the other side)

angle_1 = 10min x

6deg = 60deg angle_2 = 360deg - 60deg = 300deg(表示另一侧的角度)

So out of the two angles angle_1 is small.

hence, min_angle = 60deg

所以在两个角度中,angle_1 很小。

因此,min_angle = 60deg

def min_angle_bet_hr_min(hr, min):

    angle_1 = (hr*5 - min)*6
    angle_2 = 360 - angle_1

    if angle_1 < angle_2:
        min_angle = angle_1
    elif angle_1 > angle_2:
        min_angle = angle_2
    else:
        min_angle = 0

    return abs(min_angle)