## the mathematical analog clock

General FreeBASIC programming questions.
ron77
Posts: 33
Joined: Feb 21, 2019 19:24
Location: Israel

### the mathematical analog clock

hi all...

and here i take my 1500\$ machine and transform it to a 19.99\$ analog kitchen clock lol!

special thanks to Mishka (michael kunin) my roommate and to my teacher itay for their help... this was my first qb64 program...

Code: Select all

`'_TITLE "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"SCREEN 12'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)CONST True = 1CONST False = 0DIM SHARED hour, minute, sec, pihour = VAL(LEFT\$(TIME\$, 2))minute = VAL(MID\$(TIME\$, 4, 2))sec = VAL(RIGHT\$(TIME\$, 2))hour = hour + minute / 60 + sec / 3600minute = minute + sec / 60pi = 3.14159265Sub clock    DO        CLS        PRINT TIME\$        PRINT "press ESC to exit  "        CIRCLE (320, 240), 200        CIRCLE (320, 240), 215        FOR j = 0 TO 59            LINE (320 + 190 * COS((90 - j * 6) * 2 * pi / 360), 240 - 190 * SIN((90 - j * 6) * 2 * pi / 360))-(320 + 200 * COS((90 - j * 6) * 2 * pi / 360), (240 - 200 * SIN((90 - j * 6) * 2 * pi / 360)))        NEXT        FOR i = 0 TO 11            LINE (320 + 205 * COS((90 - i * 30) * 2 * pi / 360), 240 - 205 * SIN((90 - i * 30) * 2 * pi / 360))-(320 + 210 * COS((90 - i * 30) * 2 * pi / 360), (240 - 210 * SIN((90 - i * 30) * 2 * pi / 360)))        NEXT        t = 90 - sec * 6        t1 = 90 - minute * 6        t2 = 90 - hour * 30        LINE (320, 240)-(320 + 200 * COS(t * 2 * pi / 360), (240 - 200 * SIN(t * 2 * pi / 360)))        LINE (320, 240)-(320 + 180 * COS(t1 * 2 * pi / 360), (240 - 180 * SIN(t1 * 2 * pi / 360)))        LINE (320, 240)-(320 + 120 * COS(t2 * 2 * pi / 360), (240 - 120 * SIN(t2 * 2 * pi / 360)))        SLEEP (1)        sec = sec + 1        minute = minute + (1 / 60)        hour = hour + (1 / 3600)        k\$ = INKEY\$        IF k\$ = CHR\$(32) THEN set = True: EXIT SUB        IF k\$ = CHR\$(27) THEN END    LOOPEND SUBCLSclock`
thebigh
Posts: 34
Joined: Dec 14, 2018 11:11

### Re: the mathematical analog clock

Nice work!
angros47
Posts: 1616
Joined: Jun 21, 2005 19:04

### Re: the mathematical analog clock

This is absolutely NOT recommended:

ron77 wrote:

Code: Select all

`        SLEEP (1)        sec = sec + 1        minute = minute + (1 / 60)        hour = hour + (1 / 3600)        k\$ = INKEY\$`

The command SLEEP doesn't guarantee to pause exactly one second (if a key is pressed, for example, SLEEP terminates immediately). Even worse, other instructions, like INKEY, LINE and so on introduce further delays, that cause the time elapsed in every cycle to be likely more than one second (not much, but the offset would increase at every cycle, and after a while the clock would display the wrong time)

A better solution would be to acquire the value of sec. ,minute and hour from TIME, like you did in the beginning, at every cycle.
Posts: 2000
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: the mathematical analog clock

Suggested rewrite, without changing too much:

Code: Select all

`#lang "QB"'_TITLE "THE MATHEMATICAL ANALOG CLOCK VERSION 3 - WITHOUT CALIBRATION"SCREEN 12'DEVELOPED 2/17/2019 IN QBASIC BY RON77 AND MISHKA AND ITAY :)'screen half with & heightconst SHW = 640 \ 2const SHH = 480 \ 2 const pi = 3.14159265const RAD_PER_DEG = 2 * pi / 360Sub clock   DO      '      hour = VAL(LEFT\$(TIME\$, 2))      minute = VAL(MID\$(TIME\$, 4, 2))      sec = VAL(RIGHT\$(TIME\$, 2))      hour = hour + minute / 60 + sec / 3600      minute = minute + sec / 60      ts = 90 - sec * 6      tm = 90 - minute * 6      th = 90 - hour * 30      CLS      PRINT TIME\$      PRINT "press ESC to exit  "      CIRCLE (SHW, SHH), 200      CIRCLE (SHW, SHH), 215      'draw minute marks      FOR j = 0 TO 59         cosangle = COS((90 - j * 6) * RAD_PER_DEG)         sinangle = SIN((90 - j * 6) * RAD_PER_DEG)         LINE (SHW + 190 * cosangle, SHH - 190 * sinangle)-(SHW + 200 * cosangle, SHH - 200 * sinangle), 10      NEXT      'draw hour marks      FOR i = 0 TO 11         cosangle = COS((90 - i * 30) * RAD_PER_DEG)         sinangle = SIN((90 - i * 30) * RAD_PER_DEG)         LINE (SHW + 205 * cosangle, SHH - 205 * sinangle)-(SHW + 210 * cosangle, SHH - 210 * sinangle), 12      NEXT      'draw pointers      LINE (SHW, SHH)-(SHW + 200 * COS(ts * RAD_PER_DEG), (SHH - 200 * SIN(ts * RAD_PER_DEG)))      LINE (SHW, SHH)-(SHW + 180 * COS(tm * RAD_PER_DEG), (SHH - 180 * SIN(tm * RAD_PER_DEG))), 10      LINE (SHW, SHH)-(SHW + 120 * COS(th * RAD_PER_DEG), (SHH - 120 * SIN(th * RAD_PER_DEG))), 12      SLEEP (1)      k\$ = INKEY\$   LOOP UNTIL k\$ = CHR\$(27)END SUBclock`

Not sure if QB knew the CONST keyword.
MrSwiss
Posts: 3445
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: the mathematical analog clock

The current implementation is very inefficient.
A comparison in the Main-Loop whould be much more efficient and, free a lot of CPU time.
Only act, if a change of Time has occured ... otherwise "Sleep":

Code: Select all

`Dim As String  cTimeDo    If Time <> cTime Then        cTime = Time  ' update comparison variable        ...        ...    End If    Sleep(100, 1)Loop Until InKey = Chr(255, 107)  ' click X or press [Alt]+[F4]`
BTW: using "QB" is as outdated as the 20th century.
The same goes for [ESC]-aping (to exit a program).
Try to write new stuff in "FB" dialect, it's far more powerfull ...
Posts: 2000
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: the mathematical analog clock

MrSwiss wrote:The current implementation is very inefficient.
A comparison in the Main-Loop whould be much more efficient and, free a lot of CPU time.
...

Note that sleep in #lang "QB" is in seconds, not in milliseconds.
MrSwiss
Posts: 3445
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: the mathematical analog clock

"QB" is a long time dead, as far as I'm concerned ...
(I'm using "FB" exclusively, since joining here.)