Class: Mir::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/mir/01_model.rb

Direct Known Subclasses

Image

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Grid

Returns a new instance of Grid.



49
50
51
52
# File 'lib/mir/01_model.rb', line 49

def initialize width,height
  @size=Size.new(width,height)
  @values=Array.new(height){Array.new(width)}
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



48
49
50
# File 'lib/mir/01_model.rb', line 48

def size
  @size
end

Class Method Details

.from_a(ary1d, sx, sy) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/mir/01_model.rb', line 88

def self.from_a ary1d,sx,sy
  ret=self.new(sx,sy)
  sy.times do |y|
    sx.times do |x|
      index=y*(sx)+x
      ret[y,x]=ary1d[index]
    end
  end
  ret
end

.random(sx, sy, klass) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/mir/01_model.rb', line 99

def self.random sx,sy,klass
  ret=self.new(sx,sy)
  sy.times do |y|
    sx.times do |x|
      ret[y,x]=klass.random
    end
  end
  ret
end

Instance Method Details

#[](y, x) ⇒ Object



54
55
56
# File 'lib/mir/01_model.rb', line 54

def [](y,x)
  @values[y][x]
end

#[]=(y, x, v) ⇒ Object



58
59
60
# File 'lib/mir/01_model.rb', line 58

def []=(y,x,v)
  @values[y][x]=v
end

#each(&block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/mir/01_model.rb', line 66

def each &block
  @values.each do |row|
    row.each do |v|
      yield v
    end
  end
end

#fill_with(v) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mir/01_model.rb', line 74

def fill_with v
  @values.each_with_index do |row,y|
    row.each_with_index do |pix,x|
      self[y,x]=v
    end
  end
end


82
83
84
85
86
# File 'lib/mir/01_model.rb', line 82

def print
  @values.each do |row|
    puts row.map{|pix| pix.to_s.rjust(3)}.join(" ")
  end
end

#to_aObject



62
63
64
# File 'lib/mir/01_model.rb', line 62

def to_a
  @values.flatten
end