Sha256: 209c4a9f833d0f632654d5c6f447d513a5c72bf7ee0e505c951e9dae9d76ecdf

Contents?: true

Size: 972 Bytes

Versions: 5

Compression:

Stored size: 972 Bytes

Contents

#
# Storing Data as a Struct 
# 
# Move the mouse across the screen to change the position
# of the circles. The positions of the mouse are recorded
# into an array and played back every frame. Between each
# frame, the newest value are added to the end of each array
# and the oldest value is deleted. 
#
 
NUM = 60
 
attr_reader :pos

Vec = Struct.new(:x, :y) # create named Struct Type with getters and setters

def setup  
  size(640, 360)
  @pos = Array.new(NUM, Vec.new(0, 0))  # initialize a array of Struct
  smooth(4)
  no_stroke()
  fill(255, 153) 
end

def draw
  background(51) 
  
  # Cycle through the array, using a different entry on each frame. 
  # Using modulo (%) like this is faster than moving all the values over.
  which = frame_count % NUM
  pos[which] = Vec.new(mouse_x, mouse_y)
  
  (1 .. NUM).each do |i|
    # which + 1 is the smallest (the oldest in the array)
    idx = (which + i) % NUM
    ellipse(pos[idx].x, pos[idx].y, i, i)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-processing-2.6.2 samples/processing_app/basics/objects/struct.rb
ruby-processing-2.6.1 samples/processing_app/basics/objects/struct.rb
ruby-processing-2.6.0 samples/processing_app/basics/objects/struct.rb
ruby-processing-2.5.1 samples/processing_app/basics/objects/struct.rb
ruby-processing-2.5.0 samples/processing_app/basics/objects/struct.rb