<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: guido code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 02:18:41 GMT</pubDate>
    <description>DZone Snippets: guido code</description>
    <item>
      <title>M Clock on pys60</title>
      <link>http://snippets.dzone.com/posts/show/571</link>
      <description>See this article in Guido's blog&lt;br /&gt;http://www.artima.com/weblogs/viewpost.jsp?thread=122250&lt;br /&gt;I then try to implement M Clock on pys60. I remove a few&lt;br /&gt;things to make the code short.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from __future__ import division&lt;br /&gt;from appuifw import *&lt;br /&gt;import math, time, e32&lt;br /&gt;&lt;br /&gt;app.body = c = Canvas()&lt;br /&gt;&lt;br /&gt;radius = 72&lt;br /&gt;bigsize = radius * .975&lt;br /&gt;litsize = radius * .67&lt;br /&gt;mx, my = 88, 72&lt;br /&gt;N = 9   # 2,3,4,5,6, 32, 128&lt;br /&gt;&lt;br /&gt;def draw(hh, mm, ss, colors=(0, 1, 2)):&lt;br /&gt;    # Set bigd, litd to angles in degrees for big, little hands&lt;br /&gt;    # 12 =&gt; 90, 3 =&gt; 0, etc.&lt;br /&gt;    bigd = (90 - (mm*60 + ss) / 10) % 360&lt;br /&gt;    litd = (90 - (hh*3600 + mm*60 + ss) / 120) % 360&lt;br /&gt;    # Set bigr, litr to the same values in radians&lt;br /&gt;    bigr = bigd * math.pi / 180&lt;br /&gt;    litr = litd * math.pi / 180&lt;br /&gt;    # Draw the background colored arcs&lt;br /&gt;    drawbg(bigd, litd, colors)&lt;br /&gt;    # Draw the hands&lt;br /&gt;    c.line([mx, my, &lt;br /&gt;            mx + bigsize*math.cos(bigr),&lt;br /&gt;            my - bigsize*math.sin(bigr)],&lt;br /&gt;            0, width = radius/50)&lt;br /&gt;    c.line([mx, my, &lt;br /&gt;            mx + litsize*math.cos(litr),&lt;br /&gt;            my - litsize*math.sin(litr)],&lt;br /&gt;            0, width = radius/33)&lt;br /&gt;    # Draw the text&lt;br /&gt;    c.text([5, 144-5], u"%02d:%02d:%02d" % (hh, mm, ss), 0xffffff)&lt;br /&gt;&lt;br /&gt;def drawbg(bigd, litd, colors=(0, 1, 2)):&lt;br /&gt;    c.clear(0)&lt;br /&gt;    table = []&lt;br /&gt;    for angle, colorindex in [(bigd - 180/N, 0),&lt;br /&gt;                              (litd - 180/N, 1),&lt;br /&gt;                              (  90 - 180/N, 2)]:&lt;br /&gt;        angle %= 360&lt;br /&gt;        for i in range(N):&lt;br /&gt;            color = 255&lt;br /&gt;            if colorindex in colors:&lt;br /&gt;                color = (N-1-i)*color//(N-1)&lt;br /&gt;            table.append((angle, color, colorindex))&lt;br /&gt;            angle += 360/N&lt;br /&gt;            if angle &gt;= 360:&lt;br /&gt;                angle -= 360&lt;br /&gt;                table.append((0, color, colorindex))&lt;br /&gt;    table.sort()&lt;br /&gt;    table.append((360, None))&lt;br /&gt;    fill = [0, 0, 0]&lt;br /&gt;    for i in range(len(table)-1):&lt;br /&gt;        angle, color, colorindex = table[i]&lt;br /&gt;        fill[colorindex] = color&lt;br /&gt;        if angle &lt; 359:     # for bug when 359==360==0&lt;br /&gt;            c.pieslice([mx-radius,my-radius,mx+radius,my+radius], &lt;br /&gt;                      angle * math.pi/180, 0,    # start, end&lt;br /&gt;                      fill=tuple(fill), width=0)&lt;br /&gt;    c.line([mx+1, my, mx+radius-1, my], tuple(fill))  # complete at 360 deg&lt;br /&gt;&lt;br /&gt;running = 1&lt;br /&gt;def quit():&lt;br /&gt;    global running&lt;br /&gt;    running = 0&lt;br /&gt;app.exit_key_handler= quit&lt;br /&gt;&lt;br /&gt;while running:  # redraw loop&lt;br /&gt;    t = time.time() + time.clock()%1    # time() lack decimal precision&lt;br /&gt;    hh, mm, ss = time.localtime(t)[3:6] # +7*60*60&lt;br /&gt;    draw(hh, mm, ss, (0,1,2))&lt;br /&gt;    e32.ao_sleep(1-t%1)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You can see the sceenshot from here.&lt;br /&gt;http://flickr.com/photos/korakot/32337789/</description>
      <pubDate>Tue, 09 Aug 2005 03:08:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/571</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
