QB64.org Forum

Active Forums => Programs => Topic started by: STxAxTIC on March 19, 2021, 11:54:01 am

Title: A do-nothing zen garden
Post by: STxAxTIC on March 19, 2021, 11:54:01 am
Click the mouse to create heat, life, and waves. Right click to destroy.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 800, 32)
  2.  
  3. DIM SHARED GridWidth
  4. DIM SHARED GridHeight
  5. DIM SHARED CellWidth
  6. DIM SHARED CellHeight
  7. GridWidth = _WIDTH / 8
  8. GridHeight = _HEIGHT / 8
  9. CellWidth = _WIDTH / GridWidth
  10. CellHeight = _HEIGHT / GridHeight
  11.  
  12. TYPE Vector
  13.     x AS DOUBLE
  14.     y AS DOUBLE
  15.     z AS DOUBLE
  16.  
  17. DIM SHARED MainGrid(GridWidth, GridHeight) AS Vector
  18. DIM SHARED AuxiGrid(GridWidth, GridHeight) AS Vector
  19. DIM SHARED GridVel(GridWidth, GridHeight) AS DOUBLE
  20.  
  21. DIM SelectedCelli
  22. DIM SelectedCellj
  23.  
  24.         SelectedCelli = INT(_MOUSEX / CellWidth)
  25.         SelectedCellj = INT(_MOUSEY / CellHeight)
  26.         IF _MOUSEBUTTON(1) THEN
  27.             MainGrid(SelectedCelli, SelectedCellj).x = 8
  28.             MainGrid(SelectedCelli, SelectedCellj).y = 1
  29.             MainGrid(SelectedCelli, SelectedCellj).z = 6
  30.             GridVel(SelectedCelli, SelectedCellj) = 0
  31.         END IF
  32.         IF _MOUSEBUTTON(2) THEN
  33.             MainGrid(SelectedCelli, SelectedCellj).x = 0
  34.             MainGrid(SelectedCelli, SelectedCellj).y = 0
  35.             MainGrid(SelectedCelli, SelectedCellj).z = 0
  36.             GridVel(SelectedCelli, SelectedCellj) = 0
  37.         END IF
  38.     LOOP
  39.  
  40.     CALL UpdateGrid
  41.     CLS
  42.     CALL PlotGrid
  43.     _DISPLAY
  44.     _LIMIT 30
  45.  
  46.  
  47. SUB UpdateGrid
  48.     DIM i AS INTEGER
  49.     DIM j AS INTEGER
  50.     DIM t AS DOUBLE
  51.     DIM AS Vector a1, a2, a3, a4, a5, a6, a7, a8, a9
  52.     FOR i = 1 TO GridWidth
  53.         FOR j = 1 TO GridHeight
  54.             AuxiGrid(i, j).x = MainGrid(i, j).x
  55.             AuxiGrid(i, j).y = MainGrid(i, j).y
  56.             AuxiGrid(i, j).z = MainGrid(i, j).z
  57.         NEXT
  58.     NEXT
  59.     FOR i = 1 TO GridWidth - 2
  60.         FOR j = 1 TO GridHeight - 2
  61.             a1 = AuxiGrid(i - 1, j + 1)
  62.             a2 = AuxiGrid(i, j + 1)
  63.             a3 = AuxiGrid(i + 1, j + 1)
  64.             a4 = AuxiGrid(i - 1, j)
  65.             a5 = AuxiGrid(i, j)
  66.             a6 = AuxiGrid(i + 1, j)
  67.             a7 = AuxiGrid(i - 1, j - 1)
  68.             a8 = AuxiGrid(i, j - 1)
  69.             a9 = AuxiGrid(i + 1, j - 1)
  70.  
  71.  
  72.             ' Diffusion
  73.             MainGrid(i, j).x = (1 / 5) * (a2.x + a4.x + a6.x + a8.x + a5.x)
  74.  
  75.             ' Game of life
  76.             t = a1.y + a2.y + a3.y + a4.y + a6.y + a7.y + a8.y + a9.y
  77.             IF (a5.y = 1) THEN
  78.                 SELECT CASE t
  79.                     CASE IS < 2
  80.                         MainGrid(i, j).y = 0
  81.                     CASE 2
  82.                         MainGrid(i, j).y = 1
  83.                     CASE 3
  84.                         MainGrid(i, j).y = 1
  85.                     CASE IS > 3
  86.                         MainGrid(i, j).y = 0
  87.                 END SELECT
  88.             ELSE
  89.                 IF (t = 3) THEN
  90.                     MainGrid(i, j).y = 1
  91.                 END IF
  92.             END IF
  93.  
  94.             ' Wave propagation
  95.             DIM alpha
  96.             DIM wp1, wp2
  97.             alpha = .25
  98.             wp1 = alpha * (a6.z + a4.z) + 2 * (1 - alpha) * a5.z - GridVel(i, j)
  99.             wp2 = alpha * (a2.z + a8.z) + 2 * (1 - alpha) * a5.z - GridVel(i, j)
  100.             MainGrid(i, j).z = (0.98) * (1 / 2) * (wp1 + wp2)
  101.             GridVel(i, j) = AuxiGrid(i, j).z
  102.         NEXT
  103.     NEXT
  104.  
  105. SUB PlotGrid
  106.     DIM i AS INTEGER
  107.     DIM j AS INTEGER
  108.     FOR i = 0 TO GridWidth
  109.         FOR j = 0 TO GridHeight
  110.             LINE (i * CellWidth, j * CellHeight)-(i * CellWidth + CellWidth, j * CellHeight + CellHeight), _RGB32(255 * MainGrid(i, j).x, 25 + 230 * MainGrid(i, j).y, 255 * ABS(MainGrid(i, j).z)), BF
  111.             LINE (i * CellWidth, j * CellHeight)-(i * CellWidth + CellWidth, j * CellHeight + CellHeight), _RGB32(100, 100, 100), B
  112.         NEXT
  113.     NEXT
