Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clock angle
import unittest
from math import pi, degrees
def get_hour_angle(hour, minute=0):
angle = (hour / 6.) * pi
minute_angle = get_minute_angle(minute) / 12
return angle + minute_angle
def get_minute_angle(minute):
angle = (minute / 30.) * pi
return angle
def clock_angle(minute):
hour, minute = (minute / 60, minute % 60)
angle = abs(get_hour_angle(hour, minute) - get_minute_angle(minute))
if angle > pi:
angle = 2 * pi - angle
return angle
class ClockAngleTest(unittest.TestCase):
def test_get_hour_angle(self):
self.assertEqual(get_hour_angle(0), 0)
self.assertEqual(get_hour_angle(3), pi/2)
self.assertEqual(get_hour_angle(6), pi)
self.assertEqual(get_hour_angle(12), 2 * pi)
self.assertEqual(get_hour_angle(18), pi)
def test_get_minute_angle(self):
self.assertEqual(get_minute_angle(0), 0)
self.assertEqual(get_minute_angle(30), pi)
def test_get_clock_angle(self):
self.assertEqual(clock_angle(0), 0)
self.assertEqual(clock_angle(360), pi)
self.assertEqual(clock_angle(180), pi/2)
self.assertEqual(clock_angle(210), pi - (pi/2) - (pi/12))
self.assertEqual(round(degrees(clock_angle(140))), 50)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment