Author Topic: Even Better Stars  (Read 4440 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Even Better Stars
« on: November 18, 2021, 04:33:17 pm »
Souping up some old SdlBasic code:
Code: QB64: [Select]
  1. _Title "Even Better Stars" 'b+ 2021-11-18 trans of
  2. 'Better Stars.sdlbas (B+=MGA) 2016-05-16
  3. ' odd or even number of point, fat or skinny, better fills
  4.  
  5. Const Pi = _Acos(-1) 'cute way to get pi
  6. 'Print (Pi) 'check pi
  7. 'End
  8. Const Radians = Pi / 180 'to convert an angle measured in degrees to and angle measure in radians, just mutiply by this
  9. Const Xmax = 700
  10. Const Ymax = 700
  11. Const Cx = Xmax / 2
  12. Const Cy = Ymax / 2
  13.  
  14. 'setdisplay(xmax, ymax, 32, 1)
  15. Screen _NewImage(Xmax, Ymax, 32)
  16. _ScreenMove 300, 40
  17. 'setcaption("Better Stars demo")
  18. 'autoback(-2)
  19.  
  20. 'main
  21. Const NS = 100
  22. Dim Shared x(NS), y(NS), dx(NS), dy(NS), ri(NS), ro(NS), p(NS), a(NS), turn(NS), fill(NS), c(NS) As _Unsigned Long
  23. loopcounter = 0
  24. For i = 0 To NS
  25.     NewStar i
  26. While _KeyDown(27) = 0
  27.     Line (0, 0)-(Xmax, Ymax), _RGB32(0, 0, 0, 30), BF
  28.     For i = 0 To NS
  29.         If x(i) > 0 And x(i) < Xmax And y(i) > 0 And y(i) < Ymax Then
  30.             'ink(colr(c(i)))
  31.             Color c(i)
  32.             Star x(i), y(i), ri(i), ro(i), p(i), a(i), fill(i)
  33.             x(i) = x(i) + dx(i)
  34.             y(i) = y(i) + dy(i)
  35.             ri(i) = 1.015 * ri(i)
  36.             ro(i) = 1.015 * ro(i)
  37.             a(i) = a(i) + turn(i)
  38.         Else
  39.             NewStar i
  40.         End If
  41.     Next
  42.     'screenswap
  43.     _Display
  44.     _Limit 100
  45.     'wait(50)
  46.     loopcounter = loopcounter + 1
  47.  
  48.  
  49. Sub NewStar (nxt)
  50.     angle = Rnd * 2 * Pi
  51.     r = Rnd * 6 + 1
  52.     dx(nxt) = r * Cos(angle)
  53.     dy(nxt) = r * Sin(angle)
  54.     r = Rnd * 300
  55.     x(nxt) = Cx + r * dx(nxt)
  56.     y(nxt) = Cy + r * dy(nxt)
  57.     ri(nxt) = Rnd
  58.     ro(nxt) = ri(nxt) + 1 + Rnd
  59.     p(nxt) = 3 + Int(Rnd * 9)
  60.     a(nxt) = Rnd * 2 * Pi
  61.     turn(nxt) = Rnd * 6 - 3
  62.     fill(nxt) = Int(Rnd * 2)
  63.     c(nxt) = rndColor~&
  64.  
  65. Function rndColor~& ()
  66.     rndColor~& = _RGB32(Rnd * 255, Rnd * 255, Rnd * 255)
  67.  
  68. Sub Star (x, y, rInner, rOuter, nPoints, angleOffset, TFfill)
  69.     ' x, y are same as for circle,
  70.     ' rInner is center circle radius
  71.     ' rOuter is the outer most point of star
  72.     ' nPoints is the number of points,
  73.     ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
  74.     ' this is to allow us to spin the polygon of n sides
  75.     ' TFfill filled True or False (1 or 0)
  76.     p_angle = Radians * (360 / nPoints): rad_angle_offset = Radians * angleOffset
  77.     x1 = x + rInner * Cos(rad_angle_offset)
  78.     y1 = y + rInner * Sin(rad_angle_offset)
  79.     For i = 0 To nPoints - 1
  80.         x2 = x + rOuter * Cos(i * p_angle + rad_angle_offset + .5 * p_angle)
  81.         y2 = y + rOuter * Sin(i * p_angle + rad_angle_offset + .5 * p_angle)
  82.         x3 = x + rInner * Cos((i + 1) * p_angle + rad_angle_offset)
  83.         y3 = y + rInner * Sin((i + 1) * p_angle + rad_angle_offset)
  84.         Line (x1, y1)-(x2, y2)
  85.         Line (x2, y2)-(x3, y3)
  86.         x1 = x3: y1 = y3
  87.     Next
  88.     If TFfill Then
  89.         'Circle (x, y), 2, &HFFFFFFFF
  90.         Paint (x, y), _DefaultColor, _DefaultColor
  91.     End If
  92.  
  93.  

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Even Better Stars
« Reply #1 on: November 18, 2021, 05:17:16 pm »
Really nice, @bplus!

- Dav

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Even Better Stars
« Reply #2 on: November 18, 2021, 07:05:37 pm »
It's what Dave would have seen in 2001 if Kubrick wasn't so much into realism - Dave
after a lollipop overdose.   The animation would fit well into The Big Lebowski too.  Super pretty.




It works better if you plug it in.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Even Better Stars
« Reply #3 on: November 18, 2021, 09:35:00 pm »
Totally stellar.... (Boom. Boom)

J
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Even Better Stars
« Reply #4 on: November 23, 2021, 08:13:54 pm »
Try with steering: up, down, left, right arrows

Code: QB64: [Select]
  1. _Title "Even Better Stars 2 Arrow Steering" 'b+ 2021-11-23 try with arrow steering
  2. 'Better Stars.sdlbas (B+=MGA) 2016-05-16
  3. ' odd or even number of point, fat or skinny, better fills
  4.  
  5. Const Pi = _Acos(-1) 'cute way to get pi
  6. 'Print (Pi) 'check pi
  7. 'End
  8. Const Radians = Pi / 180 'to convert an angle measured in degrees to and angle measure in radians, just mutiply by this
  9. Const Xmax = 700
  10. Const Ymax = 700
  11. Const Cx = Xmax / 2
  12. Const Cy = Ymax / 2
  13.  
  14. 'setdisplay(xmax, ymax, 32, 1)
  15. Screen _NewImage(Xmax, Ymax, 32)
  16. _ScreenMove 300, 40
  17. 'setcaption("Better Stars demo")
  18. 'autoback(-2)
  19.  
  20. 'main
  21. Const NS = 100
  22. Dim Shared x(NS), y(NS), dx(NS), dy(NS), ri(NS), ro(NS), p(NS), a(NS), turn(NS), fill(NS), c(NS) As _Unsigned Long
  23. loopcounter = 0
  24. For i = 0 To NS
  25.     NewStar i
  26. While _KeyDown(27) = 0
  27.     If _KeyDown(19200) Then '               turn left
  28.         For i = 0 To NS
  29.             x(i) = x(i) + 2 * ri(i) ^ 2
  30.             dx(i) = dx(i) + 1
  31.         Next
  32.     End If
  33.  
  34.     If _KeyDown(19712) Then '              turn right
  35.         For i = 0 To NS
  36.             x(i) = x(i) - 2 * ri(i) ^ 2
  37.             dx(i) = dx(i) - 1
  38.         Next
  39.     End If
  40.  
  41.     If _KeyDown(18432) Then '              turn up
  42.         For i = 0 To NS
  43.             y(i) = y(i) + 2 * ri(i) ^ 2
  44.             dy(i) = dy(i) + 1
  45.         Next
  46.     End If
  47.     If _KeyDown(20480) Then '               turn down
  48.         For i = 0 To NS
  49.             y(i) = y(i) - 2 * ri(i) ^ 2
  50.             dy(i) = dy(i) - 1
  51.         Next
  52.     End If
  53.  
  54.     Line (0, 0)-(Xmax, Ymax), _RGB32(0, 0, 0, 50), BF
  55.     For i = 0 To NS
  56.         If x(i) > 0 And x(i) < Xmax And y(i) > 0 And y(i) < Ymax Then
  57.             'ink(colr(c(i)))
  58.             Color c(i)
  59.             Star x(i), y(i), ri(i), ro(i), p(i), a(i), fill(i)
  60.             x(i) = x(i) + dx(i)
  61.             y(i) = y(i) + dy(i)
  62.             ri(i) = 1.015 * ri(i)
  63.             ro(i) = 1.015 * ro(i)
  64.             a(i) = a(i) + turn(i)
  65.         Else
  66.             NewStar i
  67.         End If
  68.     Next
  69.     'screenswap
  70.     _Display
  71.     _Limit 100
  72.     'wait(50)
  73.     loopcounter = loopcounter + 1
  74.  
  75.  
  76. Sub NewStar (nxt)
  77.     angle = Rnd * 2 * Pi
  78.     r = Rnd * 6 + 1
  79.     dx(nxt) = r * Cos(angle)
  80.     dy(nxt) = r * Sin(angle)
  81.     r = Rnd * 300
  82.     x(nxt) = Cx + r * dx(nxt)
  83.     y(nxt) = Cy + r * dy(nxt)
  84.     ri(nxt) = Rnd
  85.     ro(nxt) = ri(nxt) + 1 + Rnd
  86.     p(nxt) = 3 + Int(Rnd * 9)
  87.     a(nxt) = Rnd * 2 * Pi
  88.     turn(nxt) = Rnd * 6 - 3
  89.     fill(nxt) = 0 'Int(Rnd * 2)
  90.     c(nxt) = rndColor~&
  91.  
  92. Function rndColor~& ()
  93.     rndColor~& = _RGB32(Rnd * 255, Rnd * 255, Rnd * 255)
  94.  
  95. Sub Star (x, y, rInner, rOuter, nPoints, angleOffset, TFfill)
  96.     ' x, y are same as for circle,
  97.     ' rInner is center circle radius
  98.     ' rOuter is the outer most point of star
  99.     ' nPoints is the number of points,
  100.     ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
  101.     ' this is to allow us to spin the polygon of n sides
  102.     ' TFfill filled True or False (1 or 0)
  103.     p_angle = Radians * (360 / nPoints): rad_angle_offset = Radians * angleOffset
  104.     x1 = x + rInner * Cos(rad_angle_offset)
  105.     y1 = y + rInner * Sin(rad_angle_offset)
  106.     For i = 0 To nPoints - 1
  107.         x2 = x + rOuter * Cos(i * p_angle + rad_angle_offset + .5 * p_angle)
  108.         y2 = y + rOuter * Sin(i * p_angle + rad_angle_offset + .5 * p_angle)
  109.         x3 = x + rInner * Cos((i + 1) * p_angle + rad_angle_offset)
  110.         y3 = y + rInner * Sin((i + 1) * p_angle + rad_angle_offset)
  111.         Line (x1, y1)-(x2, y2)
  112.         Line (x2, y2)-(x3, y3)
  113.         x1 = x3: y1 = y3
  114.     Next
  115.     If TFfill Then
  116.         'Circle (x, y), 2, &HFFFFFFFF
  117.         Paint (x, y), _DefaultColor, _DefaultColor
  118.     End If
  119.  
  120.