Author Topic: Chaotic Scattering: Gaspard-Rice System by _vince  (Read 7450 times)

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 39
    • View Profile
Chaotic Scattering: Gaspard-Rice System by _vince
« on: March 05, 2020, 11:26:24 pm »
Chaotic Scattering: Gaspard-Rice system

Author: @_vince
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=78.0
Version: 2018
Tags: [geometry], [ray reflections]

Description:
https://en.wikipedia.org/wiki/Chaotic_scattering

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


Source Code:
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. xx = sw / 2
  14. yy = sh / 2
  15.  
  16.     DO
  17.         mx = _MOUSEX
  18.         my = _MOUSEY
  19.         mb = _MOUSEBUTTON(1)
  20.  
  21.     LINE (0, 0)-(sw, sh), 0, BF
  22.     FOR b = 0 TO 2 * pi STEP 2 * pi / 3
  23.         CIRCLE (r * COS(b) + sw / 2, r * SIN(b) + sh / 2), rr
  24.     NEXT
  25.  
  26.     IF mb THEN
  27.         f = -1
  28.         DO WHILE mb
  29.             DO
  30.                 mb = _MOUSEBUTTON(1)
  31.             LOOP WHILE _MOUSEINPUT
  32.         LOOP
  33.         FOR b = 0 TO 2 * pi STEP 2 * pi / 3
  34.             x1 = r * COS(b) + sw / 2
  35.             y1 = r * SIN(b) + sh / 2
  36.             IF (mx - x1) ^ 2 + (my - y1) ^ 2 < rr * rr THEN f = 0
  37.         NEXT
  38.         IF f THEN
  39.             xx = mx
  40.             yy = my
  41.             f = -1
  42.         END IF
  43.     END IF
  44.  
  45.     x0 = xx
  46.     y0 = yy
  47.  
  48.     a = _ATAN2(my - yy, mx - xx)
  49.  
  50.     t = 0
  51.     DO
  52.         t = t + 1
  53.         x = t * COS(a) + x0
  54.         y = t * SIN(a) + y0
  55.         IF x < 0 OR x > sw OR y < 0 OR y > sh THEN EXIT DO
  56.         FOR b = 0 TO 2 * pi STEP 2 * pi / 3
  57.             x1 = r * COS(b) + sw / 2
  58.             y1 = r * SIN(b) + sh / 2
  59.             IF (x - x1) ^ 2 + (y - y1) ^ 2 < rr * rr THEN
  60.                 a1 = _ATAN2(y - y1, x - x1)
  61.                 a2 = 2 * a1 - a - pi
  62.  
  63.                 LINE (x0, y0)-(x, y), 14
  64.  
  65.                 x0 = x
  66.                 y0 = y
  67.                 a = a2
  68.                 t = 0
  69.                 EXIT FOR
  70.             END IF
  71.         NEXT
  72.     LOOP
  73.  
  74.     LINE (x0, y0)-(x, y), 14
  75.  
  76.     _DISPLAY
  77.     _LIMIT 50
  78.  

ChaoticScattering.png
* ChaoticScattering.bas (Filesize: 1.81 KB, Downloads: 544)
« Last Edit: September 25, 2021, 05:31:38 am by Junior Librarian »