Class: RubyCraft::Matrix3d

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubycraft/matrix3d.rb

Instance Method Summary collapse

Constructor Details

#initialize(d1, d2, d3) ⇒ Matrix3d

Returns a new instance of Matrix3d.



13
14
15
16
17
18
# File 'lib/rubycraft/matrix3d.rb', line 13

def initialize(d1,d2,d3)
  @xlimit = d1
  @ylimit = d2
  @zlimit = d3
  @data = Array.new(d1) { Array.new(d2) { Array.new(d3) } }
end

Instance Method Details

#[](x, y, z) ⇒ Object



20
21
22
# File 'lib/rubycraft/matrix3d.rb', line 20

def [](x, y, z)
  @data[x][y][z]
end

#[]=(x, y, z, value) ⇒ Object



24
25
26
# File 'lib/rubycraft/matrix3d.rb', line 24

def []=(x, y, z, value)
  @data[x][y][z] = value
end

#boundsObject



9
10
11
# File 'lib/rubycraft/matrix3d.rb', line 9

def bounds
  [@xlimit, @ylimit, @zlimit]
end

#each(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rubycraft/matrix3d.rb', line 38

def each(&block)
  for z in @data
    for y in z
      for x in y
        yield x
      end
    end
  end
end

#each_triple_index(&block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rubycraft/matrix3d.rb', line 48

def each_triple_index(&block)
  return enum_for:each_triple_index unless block_given?
  @data.each_with_index do |plane, x|
    plane.each_with_index do |column, y|
      column.each_with_index do |value, z|
        yield value, x ,y ,z
      end
    end
  end
end

#fromArray(ar) ⇒ Object

Actually from any iterable



60
61
62
63
# File 'lib/rubycraft/matrix3d.rb', line 60

def fromArray(ar)
  ar.each_with_index { |obj,i| put i, obj }
  return self
end

#get(index) ⇒ Object



33
34
35
36
# File 'lib/rubycraft/matrix3d.rb', line 33

def get(index)
  ar = indexToArray(index)
  self[*ar]
end

#put(index, value) ⇒ Object



28
29
30
31
# File 'lib/rubycraft/matrix3d.rb', line 28

def put(index, value)
  ar = indexToArray(index)
  self[*ar] = value
end

#to_a(default = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rubycraft/matrix3d.rb', line 66

def to_a(default = nil)
  map do |x|
    if x.nil?
      default
    else
      x
    end
  end
end