Author Topic: Analog (with integrated digital) clock  (Read 251 times)

Cybermonkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 515
    • View Profile
    • Home of EGSL
Analog (with integrated digital) clock
« on: 01. January 2016, 18:33:54 »
This was already posted on the old forum but since that is gone ... I just added a digital display to the analog clock.
Feel free to port this to your preferred language.
Code: [Select]
-- analog clock
-- converted to PulsarLua by Cybermonkey
require "scancodes"

win = openwindow ("Analog Clock",-1,-1,350,350)
setactivewindow (win)
setframetimer (60)
page = 0
xcenter = windowwidth()/2
ycenter = windowheight()/2

backcolor  (0,90,82,255)
textsize (2)
texttype (2)

repeat
cls()
key = getkey()

color (0,0,0,255)

fillcircle (xcenter, ycenter, 150)

color  (50,255,50,255)

for i=1, 11 do
if (i~=3) and (i~=6) and (i~=9) then
fillcircle (round(math.cos((i * 30) * math.pi / 180 - math.pi / 2) * 140 + xcenter), round(math.sin((i * 30) * math.pi / 180 - math.pi / 2) * 140 + ycenter), 3)
end
end

color  (255,0,0,255)

drawtext ("9",xcenter - 145, ycenter - 5)
drawtext ("3",xcenter + 135, ycenter - 5)
drawtext ("12",xcenter - 15, ycenter - 145)
drawtext ("6",xcenter - 5, ycenter + 130)

color (0,255,0,255)
drawtext (time(),xcenter-60 , ycenter -70)

second = os.date ("%S")
minute = os.date ("%M")
hour = os.date ("%H")
xsecond = round(math.cos(second * math.pi / 30 - math.pi / 2) * 120 + xcenter)
ysecond = round(math.sin(second * math.pi / 30 - math.pi / 2) * 120 + ycenter)
xminute = round(math.cos(minute * math.pi / 30 - math.pi / 2) * 100 + xcenter)
yminute = round(math.sin(minute * math.pi / 30 - math.pi / 2) * 100 + ycenter)
xhour = round(math.cos((hour * 30 + minute / 2) * math.pi / 180 - math.pi / 2) * 80 + xcenter)
yhour = round(math.sin((hour * 30 + minute / 2) * math.pi / 180 - math.pi / 2) * 80 + ycenter)

color (0,255,0,255)
line (xcenter, ycenter, xsecond, ysecond)
color (0,0,255,255)
line (xcenter, ycenter - 1, xminute, yminute)
line (xcenter - 1, ycenter, xminute, yminute)
color (255,0,0,255)
line (xcenter, ycenter - 1, xhour, yhour)
line (xcenter - 1, ycenter, xhour, yhour)

sync()
until key == SCANCODE_ESCAPE
closewindow (win)
closeapplication()
Best regards,
Cybermonkey

ZXDunny

  • Newcomer
  • *
  • Posts: 41
    • View Profile
Re: Analog (with integrated digital) clock
« Reply #1 on: 09. January 2016, 17:40:47 »
I remember messing around with this one back on the old forum!

Here's my updated version, in SpecBAS natch:

