Sha256: f78f79353e6af89cceb137a5c105dcb0aa93d54dad320b551322c3b021af3430

Contents?: true

Size: 947 Bytes

Versions: 9

Compression:

Stored size: 947 Bytes

Contents

require 'ruby-processing'

#  * 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.

class Array2d < Processing::App

  def setup
    
    distances = Array.new( width ) { Array.new( height ) } # [width][height]
	
    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
  
end

Array2d.new :title => "Array 2d", :width => 200, :height => 200

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-processing-1.0.11 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.10.1 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.9 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.4 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.3 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.5 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.6 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.7 samples/processing_app/basics/arrays/array_2d.rb
ruby-processing-1.0.8 samples/processing_app/basics/arrays/array_2d.rb