Sha256: c2c129f24b595303445d64f93420ad5c2de8bd4606b959ede146536378a5d50d

Contents?: true

Size: 826 Bytes

Versions: 10

Compression:

Stored size: 826 Bytes

Contents

#  * Demonstrates the syntax for creating a two-dimensional (2D) array.
#  * Values in a 2D array are accessed through two index values.  
#  * 2D arrays are useful for storing images. In this example, each dot 
#  * is colored in relation to its distance from the center of the image.

def setup
  size 640, 360
  distances = Array.new( width ) { Array.new( height ) } # [width][height]
	stroke_weight 2
  max_distance = dist( width/2, height/2, width, height )
  
  width.times do |x|
    height.times do |y|
      distance = dist( width/2, height/2, x, y )
      distances[x][y] = distance / max_distance * 255
    end
  end
  
  background 0        
  x = 0
  while x < distances.length
    y = 0 
    while y < distances[x].length
      stroke distances[x][y]
      point x, y
      y += 2
    end
    x += 2
  end
end


Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ruby-processing-2.6.3 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.6.2 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.6.1 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.6.0 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.5.1 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.5.0 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.4.4 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.4.3 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.4.2 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-2.4.1 samples/processing_app/basics/arrays/array_2d.rb