Title: Re: A do-nothing zen garden
Post by: bplus on March 19, 2021, 12:02:52 pm
Wow, cool!
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: A do-nothing zen garden
Post by: FellippeHeitor on March 19, 2021, 12:51:22 pm
Whoooooooooooooooooooooooooooooooooooooooooooooooa.

🤯

Running this feels... magical.
Title: Re: A do-nothing zen garden
Post by: 191Brian on March 19, 2021, 03:29:03 pm
Wow really cool reminds me of an android app simulating fluid mechanics.
Title: Re: A do-nothing zen garden
Post by: OldMoses on March 20, 2021, 06:38:43 pm
Amazing effect on so few code lines. I particularly liked the wave canceling effect. Very cool.
Title: Re: A do-nothing zen garden
Post by: Aurel on March 21, 2021, 04:15:07 am
Yes it is really cool i like it STATIC....raster is great
maybe is good also for RETRO -GAME or something similar
Title: Re: A do-nothing zen garden
Post by: euklides on March 21, 2021, 05:32:50 am
Nice !
I had a problem with line 57    ( DIM AS Vector a1, a2, a3, a4, a5, a6, a7, a8, a9 )
and replaced it with:
    DIM a1 AS Vector:    DIM a2 AS Vector:    DIM a3 AS Vector
    DIM a4 AS Vector:    DIM a5 AS Vector:    DIM a6 AS Vector
    DIM a7 AS Vector:    DIM a8 AS Vector:    DIM a9 AS Vector

Reason ???  (V1.4 in use)
Title: Re: A do-nothing zen garden
Post by: bplus on March 21, 2021, 11:12:15 am
Nice !
I had a problem with line 57    ( DIM AS Vector a1, a2, a3, a4, a5, a6, a7, a8, a9 )
and replaced it with:
    DIM a1 AS Vector:    DIM a2 AS Vector:    DIM a3 AS Vector
    DIM a4 AS Vector:    DIM a5 AS Vector:    DIM a6 AS Vector
    DIM a7 AS Vector:    DIM a8 AS Vector:    DIM a9 AS Vector

Reason ???  (V1.4 in use)

Yes will work in v1.5 "that alone is worth the price of admission" as they say in USA!
Meaning get v1.5 because you can DIM or ReDim mass amounts of variables with only one "As Type" at the start of the line, eg the line you noted above.
Title: Re: A do-nothing zen garden
Post by: TempodiBasic on April 05, 2021, 08:18:16 pm
@STxAxTIC
always impressive your works!
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Great job bro!