#!/usr/bin/python3
# ===================================================================
# analog clock example #2
# ===================================================================
import math
import numpy
import graphics as gr
import coordinate_conversion as cc
import draw_xy_axes as ax
radius = 300 # clock hand length (radius)
# -------------------------------------------------------------------
# ---- convert angle to cartesian coordinates relative to origin
# ---- ox,oy origin x,y
# ---- ad angle in degrees
# ---- r raidus
# ---- return integer x,y coordinates
# -------------------------------------------------------------------
def angle_to_xy(ox,oy,ad,r):
ar = numpy.deg2rad(ad)
x = round(math.sin(ar)*r + ox)
y = round(math.cos(ar)*r + oy)
return (x,y)
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
if '__main__' == __name__:
# ---- create graphics window
win = gr.GraphWin("Analog Clock", 800, 800)
## ---- draw x,y axes (debug center of window)
##ax.draw_xy_axes(win)
# ---- draw clock outline (circle) at the
# ---- center of the graphics window
wcx,wcy = cc.center_to_win_coords(0,0,win.width,win.height)
c = gr.Circle(gr.Point(wcx,wcy), radius+40)
c.setFill('white')
c.setOutline('black')
c.setWidth(6)
c.draw(win)
# ---- draw clock hand for each angle
for ang in [0, 45, 90, 135, 180, 225, 270, 315, 360 ]:
x,y = angle_to_xy(0,0,ang,radius)
wx,wy = cc.center_to_win_coords(x,y,win.width,win.height)
l = gr.Line(gr.Point(wcx,wcy),gr.Point(wx,wy))
##l.setOutline('black')
l.setWidth(10)
l.draw(win)
# ---- draw clock center (circle)
c = gr.Circle(gr.Point(wcx,wcy), 20)
c.setFill('black')
c.setOutline('black')
c.setWidth(1)
c.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done