Author Topic: Chaotic scattering  (Read 8389 times)

0 Members and 1 Guest are viewing this topic.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Chaotic scattering
« on: February 15, 2018, 06:52:14 pm »
https://en.wikipedia.org/wiki/Chaotic_scattering

Demo of the Gaspard-Rice system as described above.  Left-click to change location.

Code: QB64: [Select]
  1. defint a-z
  2. sw = 640
  3. sh = 480
  4.  
  5.  
  6. pi = 3.141593
  7.  
  8. screen _newimage(sw,sh,12)
  9.  
  10. r = 150
  11. rr = 100
  12.  
  13. 'line(0,0)-(sw,sh),0,bf
  14. 'for b=0 to 2*pi step 2*pi/3
  15. '       circle (r*cos(b)+sw/2, r*sin(b)+sh/2),rr
  16. 'next
  17. 'end
  18.  
  19. xx = sw/2
  20. yy = sh/2
  21.  
  22.         do
  23.                 mx=_mousex
  24.                 my=_mousey
  25.                 mb=_mousebutton(1)
  26.         loop while _mouseinput
  27.  
  28.         line(0,0)-(sw,sh),0,bf
  29.         for b=0 to 2*pi step 2*pi/3
  30.                 circle (r*cos(b)+sw/2, r*sin(b)+sh/2),rr
  31.         next
  32.  
  33.  
  34.         if mb then
  35.                 f = -1
  36.                 do while mb
  37.                         do
  38.                                 mb=_mousebutton(1)
  39.                         loop while _mouseinput
  40.                 loop
  41.                 for b=0 to 2*pi step 2*pi/3
  42.                         x1=r*cos(b)+sw/2
  43.                         y1=r*sin(b)+sh/2
  44.                         if (mx-x1)^2+(my-y1)^2 < rr*rr then f = 0
  45.                 next
  46.                 if f then
  47.                         xx = mx
  48.                         yy = my
  49.                         f = -1
  50.                 end if
  51.         end if
  52.  
  53.         x0 = xx
  54.         y0 = yy
  55.  
  56.         a = _atan2(my-yy,mx-xx)
  57.  
  58.         t=0
  59.         do
  60.                 t=t+1
  61.                 x = t*cos(a)+x0
  62.                 y = t*sin(a)+y0
  63.                 if x<0 or x>sw or y<0 or y>sh then exit do
  64.                 for b=0 to 2*pi step 2*pi/3
  65.                         x1=r*cos(b)+sw/2
  66.                         y1=r*sin(b)+sh/2
  67.                         if (x-x1)^2+(y-y1)^2 < rr*rr then
  68.                                 a1 = _atan2(y-y1,x-x1)
  69.                                 a2 = 2*a1-a-pi
  70.  
  71.                                 line(x0, y0)-(x,y),14
  72.  
  73.                                 x0 = x
  74.                                 y0 = y
  75.                                 a = a2
  76.                                 t=0
  77.                                 exit for
  78.                         end if
  79.                 next
  80.         loop
  81.  
  82.         line (x0,y0)-(x,y),14
  83.  
  84.         _display
  85.         _limit 50
  86.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaotic scattering
« Reply #1 on: February 15, 2018, 07:03:36 pm »
This is neat! I just thought of something we might do to make this even more interesting!

FellippeHeitor

  • Guest
Re: Chaotic scattering
« Reply #2 on: February 15, 2018, 08:30:19 pm »
This one always keeps me playing. Great job as always, v.

