Author Topic: Collision Study #3 Brownian Motion  (Read 2642 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Collision Study #3 Brownian Motion
« on: March 31, 2018, 11:11:57 am »
I doubt Einstein will approve of my version of Brownian Motion but I am reading Walter Isaacson's book, Einstein: His Life and Universe, and want to get into experimental mode of thinking. I am also playing Mozart music. (He should approve of my attempts at least (and the music of course).)
Code: QB64: [Select]
  1. _TITLE "Collision Study #3 Brownian Motion 2018-03-31 by bplus,    press spacebar to toggle tracer"
  2. ' attempt to fix
  3. CONST xmax = 800
  4. CONST ymax = 600
  5.  
  6. SCREEN _NEWIMAGE(xmax, ymax, 32)
  7. _SCREENMOVE 360, 60
  8. clrMode = 1
  9. balls = 500
  10. DIM x(balls), y(balls), r(balls), c(balls), dx(balls), dy(balls), a(balls), rr(balls), gg(balls), bb(balls)
  11. FOR i = 1 TO balls
  12.     r(i) = rand(15, 20)
  13.     x(i) = rand(r(i), xmax - r(i))
  14.     y(i) = rand(r(i), ymax - r(i))
  15.     c(i) = rand(1, 15)
  16.     dx(i) = rand(1, 5) * rdir
  17.     dy(i) = rand(1, 5) * rdir
  18.     IF RND < .01 THEN
  19.         rr(i) = rand(200, 255)
  20.         gg(i) = rand(200, 255)
  21.         bb(i) = rand(200, 255)
  22.     ELSE
  23.         rr(i) = 0
  24.         gg(i) = 0
  25.         bb(i) = 0
  26.     END IF
  27.     k$ = INKEY$
  28.     IF LEN(k$) THEN
  29.         IF ASC(k$) = 32 THEN clrMode = -1 * clrMode
  30.         IF ASC(k$) = 27 AND LEN(k$) = 1 THEN END
  31.     END IF
  32.     IF clrMode < 0 THEN CLS
  33.  
  34.     FOR i = 1 TO balls
  35.         'ready for collision
  36.         a(i) = _ATAN2(dy(i), dx(i))
  37.         power1 = (dx(i) ^ 2 + dy(i) ^ 2) ^ .5
  38.         imoved = 0
  39.         FOR j = i + 1 TO balls
  40.             IF SQR((x(i) - x(j)) ^ 2 + (y(i) - y(j)) ^ 2) < r(i) + r(j) THEN
  41.                 imoved = 1
  42.                 a(i) = _ATAN2(y(i) - y(j), x(i) - x(j))
  43.                 a(j) = _ATAN2(y(j) - y(i), x(j) - x(i))
  44.                 'update new dx, dy for i and j balls
  45.                 power2 = (dx(j) ^ 2 + dy(j) ^ 2) ^ .5
  46.                 power = (power1 + power2) / 2
  47.                 dx(i) = power * COS(a(i))
  48.                 dy(i) = power * SIN(a(i))
  49.                 dx(j) = power * COS(a(j))
  50.                 dy(j) = power * SIN(a(j))
  51.                 x(i) = x(i) + dx(i)
  52.                 y(i) = y(i) + dy(i)
  53.                 x(j) = x(j) + dx(j)
  54.                 y(j) = y(j) + dy(j)
  55.                 EXIT FOR
  56.             END IF
  57.         NEXT
  58.         IF imoved = 0 THEN
  59.             x(i) = x(i) + dx(i)
  60.             y(i) = y(i) + dy(i)
  61.         END IF
  62.         IF x(i) < r(i) THEN dx(i) = -dx(i): x(i) = r(i)
  63.         IF x(i) > xmax - r(i) THEN dx(i) = -dx(i): x(i) = xmax - r(i)
  64.         IF y(i) < r(i) THEN dy(i) = -dy(i): y(i) = r(i)
  65.         IF y(i) > ymax - r(i) THEN dy(i) = -dy(i): y(i) = ymax - r(i)
  66.         IF rr(i) <> 0 THEN
  67.             FOR rad = r(i) TO 1 STEP -1
  68.                 COLOR _RGB32(rr(i) - 10 * rad, gg(i) - 10 * rad, bb(i) - 10 * rad)
  69.                 fcirc x(i), y(i), rad
  70.             NEXT
  71.         END IF
  72.     NEXT
  73.     _DISPLAY
  74.     _LIMIT 60
  75.  
  76. FUNCTION rand (lo, hi)
  77.     rand = (RND * (hi - lo + 1)) \ 1 + lo
  78.  
  79. FUNCTION rdir ()
  80.     IF RND < .5 THEN rdir = -1 ELSE rdir = 1
  81.  
  82. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  83. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  84.     DIM subRadius AS LONG, RadiusError AS LONG
  85.     DIM X AS LONG, Y AS LONG
  86.  
  87.     subRadius = ABS(R)
  88.     RadiusError = -subRadius
  89.     X = subRadius
  90.     Y = 0
  91.  
  92.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  93.  
  94.     ' Draw the middle span here so we don't draw it twice in the main loop,
  95.     ' which would be a problem with blending turned on.
  96.     LINE (CX - X, CY)-(CX + X, CY), , BF
  97.  
  98.     WHILE X > Y
  99.         RadiusError = RadiusError + Y * 2 + 1
  100.         IF RadiusError >= 0 THEN
  101.             IF X <> Y + 1 THEN
  102.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  103.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  104.             END IF
  105.             X = X - 1
  106.             RadiusError = RadiusError - X * 2
  107.         END IF
  108.         Y = Y + 1
  109.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  110.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  111.     WEND
  112.  
  113.  

The motion along the edges has to be ignored of course, had to keep 2D molecules bounded in 2D container.
Collision Study #3 Brownian Motion.PNG
* Collision Study #3 Brownian Motion.PNG (Filesize: 104.8 KB, Dimensions: 801x626, Views: 389)