Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # analog_clock_arrow_demo.py
- # How to draw arrows on circumference.
- # Copyright (C) 2009 Alexei Muzarov aka azex
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- # if you have any question you can contact me via
- # e-mail: azexmail@gmail.com
- import appuifw as aw
- import e32
- import sysinfo
- from graphics import *
- import math
- #switch to fullscreen mode
- aw.app.screen = 'full'
- #get the screen size
- display = sysinfo.display_pixels()
- #display = (176, 208)
- #display = (240, 320)
- #display = (320, 240)
- #display = (352, 416)
- #display = (480, 640)
- def draw(rect):
- canvas.blit(img)
- #create the appuifw.Canvas object
- canvas = aw.Canvas(redraw_callback = draw)
- aw.app.directional_pad = False
- aw.app.screen = 'large'
- aw.app.orientation = 'portrait'
- #define application body as canvas
- aw.app.body = canvas
- #create the graphics.Image object (clock)
- img = Image.new(display)
- #fill the clock with black color
- img.clear(0x0)
- #coordinate of the circumference center
- cpoint = (int(display[0]) / 2, int(display[1] / 2))
- #diameter of the circumference
- if display[0] < display[1]:
- diam = round(display[0] * 0.9)
- elif display[0] > display[1]:
- diam = round(display[1] * 0.9)
- else:
- diam = round(display[1] * 0.9)
- #I prefer to work with circumferences having odd diameter
- if divmod(diam, 2)[1] == 0:
- diam -= 1
- #radius of the circumference
- ra = int(diam / 2)
- #start point of the circumference
- dpoint = (cpoint[0] - ra, cpoint[1] - ra)
- #radius-1 of the bigger arrow
- AB = int(ra * 0.95)
- #radius-1 of the cut circumference of the bigger arrow
- ABC = int(AB * 0.9)
- #radius-1 of the smaller arrow
- AS = int(ra * 0.7)
- #radius-1 of the cut circumference of the smaller arrow
- ASC = int(AS * 0.9)
- #coordinates of the circumference
- c_cor = (dpoint[0], dpoint[1], dpoint[0] + diam, dpoint[1] + diam)
- #inside circumference coordinates
- 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))
- #draw clock
- def draw_clock(gradus1, gradus2):
- #circumference
- img.ellipse(c_cor, 0xffffff, 0xffffff)
- #bigger arrow
- d1 = gradus1 + 7
- if d1 > 359: d1 -= 360
- cr1 = calc(ABC, d1)
- cr2 = calc(AB, gradus1)
- d3 = gradus1 - 7
- if d3 < 0: d3 += 360
- cr3 = calc(ABC, d3)
- img.polygon((cr1, cr2, cr3), 0xff0000, 0xff0000)
- #smaller arrow
- d1 = gradus2 + 7
- if d1 > 359: d1 -= 360
- cr1 = calc(ASC, d1)
- cr2 = calc(AS, gradus2)
- d3 = gradus2 - 7
- if d3 < 0: d3 += 360
- cr3 = calc(ASC, d3)
- img.polygon((cr1, cr2, cr3), 0x0000ff, 0x0000ff)
- #inside circumference
- img.ellipse(ins, 0xffff, 0xffff)
- draw(())
- #translate from radians to degrees
- sin = lambda degree: math.sin((degree*math.pi)/180)
- #calculation the triangle side
- side = lambda radius, degree: round(radius*sin(degree))
- #calculation of coordinate on the radius
- def calc(radius, degree):
- if degree == 0:
- cor = (cpoint[0] + radius, cpoint[1])
- elif degree == 90:
- cor = (cpoint[0], cpoint[1] - radius)
- elif degree == 180:
- cor = (cpoint[0] - radius, cpoint[1])
- elif degree == 270:
- cor = (cpoint[0], cpoint[1] + radius)
- elif 0 < degree < 90:
- #offset by X
- offx = int(side(radius, 90-degree))
- #offset by Y
- offy = int(side(radius, degree))
- cor = (cpoint[0] + offx, cpoint[1] - offy)
- elif 90 < degree < 180:
- offx = int(side(radius, degree-90))
- offy = int(side(radius, 180-degree))
- cor = (cpoint[0] - offx, cpoint[1] - offy)
- elif 180 < degree < 270:
- offx = int(side(radius, 270-degree))
- offy = int(side(radius, degree-180))
- cor = (cpoint[0] - offx, cpoint[1] + offy)
- elif 270 < degree < 360:
- offx = int(side(radius, degree-270))
- offy = int(side(radius, 360-degree))
- cor = (cpoint[0] + offx, cpoint[1] + offy)
- return int(cor[0]), int(cor[1])
- def drawer():
- global ex
- deg1 = 0
- deg2 = 0
- check = 0
- while ex:
- draw_clock(deg1, deg2)
- deg1 -= 1
- check +=1
- if deg1 == -1:
- deg1 = 359
- if check == 5:
- check = 0
- deg2 -= 1
- if deg2 == -1:
- deg2 = 359
- e32.ao_sleep(0.1)
- e32.ao_yield()
- ex = 1
- def exit():
- global ex
- ex = 0
- lock.signal()
- lock = e32.Ao_lock()
- aw.app.exit_key_handler = exit
- e32.ao_sleep(0.1, drawer)
- lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement