Author Topic: Analog clocks code  (Read 5131 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Analog clocks code
« on: October 02, 2018, 10:04:20 am »
But will the lines fit?
Code: QB64: [Select]
  1. _TITLE "49+ Analog Clocks in 38 Lines of Code for QB64 B+ 2018-10-02"
  2. SCREEN _NEWIMAGE(720, 720, 32)
  3. _SCREENMOVE 300, 10
  4.     CLS
  5.     FOR i = 0 TO 11
  6.         clock 360 + 258 * COS(_PI(2 * i / 12)), 360 + 258 * SIN(_PI(2 * i / 12)), 65
  7.         clock 360 + 149 * COS(_PI(2 * i / 12)), 360 + 149 * SIN(_PI(2 * i / 12)), 37
  8.         clock 360 + 86 * COS(_PI(2 * i / 12)), 360 + 86 * SIN(_PI(2 * i / 12)), 20
  9.         clock 360 + 50 * COS(_PI(2 * i / 12)), 360 + 50 * SIN(_PI(2 * i / 12)), 12
  10.     NEXT
  11.     clock 360, 360, 340
  12.     clock 700, 700, 865
  13.     _DISPLAY
  14.     _LIMIT 2
  15. SUB clock (x, y, r)
  16.     FOR a = 0 TO 359 STEP 6
  17.         IF a MOD 30 = 0 THEN r1 = 1 / 30 * r ELSE r1 = 1 / 75 * r
  18.         CIRCLE (x + r * COS(_D2R(a)), y + r * SIN(_D2R(a))), r1
  19.         PAINT (x + r * COS(_D2R(a)), y + r * SIN(_D2R(a))), _RGB32(100, 100, 100), _RGB32(255, 255, 255)
  20.     NEXT
  21.     IF VAL(LEFT$(TIME$, 2)) + (VAL(MID$(TIME$, 4, 2)) / 60) >= 12 THEN hrs = VAL(LEFT$(TIME$, 2)) + (VAL(MID$(TIME$, 4, 2)) / 60) - 12 ELSE hrs = VAL(LEFT$(TIME$, 2)) + (VAL(MID$(TIME$, 4, 2)) / 60)
  22.     ftri x + 1 / 15 * r * COS(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2) + _PI(1 / 2)), y + 1 / 15 * r * SIN(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2) + _PI(1 / 2)), x + 1 / 15 * r * COS(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2) - _PI(1 / 2)), y + 1 / 15 * r * SIN(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2) - _PI(1 / 2)), x + r * COS(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2)), y + r * SIN(VAL(MID$(TIME$, 4, 2)) * _PI(1 / 30) - _PI(1 / 2)), _RGB32(255, 0, 0)
  23.     ftri x + 1 / 10 * r * COS(hrs * _PI(1 / 6) - _PI(1 / 2) + _PI(1 / 2)), y + 1 / 10 * r * SIN(hrs * _PI(1 / 6) - _PI(1 / 2) + _PI(1 / 2)), x + 1 / 10 * r * COS(hrs * _PI(1 / 6) - _PI(1 / 2) - _PI(1 / 2)), y + 1 / 10 * r * SIN(hrs * _PI(1 / 6) - _PI(1 / 2) - _PI(1 / 2)), x + 2 / 3 * r * COS(hrs * _PI(1 / 6) - _PI(1 / 2)), y + 2 / 3 * r * SIN(hrs * _PI(1 / 6) - _PI(1 / 2)), _RGB32(0, 0, 255)
  24.     LINE (x, y)-(x + r * COS(VAL(RIGHT$(TIME$, 2)) * _PI(1 / 30) - _PI(1 / 2)), y + r * SIN(VAL(RIGHT$(TIME$, 2)) * _PI(1 / 30) - _PI(1 / 2))), _RGB32(255, 255, 0)
  25.     CIRCLE (x, y), 1 / 10 * r, _RGB32(255, 255, 255)
  26.     PAINT (x + 1 / 75 * r, y + 1 / 75 * r), _RGB32(100, 100, 100), _RGB32(255, 255, 255)
  27.     CIRCLE (x, y), 1 / 30 * r, _RGB32(0, 0, 0)
  28. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  29.     a& = _NEWIMAGE(1, 1, 32)
  30.     _DEST a&
  31.     PSET (0, 0), K
  32.     _DEST 0
  33.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  34.     _FREEIMAGE a& '<<< this is important!
  35.  

EDIT: Some paints were part off / part on screen.
« Last Edit: October 02, 2018, 08:57:45 pm by bplus »

Marked as best answer by bplus on October 02, 2018, 12:40:50 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re:Analog clocks code
« Reply #1 on: October 02, 2018, 03:39:22 pm »
Hated triangle arms plus wanted to change colors of face and hands but mainly wanted transparent clocks.

Here is all that:
Code: QB64: [Select]
  1. _TITLE "Clock code for QB64 B+ 2018-10-02"
  2. SCREEN _NEWIMAGE(720, 720, 32)
  3. _SCREENMOVE 300, 10
  4.  
  5. CONST PI = 3.141492653589793
  6. CONST PI2 = 1.570796326794897
  7.  
  8. 'check scaling
  9. _TITLE "Scale Test:  Press m for more, l for less clock size, esc for next test..."
  10. sz = 4
  11. WHILE _KEYDOWN(27) = 0
  12.     K$ = INKEY$
  13.     IF K$ = "m" AND INT(sz * 1.25) < 360 THEN sz = INT(sz * 1.25)
  14.     IF K$ = "l" AND INT(sz / 1.25) > 3 THEN sz = INT(sz / 1.25)
  15.     CLS
  16.     clock 360, 360, sz, _RGB32(0, 0, 80), _RGB32(200, 155, 255), _RGB32(60, 30, 45)
  17.     PRINT sz
  18.     _DISPLAY
  19.     _LIMIT 30
  20.  
  21. 'transparency tests, first some background picture
  22. bgrd& = _NEWIMAGE(720, 720, 32) 'draw background
  23. _DEST bgrd&
  24. FOR I = 1 TO 100 'draw background
  25.     LINE (RND * 720, RND * 720)-STEP(RND * 90, RND * 90), _RGB32(RND * 255, RND * 255, RND * 255), BF
  26. LINE (350, 350)-STEP(100, 50), _RGB32(255, 255, 0), BF
  27.  
  28. _TITLE "Transparency Test: press m for more opaqueness press l for less opaqueness, esc for next test..."
  29. sz = 200
  30. t = 15
  31. WHILE _KEYDOWN(27) = 0
  32.     CLS
  33.     K$ = INKEY$
  34.     IF K$ = "m" AND (t + 10 <= 255) THEN t = t + 10
  35.     IF K$ = "l" AND (t - 10 >= 15) THEN t = t - 10
  36.     _PUTIMAGE , bgrd&
  37.     clock 360, 360, sz, _RGBA32(60, 30, 10, t), _RGBA32(128, 196, 255, t), _RGBA32(60, 30, 10, t)
  38.     COLOR _RGB32(255, 255, 255)
  39.     PRINT t
  40.     _DISPLAY
  41.     _LIMIT 30
  42.  
  43. 'random tests
  44. DIM SHARED nClocks
  45. nClocks = 15
  46. DIM SHARED cx(nClocks), cy(nClocks), cr(nClocks), ccb(nClocks) AS _UNSIGNED LONG, ccf(nClocks) AS _UNSIGNED LONG, cch(nClocks) AS _UNSIGNED LONG
  47.  
  48. ON TIMER(4) newClocks
  49. newClocks
  50. _TITLE "Random clocks test,  press q to quit..."
  51. WHILE K$ <> "q"
  52.     CLS
  53.     K$ = INKEY$
  54.     _PUTIMAGE , bgrd&
  55.     FOR I = 1 TO 15
  56.         clock cx(I), cy(I), cr(I), ccf(I), ccb(I), cch(I)
  57.     NEXT
  58.     _DISPLAY
  59.     _LIMIT 30
  60.  
  61. SUB newClocks
  62.     FOR i = 1 TO nClocks
  63.         cr(i) = RND * 200 + 10
  64.         cx(i) = RND * 720 - .5 * cr(i)
  65.         cy(i) = RND * 720 - .5 * cr(i)
  66.         t = RND * 255
  67.         ccb(i) = _RGBA32(RND * 255, RND * 255, RND * 255, t)
  68.         ccf(i) = _RGBA32(RND * 255, RND * 255, RND * 255, t)
  69.         cch(i) = _RGBA32(RND * 255, RND * 255, RND * 255, t)
  70.     NEXT
  71.  
  72. SUB clock (x, y, rr, fc AS _UNSIGNED LONG, bc AS _UNSIGNED LONG, hc AS _UNSIGNED LONG)
  73.     clockFace x, y, rr, fc, bc
  74.     r = .9 * rr
  75.     hrs = VAL(LEFT$(TIME$, 2)) + (VAL(MID$(TIME$, 4, 2)) / 60)
  76.     IF hrs >= 12 THEN hrs = hrs - 12
  77.     hrs = hrs * _PI(1 / 6) - PI2
  78.     ms = VAL(MID$(TIME$, 4, 2)) * PI / 30 - PI2
  79.     ss = VAL(RIGHT$(TIME$, 2)) * PI / 30 - PI2
  80.     x3 = x + r * COS(ms): y3 = y + r * SIN(ms)
  81.     x4 = x + r / 10 * COS(ms - PI): y4 = y + r / 10 * SIN(ms - PI)
  82.     thic x3, y3, x4, y4, r / 15, hc
  83.     x3 = x + 3 / 4 * r * COS(hrs): y3 = y + 3 / 4 * r * SIN(hrs)
  84.     x4 = x + r / 10 * COS(hrs - PI): y4 = y + r / 10 * SIN(hrs - PI)
  85.     thic x3, y3, x4, y4, r / 15, hc
  86.     LINE (x, y)-(x + r * COS(ss), y + r * SIN(ss)), hc
  87.     fcirc x, y, r / 15, hc
  88.     CIRCLE (x, y), r / 30, bc
  89.  
  90. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  91.     a& = _NEWIMAGE(1, 1, 32)
  92.     _DEST a&
  93.     PSET (0, 0), K
  94.     _DEST 0
  95.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  96.     _FREEIMAGE a&
  97.  
  98. SUB clockFace (x, y, r, fc AS _UNSIGNED LONG, bc AS _UNSIGNED LONG)
  99.     b& = _NEWIMAGE(2 * r + 1, 2 * r + 1, 32)
  100.     _DEST b&
  101.     fcirc r, r, r, bc
  102.     FOR a = 0 TO 359 STEP 6
  103.         IF a MOD 30 = 0 THEN
  104.             r1 = 1 / 30 * r
  105.         ELSE
  106.             IF r / 60 > 1 THEN r1 = r / 60 ELSE r1 = 0
  107.         END IF
  108.         IF r1 THEN fcirc r + .9 * r * COS(_D2R(a)), r + .9 * r * SIN(_D2R(a)), r1, fc
  109.     NEXT
  110.     _PUTIMAGE (x - r, y - r)-(x + r, y + r), b&, 0
  111.     _DEST 0
  112.     _FREEIMAGE b&
  113.  
  114. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  115. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG, K AS _UNSIGNED LONG)
  116.     DIM subRadius AS LONG, RadiusError AS LONG
  117.     DIM X AS LONG, Y AS LONG
  118.  
  119.     subRadius = ABS(R)
  120.     RadiusError = -subRadius
  121.     X = subRadius
  122.     Y = 0
  123.  
  124.     IF subRadius = 0 THEN PSET (CX, CY), K: EXIT SUB
  125.  
  126.     ' Draw the middle span here so we don't draw it twice in the main loop,
  127.     ' which would be a problem with blending turned on.
  128.     LINE (CX - X, CY)-(CX + X, CY), K, BF
  129.  
  130.     WHILE X > Y
  131.         RadiusError = RadiusError + Y * 2 + 1
  132.         IF RadiusError >= 0 THEN
  133.             IF X <> Y + 1 THEN
  134.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), K, BF
  135.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), K, BF
  136.             END IF
  137.             X = X - 1
  138.             RadiusError = RadiusError - X * 2
  139.         END IF
  140.         Y = Y + 1
  141.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), K, BF
  142.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), K, BF
  143.     WEND
  144.  
  145. SUB thic (x1, y1, x2, y2, thick, K AS _UNSIGNED LONG)
  146.     t2 = thick / 2
  147.     IF t2 < 1 THEN t2 = 1
  148.     a = _ATAN2(y2 - y1, x2 - x1)
  149.     x3 = x1 + t2 * COS(a + PI2)
  150.     y3 = y1 + t2 * SIN(a + PI2)
  151.     x4 = x1 + t2 * COS(a - PI2)
  152.     y4 = y1 + t2 * SIN(a - PI2)
  153.     x5 = x2 + t2 * COS(a + PI2)
  154.     y5 = y2 + t2 * SIN(a + PI2)
  155.     x6 = x2 + t2 * COS(a - PI2)
  156.     y6 = y2 + t2 * SIN(a - PI2)
  157.     ftri x6, y6, x4, y4, x3, y3, K
  158.     ftri x3, y3, x5, y5, x6, y6, K
  159.  
  160.  
Random clocks test.PNG
* Random clocks test.PNG (Filesize: 60.29 KB, Dimensions: 722x744, Views: 395)
« Last Edit: October 02, 2018, 09:03:26 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Analog clocks code
« Reply #2 on: October 02, 2018, 08:57:19 pm »
The above code was cleaned up and optimized (same day as original post) also used a ON TIMER for first time for testing Random clocks.
« Last Edit: October 02, 2018, 08:58:25 pm by bplus »