Author Topic: Binary (BCD) Clock  (Read 4263 times)

0 Members and 1 Guest are viewing this topic.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Binary (BCD) Clock
« on: September 10, 2018, 06:41:39 pm »
Hi all,
wasn't in the mood to work on my GuiTools project today, instead I created this one, enjoy....

NOTE: On Linux/MacOS you probably need to adjust the _LOADFONT line.

Code: QB64: [Select]
  1. '+---------------+---------------------------------------------------+
  2. '| ###### ###### |     .--. .         .-.                            |
  3. '| ##  ## ##   # |     |   )|        (   ) o                         |
  4. '| ##  ##  ##    |     |--' |--. .-.  `-.  .  .-...--.--. .-.        |
  5. '| ######   ##   |     |  \ |  |(   )(   ) | (   ||  |  |(   )       |
  6. '| ##      ##    |     '   `'  `-`-'  `-'-' `-`-`|'  '  `-`-'`-      |
  7. '| ##     ##   # |                            ._.'                   |
  8. '| ##     ###### |  Sources & Documents placed in the Public Domain. |
  9. '+---------------+---------------------------------------------------+
  10. '|                                                                   |
  11. '| === BinClock.bas ===                                              |
  12. '|                                                                   |
  13. '| == A simple binary (BCD) clock inspired by the alien countdown    |
  14. '| == from the movie "Mission to Mars".                              |
  15. '|                                                                   |
  16. '+-------------------------------------------------------------------+
  17. '| Done by RhoSigma, R.Heyder, provided AS IS, use at your own risk. |
  18. '| Find me in the QB64 Forum or mail to support&rhosigma-cw.net for  |
  19. '| any questions or suggestions. Thanx for your interest in my work. |
  20. '+-------------------------------------------------------------------+
  21.  
  22. 'setup screen
  23. SCREEN _NEWIMAGE(875, 395, 256)
  24. scrFont& = _LOADFONT("C:\Windows\Fonts\timesbd.ttf", 72)
  25. _FONT scrFont&
  26.  
  27. '3D space origin is on these screen coordinates
  28. DIM SHARED cx%: cx% = 30
  29. DIM SHARED cy%: cy% = 250
  30.  
  31. 'init BCD discs
  32. TYPE Disc
  33.     x AS INTEGER
  34.     y AS INTEGER
  35.     z AS INTEGER
  36.     r AS INTEGER
  37.     a AS INTEGER
  38. DIM SHARED Discs(23) AS Disc
  39. InitDiscs
  40. DIM SHARED curState&: curState& = 0
  41. DIM SHARED newState&: newState& = 0
  42.  
  43. 'draw hour/minute/seconds separators
  44. Line3D 175, 0, 0, 175, 440, 0, 2
  45. Line3D 175, 0, 0, 175, 0, -110, 2
  46. Line3D 425, 0, 0, 425, 440, 0, 2
  47. Line3D 425, 0, 0, 425, 0, -110, 2
  48.  
  49. 'main loop
  50.     _LIMIT 1
  51.     FlipDiscs
  52. _FREEFONT scrFont&
  53.  
  54. 'run the clock
  55. SUB FlipDiscs
  56. t$ = TIME$
  57. newState& = (VAL(MID$(t$, 1, 1)) * (2 ^ 20)) + (VAL(MID$(t$, 2, 1)) * (2 ^ 16)) +_
  58.             (VAL(MID$(t$, 4, 1)) * (2 ^ 12)) + (VAL(MID$(t$, 5, 1)) * (2 ^ 8)) +_
  59.             (VAL(MID$(t$, 7, 1)) * (2 ^ 4)) + (VAL(MID$(t$, 8, 1)) * (2 ^ 0))
  60. diff& = curState& XOR newState&
  61. curState& = newState&
  62. FOR rot% = 1 TO 90
  63.     FOR n% = 0 TO 23
  64.         IF (n% MOD 4) = 0 THEN AxisSegments Discs(n%).x
  65.         IF diff& AND (2 ^ n%) THEN
  66.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a, 0
  67.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a + 1, 15
  68.             Discs(n%).a = Discs(n%).a + 1
  69.             IF Discs(n%).a = 180 THEN Discs(n%).a = 0
  70.         ELSE
  71.             Circle3D Discs(n%).x, Discs(n%).y, Discs(n%).z, Discs(n%).r, Discs(n%).a, 15
  72.         END IF
  73.     NEXT n%
  74.     IF rot% = 60 THEN
  75.         COLOR 1
  76.         _PRINTSTRING (50, 280), MID$(t$, 1, 2)
  77.         _PRINTSTRING (300, 280), MID$(t$, 4, 2)
  78.         _PRINTSTRING (550, 280), MID$(t$, 7, 2)
  79.     END IF
  80.     _DISPLAY
  81. NEXT rot%
  82.  
  83. 'setup start values for all discs
  84. SUB InitDiscs
  85. n% = 0
  86. FOR i% = 600 TO 500 STEP -100
  87.     FOR j% = 70 TO 370 STEP 100
  88.         Discs(n%).x = i%
  89.         Discs(n%).y = j%
  90.         Discs(n%).z = 0
  91.         Discs(n%).r = 30
  92.         Discs(n%).a = 0
  93.         n% = n% + 1
  94.     NEXT j%
  95. NEXT i%
  96. FOR i% = 350 TO 250 STEP -100
  97.     FOR j% = 70 TO 370 STEP 100
  98.         Discs(n%).x = i%
  99.         Discs(n%).y = j%
  100.         Discs(n%).z = 0
  101.         Discs(n%).r = 30
  102.         Discs(n%).a = 0
  103.         n% = n% + 1
  104.     NEXT j%
  105. NEXT i%
  106. FOR i% = 100 TO 0 STEP -100
  107.     FOR j% = 70 TO 370 STEP 100
  108.         Discs(n%).x = i%
  109.         Discs(n%).y = j%
  110.         Discs(n%).z = 0
  111.         Discs(n%).r = 30
  112.         Discs(n%).a = 0
  113.         n% = n% + 1
  114.     NEXT j%
  115. NEXT i%
  116.  
  117. 'draw rotation axis segments between discs
  118. SUB AxisSegments (x%)
  119. Line3D x%, 0, 0, x%, 40, 0, 4
  120. Line3D x%, 100, 0, x%, 140, 0, 4
  121. Line3D x%, 200, 0, x%, 240, 0, 4
  122. Line3D x%, 300, 0, x%, 340, 0, 4
  123. Line3D x%, 400, 0, x%, 440, 0, 4
  124.  
  125. SUB Line3D (x1%, y1%, z1%, x2%, y2%, z2%, col%)
  126. 'x1%/y1%/z1% = start, x2%/y2%/z2% = end, col% = color pen
  127. x1# = (x1% + (y1% * 0.5)): z1# = (z1% + (y1% * 0.5))
  128. x2# = (x2% + (y2% * 0.5)): z2# = (z2% + (y2% * 0.5))
  129. LINE (x1# + cx% - 1, -z1# + cy%)-(x2# + cx% - 1, -z2# + cy%), col%
  130. LINE (x1# + cx%, -z1# + cy%)-(x2# + cx%, -z2# + cy%), col%
  131. LINE (x1# + cx% + 1, -z1# + cy%)-(x2# + cx% + 1, -z2# + cy%), col%
  132.  
  133. SUB Circle3D (x%, y%, z%, r%, ba%, col%)
  134. 'x%/y%/z% = center, r% = radius, ba% = B-Axis angle, col% = color pen
  135. mx# = (x% + (y% * 0.5)): mz# = (z% + (y% * 0.5))
  136. zx# = r% * COS(ba% * 0.01745329)
  137. zz# = r% * SIN(ba% * 0.01745329)
  138. FOR cir% = 0 TO 359 STEP 5
  139.     x# = zx# * COS(cir% * 0.01745329)
  140.     y# = r% * SIN(cir% * 0.01745329)
  141.     z# = zz# * COS(cir% * 0.01745329)
  142.     x# = (x# + (y# * 0.5)): z# = (z# + (y# * 0.5))
  143.     LINE (x# + mx# + cx% - 1, -z# + -mz# + cy% - 1)-(x# + mx# + cx% + 1, -z# + -mz# + cy% + 1), col%, BF
  144. NEXT cir%
  145.  
  146.  
« Last Edit: January 27, 2022, 12:08:15 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Binary (BCD) Clock
« Reply #1 on: September 10, 2018, 07:08:16 pm »
Awesome! I remember where that was in the movie too.
In order to understand recursion, one must first understand recursion.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Binary (BCD) Clock
« Reply #2 on: September 11, 2018, 01:47:47 am »
Glad, that you like it.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: Binary (BCD) Clock
« Reply #3 on: September 11, 2018, 02:05:23 am »
The wife saw it running and said, "Hey, isn't that like the clock on the floor from that Mars movie?" LOL
In order to understand recursion, one must first understand recursion.