PS: What about that slow fading of the lasers in the laser show room that was added the other day? Scraped for good?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaotic scattering
« Reply #3 on: February 15, 2018, 09:44:45 pm »
Finally got the cool mod I had in mind working:
Code: QB64: [Select]
  1. _TITLE "*** Chaotic Scattering *** by vince and mod by bplus 2018-02-15                     click mouse to reset LASER"
  2. DEFINT A-Z
  3. CONST sw = 1200
  4. CONST sh = 700
  5.  
  6. qb(0) = &HFF000000
  7. qb(1) = &HFF000088
  8. qb(2) = &HFF008800
  9. qb(3) = &HFF008888
  10. qb(4) = &HFF880000
  11. qb(5) = &HFF880088
  12. qb(6) = &HFF888800
  13. qb(7) = &HFFCCCCCC
  14. qb(8) = &HFF888888
  15. qb(9) = &HFF0000FF
  16. qb(10) = &HFF00FF00
  17. qb(11) = &HFF00FFFF
  18. qb(12) = &HFFFF0000
  19. qb(13) = &HFFFF00FF
  20. qb(14) = &HFFFFFF00
  21. qb(15) = &HFFFFFFFF
  22.  
  23. CONST nCircs = 25
  24. CONST r = 150
  25. CONST maxr = 100
  26. TYPE circles
  27.     x AS INTEGER
  28.     y AS INTEGER
  29.     r AS INTEGER
  30.     c AS _INTEGER64
  31. DIM SHARED cs(nCircs) AS circles
  32. FOR i = 1 TO nCircs
  33.     cs(i).r = RND * (maxr - 20) + 20
  34.     cs(i).c = qb(INT(RND * 15) + 1)
  35.     IF i > 1 THEN
  36.         ck = 0
  37.         WHILE ck = 0
  38.             cs(i).x = INT(RND * (sw - 2 * cs(i).r)) + cs(i).r
  39.             cs(i).y = INT(RND * (sh - 2 * cs(i).r)) + cs(i).r
  40.             ck = 1
  41.             FOR c = 1 TO i - 1
  42.                 IF ((cs(i).x - cs(c).x) ^ 2 + (cs(i).y - cs(c).y) ^ 2) ^ .5 < cs(i).r + cs(c).r THEN ck = 0: EXIT FOR
  43.             NEXT
  44.         WEND
  45.     ELSE
  46.         cs(i).x = INT(RND * (sw - 2 * cs(i).r)) + cs(i).r
  47.         cs(i).y = INT(RND * (sh - 2 * cs(i).r)) + cs(i).r
  48.     END IF
  49.  
  50.  
  51.  
  52.  
  53. SCREEN _NEWIMAGE(sw, sh, 32)
  54. _SCREENMOVE 100, 20
  55.  
  56. 'find a place not inside a circle
  57. xx = sw / 2
  58. yy = sh / 2
  59. WHILE checkxy%(xx, yy) = 0
  60.     xx = INT(RND * (sw - 2 * maxr)) + maxr
  61.     yy = INT(RND * (sh - 2 * maxr)) + maxr
  62.  
  63.     IF LEN(INKEY$) THEN
  64.         _DELAY 5 'to get dang screen shot
  65.     ELSE
  66.         'get mouse x, y if click
  67.         DO
  68.             mx = _MOUSEX
  69.             my = _MOUSEY
  70.             mb = _MOUSEBUTTON(1)
  71.         LOOP WHILE _MOUSEINPUT
  72.     END IF
  73.     'cls
  74.     LINE (0, 0)-(sw, sh), qb(0), BF
  75.     'draw circles
  76.     FOR c = 1 TO nCircs
  77.         COLOR cs(c).c
  78.         fcirc cs(c).x, cs(c).y, cs(c).r
  79.     NEXT
  80.  
  81.     'if click make sure click was not inside one of the circles
  82.     IF mb THEN
  83.         DO WHILE mb
  84.             DO
  85.                 mb = _MOUSEBUTTON(1)
  86.             LOOP WHILE _MOUSEINPUT
  87.         LOOP
  88.         f = checkxy%(mx, my)
  89.         IF f THEN
  90.             xx = mx
  91.             yy = my
  92.             f = -1
  93.         END IF
  94.     END IF
  95.  
  96.     x0 = xx
  97.     y0 = yy
  98.     a = _ATAN2(my - yy, mx - xx)
  99.     t = 0
  100.     DO
  101.         t = t + 1
  102.         x = t * COS(a) + x0
  103.         y = t * SIN(a) + y0
  104.         IF x < 0 OR x > sw OR y < 0 OR y > sh THEN EXIT DO
  105.         FOR c = 1 TO nCircs
  106.             IF (x - cs(c).x) ^ 2 + (y - cs(c).y) ^ 2 < cs(c).r * cs(c).r THEN
  107.                 a1 = _ATAN2(y - cs(c).y, x - cs(c).x)
  108.                 a2 = 2 * a1 - a - _PI
  109.                 LINE (x0, y0)-(x, y), qb(14)
  110.                 x0 = x
  111.                 y0 = y
  112.                 a = a2
  113.                 t = 0
  114.                 EXIT FOR
  115.             END IF
  116.         NEXT
  117.     LOOP
  118.     LINE (x0, y0)-(x, y), qb(14)
  119.     _DISPLAY
  120.     _LIMIT 50
  121.  
  122. FUNCTION checkxy% (x, y)
  123.     DIM c AS INTEGER
  124.     FOR c = 1 TO nCircs
  125.         IF (x - cs(c).x) ^ 2 + (y - cs(c).y) ^ 2 < cs(c).r * cs(c).r THEN checkxy% = 0: EXIT FUNCTION
  126.     NEXT
  127.     checkxy% = 1
  128.  
  129. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  130. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  131.     DIM subRadius AS LONG, RadiusError AS LONG
  132.     DIM X AS LONG, Y AS LONG
  133.  
  134.     subRadius = ABS(R)
  135.     RadiusError = -subRadius
  136.     X = subRadius
  137.     Y = 0
  138.  
  139.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  140.  
  141.     ' Draw the middle span here so we don't draw it twice in the main loop,
  142.     ' which would be a problem with blending turned on.
  143.     LINE (CX - X, CY)-(CX + X, CY), , BF
  144.  
  145.     WHILE X > Y
  146.         RadiusError = RadiusError + Y * 2 + 1
  147.         IF RadiusError >= 0 THEN
  148.             IF X <> Y + 1 THEN
  149.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  150.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  151.             END IF
  152.             X = X - 1
  153.             RadiusError = RadiusError - X * 2
  154.         END IF
  155.         Y = Y + 1
  156.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  157.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  158.     WEND
  159.  
  160.  
  161.  
