Sha256: 67afa3c10b6e9e13f607b9314f7b8e27c3b70e4122624f48a43e2a1f6491876c

Contents?: true

Size: 812 Bytes

Versions: 9

Compression:

Stored size: 812 Bytes

Contents

#
# Brownian motion. 
# 
# Recording random movement as a continuous line. 
#

NUM = 2000
RANGE = 6

attr_accessor :ax, :ay 

def setup   
  size(640, 360)
  @ax = Array.new(NUM, width/2)
  @ay = Array.new(NUM, height/2)
  frame_rate(30)
end

def draw   
  background(51)  
  # Shift all elements 1 place to the left
  (1 ... NUM).each do |i|
    ax[i-1] = ax[i]
    ay[i-1] = ay[i]
  end  
  # Put a new value at the end of the array
  ax[NUM-1] += rand(-RANGE .. RANGE)
  ay[NUM-1] += rand(-RANGE .. RANGE)
  
  # Constrain all points to the screen
  ax[NUM-1] = constrain(ax[NUM-1], 0, width)
  ay[NUM-1] = constrain(ay[NUM-1], 0, height)
  
  # Draw a line connecting the points
  (1 ... NUM).each do |i|
    val = i.to_f / NUM * 204.0 + 51
    stroke(val)
    line(ax[i-1], ay[i-1], ax[i], ay[i])
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-processing-2.6.2 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.6.1 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.6.0 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.5.1 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.5.0 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.4.4 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.4.3 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.4.2 samples/processing_app/topics/motion/brownian.rb
ruby-processing-2.4.1 samples/processing_app/topics/motion/brownian.rb