Code: [Select]
10 REM Analog Clock with sweep-second-hand
20 PALETTE 8,0,90,82: SCREEN LOCK: DEGREES: OPTION "base",0
30 xc=SCRw/2,yc=SCRh/2: GO SUB 190
40 REM Draw the clock face
50 CLS : CIRCLE INK 0;xc,yc,150 FILL
60 FOR f=0 TO 59: CIRCLE INK 3;xc+COS(6*f)*136,yc+SIN(6*f)*136,1.5 FILL: NEXT f
70 FOR f=0 TO 11: x=xc+COS(30*f)*136,y=yc+SIN(30*f)*136: IF f MOD 3 THEN s$=STR$((f+3) MOD 12): PRINT PAPER 0;INK 4;MOVE x-4*LEN s$,y-4;INK 4;s$ ELSE s$=STR$ IIF(f=9,12,(f+3) MOD 12): PRINT PAPER 0;MOVE x-(8*LEN s$),y-8;SCALE 2,2;INK 10;s$
80 NEXT f
90 REM Calculate the angles of the hands
100 t=TIME,s=6*SECONDS t+MILLISECONDS t*.006,m=6*MINUTES t+(6*(s/360)),h=30*HOUR t+(30*(m/360)),s-=90,m-=90,h-=90
110 a$=TIME$(TIME,"hh:mm:ss"): GO SUB 250
120 REM Draw the hands
130 POLYGON INK 10;xc+COS(m-90)*3,yc+SIN(m-90)*3 TO xc+COS(m)*100,yc+SIN(m)*100 TO xc+COS(m+90)*3,yc+SIN(m+90)*3 FILL
140 POLYGON INK 9;xc+COS(h-90)*2,yc+SIN(h-90)*2 TO xc+COS(h)*80,yc+SIN(h)*80 TO xc+COS(h+90)*2,yc+SIN(h+90)*2 FILL
150 POLYGON INK 12;xc+COS(s-90)*1.5,yc+SIN(s-90)*1.5 TO xc+COS(s)*120,yc+SIN(s)*120 TO xc+COS(s+90)*1.5,yc+SIN(s+90)*1.5 FILL
160 CIRCLE INK 11;xc,yc,8 FILL : CIRCLE INK 0;xc,yc,4 FILL
170 IF INKEY$="" THEN WAIT SCREEN : GO TO 50
180 REM Set up LED segments
190 sw=1,svh=6,shw=4
200 vd$="sx,sy TO sx+sw,sy+sw TO sx+sw,sy+svh+sw TO sx,sy+sw+svh+sw TO sx-sw,sy+svh+sw TO sx-sw,sy+sw",hd$="sx,sy TO sx+sw,sy-sw TO sx+sw+shw,sy-sw TO sx+sw+shw+sw,sy TO sx+sw+shw,sy+sw TO sx+sw,sy+sw"
210 DIM digit$="123456","34","23567","23457","1347","12457","124567","234","1234567","123457" BASE 0: DIM coord$="sw,sw+1","sw+1,sw","sw*3+shw+2,sw+1","sw*3+shw+2,sw*3+svh+3","sw+1,sw*5+svh*2+4","sw,sw*3+svh+3","sw+1,sw*3+svh+2" BASE 1
220 DIM led$(10) BASE 0: FOR f=0 TO 9: b$="": FOR g=1 TO LEN digit$(f): b=VAL digit$(f)(g): b$=b$+REPLACE$("sx=x+"+coord$(b),",",",sy=y+")+":POLYGON "+IIF$(b IN [2,7,5],hd$,vd$)+" FILL:": NEXT g: led$(f)=TOKEN$ b$: NEXT f
230 RETURN
240 REM Draw LED segment string
250 INK 1: x=xc-48,y=yc+64: FOR i=1 TO LEN a$: IF a$(i) IN ["0" TO "9"] THEN EXECUTE led$(VAL a$(i)): x+=shw+sw+sw+6 ELSE IF (s+90) MOD 6<3 THEN RECTANGLE 3+x+sw+shw/2,2+y+sw+svh/2,2,2 FILL: RECTANGLE 3+x+sw+shw/2,2+y+sw+svh*2,2,2 FILL: x+=shw+sw*2+6 ELSE x+=shw+sw*2+6
260 NEXT i: RETURN



And here's (attached) a small executable-ish... Run the batch file after unpacking :)

D.

Cybermonkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 515
    • View Profile
    • Home of EGSL
Re: Analog (with integrated digital) clock
« Reply #2 on: 13. February 2016, 19:57:44 »
Now this is also possible with AllegroBASIC v0.6 (I implemented a TIME function - downloads are coming soon).
Code: [Select]
' analog clock
' converted from Pascal to Lua to AllegroBASIC
' by Cybermonkey 2/2016

KEY_ESC          = 59
screen (352,352,"Analog Clock")
setfps (60) ' That's enough ...
PI = 3.141
xcenter = screenwidth/2
ycenter = screenheight/2
paper (0,90,82)
textsize (2)

do
cls
ink (0,0,0)
fillcircle (xcenter, ycenter, 150)
ink (50,255,50)
fillcircle (175,175,3)
for i=1 to 11
  if (i<>3) and (i<>6) and (i<>9) then
    fillcircle (round(cos((i * 30) * PI / 180 - PI / 2) * 140 + xcenter), round(sin((i * 30) * PI / 180 - PI / 2) * 140 + ycenter), 3)
 endif
next

ink (255,0,0)

drawtext (xcenter - 145, ycenter - 5,"9")
drawtext (xcenter + 135, ycenter - 5,"3")
drawtext (xcenter - 15, ycenter - 145,"12")
drawtext (xcenter - 5, ycenter + 130,"6")

ink (0,255,0)

second = val (right (time,2))
hour = val (left (time,2))
minute = val (mid (time,3,2))

xsecond = round(cos(second * PI / 30 - PI / 2) * 120 + xcenter)
ysecond = round(sin(second * PI / 30 - PI / 2) * 120 + ycenter)
xminute = round(cos(minute * PI / 30 - PI / 2) * 100 + xcenter)
yminute = round(sin(minute * PI / 30 - PI / 2) * 100 + ycenter)
xhour = round(cos((hour * 30 + minute / 2) * PI / 180 - PI / 2) * 80 + xcenter)
yhour = round(sin((hour * 30 + minute / 2) * PI / 180 - PI / 2) * 80 + ycenter)

ink (0,255,0)
line (xcenter, ycenter, xsecond, ysecond)
ink (0,0,255)
line (xcenter, ycenter - 1, xminute, yminute)
line (xcenter - 1, ycenter, xminute, yminute)
ink (255,0,0)
line (xcenter, ycenter - 1, xhour, yhour)
line (xcenter - 1, ycenter, xhour, yhour)
ink(255,255,255):textsize (1)
drawtext (xcenter - 30, ycenter + 160,TIME)
textsize (2)
sync
until (keystate (KEY_ESC) = true) or (windowclosed=true)
Best regards,
Cybermonkey