Choatic scatter.PNG
* Choatic scatter.PNG (Filesize: 38.42 KB, Dimensions: 1272x768, Views: 716)

FellippeHeitor

  • Guest
Re: Chaotic scattering
« Reply #4 on: February 15, 2018, 09:51:23 pm »
Everything looks better with semitransparent cls:

Code: QB64: [Select]
  1.     'cls
  2.     LINE (0, 0)-(sw, sh), _RGBA32(0, 0, 0, 30), BF

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaotic scattering
« Reply #5 on: February 15, 2018, 09:59:31 pm »
Oh yeah! Suddenly the laser light is ... I don't know how to describe it, but it is vastly improved!

So with transparent layers over the beam each frame, you can still see the ones before in the frames before fading as they get covered over more and more. Nice effect!
« Last Edit: February 15, 2018, 10:02:38 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaotic scattering
« Reply #6 on: February 15, 2018, 10:41:39 pm »
Decent screen shots are hard to get (for me anyway) this is OK:
Chaotic Scatter.PNG
* Chaotic Scatter.PNG (Filesize: 91.93 KB, Dimensions: 896x726, Views: 709)

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Chaotic scattering
« Reply #7 on: February 16, 2018, 12:45:52 am »
Wow nice work bplus.  Neat to see that you were able to interpret the code.  @phil yeah I wanted to post the fading one initially but I may have lost it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Chaotic scattering
« Reply #8 on: February 16, 2018, 10:00:18 am »
It is funny, I change the SCREEN _NEWIMAGE color number to 32 for RGB full range which I am most familiar and then realize you, v, were using QB color numbers for the line, daaaaaaaaaaaah! well at least I now have my QB numbers updated in my 000Handy.bas file for code to copy and paste from, not up to library level yet.

https://www.qb64.org/wiki/NEWIMAGE

so the 12 v had was for screen 12 colors

https://www.qb64.org/wiki/SCREEN

16 color, 4 BPP

Sure glad to see Wiki!

Well not a total waste of time to switch to 32, it was easy as pie to implement Fellippe's suggestion for cls with transparencies. I bet that trick will come in handy in future!
« Last Edit: February 16, 2018, 10:21:45 am by bplus »