Advertisement
here2share

# analog_clock_arrow_demo.py

Jan 9th, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # analog_clock_arrow_demo.py
  2.  
  3. #    How to draw arrows on circumference.
  4. #    Copyright (C) 2009  Alexei Muzarov aka azex
  5. #
  6. #    This program is free software: you can redistribute it and/or modify
  7. #    it under the terms of the GNU General Public License as published by
  8. #    the Free Software Foundation, either version 3 of the License, or
  9. #    (at your option) any later version.
  10. #
  11. #    This program is distributed in the hope that it will be useful,
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #    GNU General Public License for more details.
  15. #
  16. #    You should have received a copy of the GNU General Public License
  17. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. #    if you have any question you can contact me via
  20. #    e-mail: azexmail@gmail.com
  21.  
  22. import appuifw as aw
  23. import e32
  24. import sysinfo
  25. from graphics import *
  26. import math
  27.  
  28. #switch to fullscreen mode
  29. aw.app.screen = 'full'
  30.  
  31. #get the screen size
  32. display = sysinfo.display_pixels()
  33.  
  34. #display = (176, 208)
  35. #display = (240, 320)
  36. #display = (320, 240)
  37. #display = (352, 416)
  38. #display = (480, 640)
  39.  
  40. def draw(rect):
  41.         canvas.blit(img)
  42.  
  43. #create the appuifw.Canvas object
  44. canvas = aw.Canvas(redraw_callback = draw)
  45.  
  46. aw.app.directional_pad = False
  47. aw.app.screen = 'large'
  48. aw.app.orientation = 'portrait'
  49.  
  50. #define application body as canvas
  51. aw.app.body = canvas
  52.  
  53. #create the graphics.Image object (clock)
  54. img = Image.new(display)
  55.  
  56. #fill the clock with black color
  57. img.clear(0x0)
  58.  
  59. #coordinate of the circumference center
  60. cpoint = (int(display[0]) / 2, int(display[1] / 2))
  61.  
  62. #diameter of the circumference
  63. if display[0] < display[1]:
  64.     diam = round(display[0] * 0.9)
  65. elif display[0] > display[1]:
  66.     diam = round(display[1] * 0.9)
  67. else:
  68.     diam = round(display[1] * 0.9)
  69.  
  70. #I prefer to work with circumferences having odd diameter
  71. if divmod(diam, 2)[1] == 0:
  72.     diam -= 1
  73.  
  74. #radius of the circumference
  75. ra = int(diam / 2)
  76.  
  77. #start point of the circumference
  78. dpoint = (cpoint[0] - ra, cpoint[1] - ra)
  79.  
  80. #radius-1 of the bigger arrow
  81. AB = int(ra * 0.95)
  82.  
  83. #radius-1 of the cut circumference of the bigger arrow
  84. ABC = int(AB * 0.9)
  85.  
  86. #radius-1 of the smaller arrow
  87. AS = int(ra * 0.7)
  88.  
  89. #radius-1 of the cut circumference of the smaller arrow
  90. ASC = int(AS * 0.9)
  91.  
  92. #coordinates of the circumference
  93. c_cor = (dpoint[0], dpoint[1], dpoint[0] + diam, dpoint[1] + diam)
  94.  
  95. #inside circumference coordinates
  96. ins = (dpoint[0] + int(ra*0.5), dpoint[1] + int(ra*0.5), dpoint[0] + diam - int(ra*0.5), dpoint[1] + diam - int(ra*0.5))
  97.  
  98. #draw clock
  99. def draw_clock(gradus1, gradus2):
  100.     #circumference
  101.     img.ellipse(c_cor, 0xffffff, 0xffffff)
  102.     #bigger arrow
  103.     d1 = gradus1 + 7
  104.     if d1 > 359: d1 -= 360
  105.     cr1 = calc(ABC, d1)
  106.     cr2 = calc(AB, gradus1)
  107.     d3 = gradus1 - 7
  108.     if d3 < 0: d3 += 360
  109.     cr3 = calc(ABC, d3)
  110.     img.polygon((cr1, cr2, cr3), 0xff0000, 0xff0000)
  111.     #smaller arrow
  112.     d1 = gradus2 + 7
  113.     if d1 > 359: d1 -= 360
  114.     cr1 = calc(ASC, d1)
  115.     cr2 = calc(AS, gradus2)
  116.     d3 = gradus2 - 7
  117.     if d3 < 0: d3 += 360
  118.     cr3 = calc(ASC, d3)
  119.     img.polygon((cr1, cr2, cr3), 0x0000ff, 0x0000ff)
  120.     #inside circumference
  121.     img.ellipse(ins, 0xffff, 0xffff)
  122.     draw(())
  123.  
  124. #translate from radians to degrees
  125. sin = lambda degree: math.sin((degree*math.pi)/180)
  126. #calculation the triangle side
  127. side = lambda radius, degree: round(radius*sin(degree))
  128.  
  129. #calculation of coordinate on the radius
  130. def calc(radius, degree):
  131.     if degree == 0:
  132.         cor = (cpoint[0] + radius, cpoint[1])
  133.     elif degree == 90:
  134.         cor = (cpoint[0], cpoint[1] - radius)
  135.     elif degree == 180:
  136.         cor = (cpoint[0] - radius, cpoint[1])
  137.     elif degree == 270:
  138.         cor = (cpoint[0], cpoint[1] + radius)
  139.     elif 0 < degree < 90:
  140.         #offset by X
  141.         offx = int(side(radius, 90-degree))
  142.         #offset by Y
  143.         offy = int(side(radius, degree))
  144.         cor = (cpoint[0] + offx, cpoint[1] - offy)
  145.     elif 90 < degree < 180:
  146.         offx = int(side(radius, degree-90))
  147.         offy = int(side(radius, 180-degree))
  148.         cor = (cpoint[0] - offx, cpoint[1] - offy)
  149.     elif 180 < degree < 270:
  150.         offx = int(side(radius, 270-degree))
  151.         offy = int(side(radius, degree-180))
  152.         cor = (cpoint[0] - offx, cpoint[1] + offy)
  153.     elif 270 < degree < 360:
  154.         offx = int(side(radius, degree-270))
  155.         offy = int(side(radius, 360-degree))
  156.         cor = (cpoint[0] + offx, cpoint[1] + offy)
  157.     return int(cor[0]), int(cor[1])
  158.  
  159. def drawer():
  160.     global ex
  161.     deg1 = 0
  162.     deg2 = 0
  163.     check = 0
  164.     while ex:
  165.         draw_clock(deg1, deg2)
  166.         deg1 -= 1
  167.         check +=1
  168.         if deg1 == -1:
  169.             deg1 = 359
  170.         if check == 5:
  171.             check = 0
  172.             deg2 -= 1
  173.             if deg2 == -1:
  174.                 deg2 = 359
  175.         e32.ao_sleep(0.1)
  176.         e32.ao_yield()
  177.  
  178. ex = 1
  179.  
  180. def exit():
  181.     global ex
  182.     ex = 0
  183.     lock.signal()
  184.  
  185. lock = e32.Ao_lock()
  186. aw.app.exit_key_handler = exit
  187.  
  188. e32.ao_sleep(0.1, drawer)
  189.  
  190. lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement