Advertisement
beezing

Racing clock - Pythonista

May 2nd, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. from scene import *
  2. from datetime import datetime
  3. from math import sin, cos, radians
  4.  
  5. WDAYS = ['MON','TUE','WED','THU','FRI','SAT','SUN']
  6.  
  7. font_HNT = 'HelveticaNeue-Thin'
  8. font_ARB = 'ArialRoundedMTBold'
  9.    
  10. # scene's text() shorcut
  11. def txt(x,y,t,f=font_HNT,s=14):
  12.   text(t,f,s,x,y)
  13.  
  14. # scene's ellipse() shortcut
  15. def circle(cx,cy,d):
  16.   ellipse(cx-d/2,cy-d/2,d,d)
  17.  
  18. # radius by angle in degree clockwise
  19. def rad(cx,cy,a,r):
  20.   x = cx + r*sin(radians(a))
  21.   y = cy + r*cos(radians(a))
  22.   return x,y
  23.  
  24. # arc by angle in degree clockwise
  25. def arc(cx,cy,a,b,r,t=2,p=0.5):
  26.   i = a
  27.   if a == b: b = b + p # avoid 0
  28.   while i < b:
  29.     x,y = rad(cx,cy,i,r)
  30.     circle(x,y,t) # "stroke" of the arc
  31.     i = i + p # precision in degree
  32.    
  33. def days_of_month(month,year):
  34.   if month == 2:
  35.     # kabisat year
  36.     if (year % 4 == 0) or (year % 1000 == 0):
  37.       result = 29
  38.     else:
  39.       result = 28
  40.   elif month in [1,3,5,7,8,10,12]:
  41.     result = 31
  42.   else: # in [4,6,9,11]
  43.     result = 30
  44.   return result
  45.  
  46. class clock(Scene):
  47.   def setup(self):
  48.     # settings
  49.     self.quartz = False
  50.     self.draw_reff = True
  51.     self.draw_text = True
  52.     self.draw_time = True
  53.     self.draw_date = True
  54.    
  55.   def should_rotate(self, orientation):
  56.     return True # support rotation
  57.    
  58.   def draw(self):
  59.     # get current datetime
  60.     n  = datetime.now()
  61.     wd = datetime.date(n).weekday()
  62.    
  63.     # setup clock dimension
  64.     sz = 12 # arc circle size
  65.     s = sz + 5 # space between arc
  66.     w = self.size.w
  67.     h = self.size.h
  68.     x = w/2 # origin of the clock
  69.     y = h/2
  70.     ls = w > h # landscape
  71.     d = h-s*2 if ls else w-s*2
  72.     r = d/2
  73.     td = '%02d-%02d-%02d'
  74.     tt = '%s, %02d:%02d:%02d'
  75.    
  76.     # setup clock values
  77.     if not self.quartz:
  78.       # milisecond
  79.       msec = n.microsecond / 10000.0
  80.       sec  = (n.second + msec/100) * 6.0
  81.     else:
  82.       sec  = n.second * 6.0
  83.     min  = (n.minute + (1.0*n.second)/60) * 6.0
  84.     pm   = n.hour >= 12; am = not pm
  85.     hour = n.hour-12 if pm else n.hour
  86.     hour = (hour + (1.0*n.minute)/60) * 30.0
  87.     dom  = days_of_month(n.month,n.year)
  88.     wday = (wd + (1.0*n.hour)/24) * 51.5
  89.     day  = (n.day-1 + (1.0*n.hour)/24) * 12
  90.     mon  = (n.month + (1.0*n.day)/dom) * dom
  91.     year = (n.year-2000 + (1.0*n.month)/12) * 3.6
  92.    
  93.     # default style
  94.     background(0.2,0.2,0.2)
  95.     stroke_weight(0)
  96.     stroke(1,1,1,1)
  97.     fill(1,1,1,1)
  98.    
  99.     # draw clock ticks
  100.     if self.draw_reff:
  101.       if not ls:
  102.         arc(x,y,0,360,r,1,6)
  103.         arc(x,y,0,360,r,3,30)
  104.         circle(x,y,2)
  105.         stroke(.25,.25,.25,1)
  106.         fill(.1,.1,.1,0)
  107.         stroke_weight(sz)
  108.         circle(x,y,2*(r-s)+sz)
  109.         circle(x,y,2*(r-2*s)+sz)
  110.         circle(x,y,2*(r-3*s)+sz)
  111.         circle(x,y,2*(r-4*s)+sz)
  112.         circle(x,y,2*(r-5*s)+sz)
  113.         circle(x,y,2*(r-6*s)+sz)
  114.         circle(x,y,2*(r-7*s)+sz)
  115.         stroke_weight(0)
  116.        
  117.     # draw clock text
  118.     if self.draw_text:
  119.       txt(x,y+1.2*r+s,'RACING CLOCK',font_ARB,22)
  120.       td = td % (n.day,n.month,n.year)
  121.       txt(x,y-1.1*r-s,td)
  122.       tt = tt % (WDAYS[wd],n.hour,n.minute,n.second)
  123.       txt(x,y-1.1*r-2*s,tt)
  124.  
  125.     # draw time arcs
  126.     if self.draw_time:
  127.       fill(1.0,0.0,0.0)
  128.       arc(x,y,0,sec,r-s,sz)
  129.       fill(1.0,0.5,0.0)
  130.       arc(x,y,sec,sec+min,r-2*s,sz)
  131.       fill(1.0,1.0,0.0)
  132.       arc(x,y,sec+min,sec+min+hour,r-3*s,sz)
  133.       fill(0.2,0.2,0.2,1)
  134.       arc(x,y,sec,sec,r-s,sz/2)
  135.       arc(x,y,sec+min,sec+min,r-2*s,sz/2)
  136.       arc(x,y,sec+min+hour,sec+min+hour,r-3*s,sz/2)
  137.      
  138.     # draw date arcs
  139.     if self.draw_date:
  140.       fill(0.0,1.0,0.0)
  141.       arc(x,y,sec+min+hour,sec+min+hour+wday,r-4*s,sz)
  142.       fill(0.0,0.0,1.0)
  143.       arc(x,y,sec+min+hour+wday,sec+min+hour+wday+day,r-5*s,sz)
  144.       fill(0.5,0.0,0.7)
  145.       arc(x,y,sec+min+hour+wday+day,sec+min+hour+wday+day+mon,r-6*s,sz)
  146.       fill(0.7,0.0,1.0)
  147.       arc(x,y,sec+min+hour+wday+day+mon,sec+min+hour+wday+day+mon+year,r-7*s,sz)
  148.       fill(0.2,0.2,0.2,1)
  149.       arc(x,y,sec+min+hour+wday,sec+min+hour+wday,r-4*s,sz/2)
  150.       arc(x,y,sec+min+hour+wday+day,sec+min+hour+wday+day,r-5*s,sz/2)
  151.       arc(x,y,sec+min+hour+wday+day+mon,sec+min+hour+wday+day+mon,r-6*s,sz/2)
  152.       arc(x,y,sec+min+hour+wday+day+mon+year,sec+min+hour+wday+day+mon+year,r-7*s,sz/2)
  153.    
  154. run(clock())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement