Class: Grid
- Inherits:
-
Object
- Object
- Grid
- Defined in:
- lib/gem_with_extension_example/grid.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, a) ⇒ Object
- #clear(value = nil) ⇒ Object
- #each ⇒ Object
- #each_col ⇒ Object
-
#fill_complex(x0, y0, x1, y1) ⇒ Object
Generate a two dimensional grid of float pairs, evenly distributed across width x height samples ranging from x0 to x1 and y0 to y1.
- #fill_test ⇒ Object
-
#initialize(width, height, default_value = nil) ⇒ Grid
constructor
A new instance of Grid.
- #to_s ⇒ Object
Constructor Details
#initialize(width, height, default_value = nil) ⇒ Grid
Returns a new instance of Grid.
6 7 8 9 10 11 |
# File 'lib/gem_with_extension_example/grid.rb', line 6 def initialize(width, height, default_value = nil) @width = width @height = height @default_value = default_value @grid = create(width, height, default_value) end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/gem_with_extension_example/grid.rb', line 4 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/gem_with_extension_example/grid.rb', line 4 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/gem_with_extension_example/grid.rb', line 13 def [](x,y) if (x < 0 or x >= @width) or (y < 0 or y >= @height) throw Error("Grid index out of bounds. [%d][%d] not in range ([0 through %d][0 through %d])" % [x, y, width - 1, height - 1]) end @grid[x][y] end |
#[]=(x, y, a) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/gem_with_extension_example/grid.rb', line 20 def []=(x,y,a) if (x < 0 or x >= @width) or (y < 0 or y >= @height) throw Error("Grid index out of bounds. [%d][%d] not in range ([0 through %d][0 through %d])" % [x, y, width - 1, height - 1]) end @grid[x][y] = a end |
#clear(value = nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/gem_with_extension_example/grid.rb', line 27 def clear(value=nil) (0...@width).each do |xi| (0...@height).each do |yi| @grid[xi][yi] = value end end end |
#each ⇒ Object
45 46 47 48 49 |
# File 'lib/gem_with_extension_example/grid.rb', line 45 def each @grid.each do |c| c.each{|i| yield i} end end |
#each_col ⇒ Object
41 42 43 |
# File 'lib/gem_with_extension_example/grid.rb', line 41 def each_col @grid.each{|c| yield c.dup} end |
#fill_complex(x0, y0, x1, y1) ⇒ Object
Generate a two dimensional grid of float pairs, evenly distributed across width x height samples ranging from x0 to x1 and y0 to y1
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gem_with_extension_example/grid.rb', line 54 def fill_complex(x0, y0, x1, y1) x_range = x1 - x0 y_range = y1 - y0 width_limit = Float(@width - 1) height_limit = Float(@height - 1) (0...@width).each do |xi| x_unit = Float(xi) / width_limit x = x0 + (x_unit * x_range) (0...@height).each do |yi| y_unit = Float(yi) / height_limit y = y0 + (y_unit * y_range) @grid[xi][yi] = Complex(x,y) #@grid[xi][yi] = [x,y] end end end |
#fill_test ⇒ Object
74 75 76 77 78 79 |
# File 'lib/gem_with_extension_example/grid.rb', line 74 def fill_test() t = 0 @grid.each_with_index do |c, x| c.each_with_index{|i, y| @grid[x][y] = t; t += 1} end end |
#to_s ⇒ Object
35 36 37 38 39 |
# File 'lib/gem_with_extension_example/grid.rb', line 35 def to_s s = "" @grid.each{|c| s << c.to_s << "\n"} return s end |