Author Topic: Sandpile revisited  (Read 2949 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Sandpile revisited
« on: January 29, 2021, 03:28:57 am »
Code: QB64: [Select]
  1. _TITLE "Sandpiles by Ashish, mod #3 B+ 2021-01-22 more color bigger grain"
  2. ' from "Sandpiles by Ashish mod for symmetry and color by B+ 2019-03-04"
  3. ' mod 3 2021-01-22 bigger grains bigger pallet
  4.  
  5. SCREEN _NEWIMAGE(600, 600, 32)
  6. _SCREENMOVE 300, 100
  7. REDIM grain(150, 150) AS _UNSIGNED LONG
  8. REDIM ng(150, 150) AS _UNSIGNED LONG 'next generation grain
  9. FOR i = 0 TO 8
  10.     pal(i) = _RGB32(i * 31, i * 17, i * 8)
  11. grain(75, 75) = 500000: ng(75, 75) = 500000
  12.     FOR y = 1 TO 149
  13.         FOR x = 1 TO 149
  14.             IF grain(x, y) < 4 THEN c = pal(grain(x, y)) ELSE c = &HFFFF8844
  15.             LINE (x * 4, y * 4)-STEP(4, 4), c, BF
  16.             IF grain(x, y) > 4 THEN 'update next generation array NOT this generation array
  17.                 ng(x - 1, y) = ng(x - 1, y) + 1
  18.                 ng(x + 1, y) = ng(x + 1, y) + 1
  19.                 ng(x, y + 1) = ng(x, y + 1) + 1
  20.                 ng(x, y - 1) = ng(x, y - 1) + 1
  21.                 ng(x, y) = ng(x, y) - 4
  22.             END IF
  23.     NEXT x, y
  24.  
  25.     'copy ng() into grain()  ' AFTER UPDATING ALL OF NG()
  26.     FOR y = 0 TO 150
  27.         FOR x = 0 TO 150
  28.             grain(x, y) = ng(x, y)
  29.         NEXT
  30.     NEXT
  31.     _DISPLAY
  32.     '_LIMIT 60
  33. LOOP WHILE grain(75, 75) > 4
  34.  
  35.  

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Sandpile revisited
« Reply #1 on: January 29, 2021, 04:50:14 am »
I just stared at this for 10 minutes theorizing why the radius stops growing but then looked at the code and saw the 150's. Hehe. Nice.
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Sandpile revisited
« Reply #2 on: January 29, 2021, 08:25:57 am »
And here's the simple and efficient way to copy info from one array to the other for you:

Code: QB64: [Select]
  1. _TITLE "Sandpiles by Ashish, mod #3 B+ 2021-01-22 more color bigger grain"
  2. ' from "Sandpiles by Ashish mod for symmetry and color by B+ 2019-03-04"
  3. ' mod 3 2021-01-22 bigger grains bigger pallet
  4.  
  5. SCREEN _NEWIMAGE(600, 600, 32)
  6. _SCREENMOVE 300, 100
  7. REDIM grain(150, 150) AS _UNSIGNED LONG
  8. REDIM ng(150, 150) AS _UNSIGNED LONG 'next generation grain
  9. DIM m(1) AS _MEM
  10. m(0) = _MEM(ng()): m(1) = _MEM(grain())
  11. FOR i = 0 TO 8
  12.     pal(i) = _RGB32(i * 31, i * 17, i * 8)
  13. grain(75, 75) = 500000: ng(75, 75) = 500000
  14.     FOR y = 1 TO 149
  15.         FOR x = 1 TO 149
  16.             IF grain(x, y) < 4 THEN c = pal(grain(x, y)) ELSE c = &HFFFF8844
  17.             LINE (x * 4, y * 4)-STEP(4, 4), c, BF
  18.             IF grain(x, y) > 4 THEN 'update next generation array NOT this generation array
  19.                 ng(x - 1, y) = ng(x - 1, y) + 1
  20.                 ng(x + 1, y) = ng(x + 1, y) + 1
  21.                 ng(x, y + 1) = ng(x, y + 1) + 1
  22.                 ng(x, y - 1) = ng(x, y - 1) + 1
  23.                 ng(x, y) = ng(x, y) - 4
  24.             END IF
  25.     NEXT x, y
  26.  
  27.     'copy ng() into grain()  ' AFTER UPDATING ALL OF NG()
  28.     _MEMCOPY m(0), m(0).OFFSET, m(0).SIZE TO m(1), m(1).OFFSET
  29.     _DISPLAY
  30.     '_LIMIT 60
  31. LOOP WHILE grain(75, 75) > 4

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Sandpile revisited
« Reply #3 on: January 29, 2021, 02:51:23 pm »
Oh yeah, thanks @SMcNeill  I did have that little trick ready in tool bag, forgot all about it, yeah thanks!

For experiment might try a timing this with and without the memory copy, be interesting to compare.
« Last Edit: January 29, 2021, 02:53:46 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Sandpile revisited
« Reply #4 on: January 29, 2021, 03:21:09 pm »
Oh before I forget, I did try using a bigger palette as you can see in code here:
Code: QB64: [Select]
  1. FOR i = 0 TO 8
  2.     pal(i) = _RGB32(i * 31, i * 17, i * 8)
  3.  


But more colors makes it run longer for each diameter for sand pile to spread out, it's more fun to see designs as pile spreads as fast as possible. I was hoping designs might be more interesting with more colors but I didn't think so but you can try by changing wait... I will rewrite.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Sandpile revisited
« Reply #5 on: January 29, 2021, 04:49:46 pm »
Yeah more colrs slow spred and contrast begins to blend so looks more like sand and less like neat design.

Code: QB64: [Select]
  1. _TITLE "Sandpiles by Ashish, mod #4 B+ 2021-01-22 more color bigger grain"
  2. ' from "Sandpiles by Ashish mod for symmetry and color by B+ 2019-03-04"
  3. ' mod 3 2021-01-22 bigger grains bigger pallet
  4. ' mod 4 2021-01-29 mod to use more than 4 colors maybe a range, just takes 1 number as variable to test different pallet amounts
  5. '        using Steve's Memory method for array copy Thanks for reminder!
  6.  
  7. CONST Ncolrs = 16 'number of shades of brown for sand   <<<  low numbers give high contrast for nice design and faster spred
  8. SCREEN _NEWIMAGE(600, 600, 32)
  9. _SCREENMOVE 300, 100
  10. REDIM grain(150, 150) AS _UNSIGNED LONG
  11. REDIM ng(150, 150) AS _UNSIGNED LONG 'next generation grain
  12. REDIM pal(Ncolrs) AS _UNSIGNED LONG
  13. DIM m(1) AS _MEM
  14. m(0) = _MEM(ng()): m(1) = _MEM(grain())
  15. FOR i = 0 TO Ncolrs
  16.     f = 255 / Ncolrs
  17.     pal(i) = _RGB32(i * f, i * f / 2, i * f / 4)
  18. grain(75, 75) = 500000: ng(75, 75) = 500000
  19.     FOR y = 1 TO 149
  20.         FOR x = 1 TO 149
  21.             IF grain(x, y) <= Ncolrs THEN c = pal(grain(x, y)) ELSE c = &HFFFFAA66 'see update
  22.             LINE (x * 4, y * 4)-STEP(4, 4), c, BF
  23.             IF grain(x, y) > Ncolrs THEN 'update next generation array NOT this generation array
  24.                 ng(x - 1, y) = ng(x - 1, y) + 1
  25.                 ng(x + 1, y) = ng(x + 1, y) + 1
  26.                 ng(x, y + 1) = ng(x, y + 1) + 1
  27.                 ng(x, y - 1) = ng(x, y - 1) + 1
  28.                 ng(x, y) = ng(x, y) - 4
  29.             END IF
  30.     NEXT x, y
  31.  
  32.     'copy ng() into grain()  ' AFTER UPDATING ALL OF NG()
  33.     _MEMCOPY m(0), m(0).OFFSET, m(0).SIZE TO m(1), m(1).OFFSET
  34.     _DISPLAY
  35.     '_LIMIT 60
  36. LOOP WHILE grain(75, 75) > 4
  37.  
  38.  

Update: this works better with a whiter top color, past the Ncolrs amount.
« Last Edit: January 29, 2021, 08:56:06 pm by bplus »