Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Brownian-tree/Nim/brownian-tree.nim
Normal file
43
Task/Brownian-tree/Nim/brownian-tree.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import random
|
||||
import imageman
|
||||
|
||||
const
|
||||
Size = 400 # Area size.
|
||||
MaxXY = Size - 1 # Maximum possible value for x and y.
|
||||
NPart = 25_000 # Number of particles.
|
||||
Background = ColorRGBU [byte 0, 0, 0] # Background color.
|
||||
Foreground = ColorRGBU [byte 50, 150, 255] # Foreground color.
|
||||
|
||||
randomize()
|
||||
var image = initImage[ColorRGBU](Size, Size)
|
||||
image.fill(Background)
|
||||
image[Size div 2, Size div 2] = Foreground
|
||||
|
||||
for _ in 1..NPart:
|
||||
|
||||
block ProcessParticle:
|
||||
while true: # Repeat until the particle is freezed.
|
||||
|
||||
# Choose position of particle.
|
||||
var x, y = rand(MaxXY)
|
||||
if image[x, y] == Foreground:
|
||||
continue # Not free. Try again.
|
||||
|
||||
# Move the particle.
|
||||
while true:
|
||||
|
||||
# Choose a motion.
|
||||
let dx, dy = rand(-1..1)
|
||||
inc x, dx
|
||||
inc y, dy
|
||||
if x notin 0..MaxXY or y notin 0..MaxXY:
|
||||
break # Out of limits. Try again.
|
||||
|
||||
# Valid move.
|
||||
if image[x, y] == Foreground:
|
||||
# Not free. Freeze the particle at its previous position.
|
||||
image[x - dx, y - dy] = Foreground
|
||||
break ProcessParticle # Done. Process next particle.
|
||||
|
||||
# Save into a PNG file.
|
||||
image.savePNG("brownian.png", compression = 9)
|
||||
Loading…
Add table
Add a link
Reference in a new issue