class Grobber::SquareMatrix

Attributes

array[R]

Public Class Methods

new(_array) click to toggle source
# File lib/grobber/square_matrix.rb, line 7
def initialize _array
  check_square(_array)
  @array = _array
end

Public Instance Methods

array_length() click to toggle source
# File lib/grobber/square_matrix.rb, line 22
def array_length
  array.length
end
bottom_add(square_matrix) click to toggle source
# File lib/grobber/square_matrix.rb, line 76
def bottom_add square_matrix
end
check_square(array) click to toggle source
# File lib/grobber/square_matrix.rb, line 83
def check_square array
  length = length_for array
  raise "Non Square Matrix" unless array.length == length ** 2
end
coords_for(index) click to toggle source
# File lib/grobber/square_matrix.rb, line 18
def coords_for index
  [x_coord(index), y_coord(index)]
end
display() click to toggle source
# File lib/grobber/square_matrix.rb, line 93
def display
  length.times do |i|
    puts line(i).to_s
  end
end
each_with_coords() { |e, *coords_for(i)| ... } click to toggle source
# File lib/grobber/square_matrix.rb, line 12
def each_with_coords
  array.each_with_index do |e, i|
    yield e, *coords_for(i)
  end
end
flip() click to toggle source
# File lib/grobber/square_matrix.rb, line 66
def flip
  self.class.new flip_array
end
flip_array() click to toggle source
# File lib/grobber/square_matrix.rb, line 60
def flip_array
  array_length.times.map do |i|
    array[index_for x_coord(i), length - 1 - y_coord(i)]
  end
end
flip_flop() click to toggle source
# File lib/grobber/square_matrix.rb, line 52
def flip_flop
  self.class.new flip_flop_array
end
flip_flop_array() click to toggle source
# File lib/grobber/square_matrix.rb, line 56
def flip_flop_array
  right_add_array(flop) + flip.right_add_array(flip.flop)
end
flop() click to toggle source
# File lib/grobber/square_matrix.rb, line 48
def flop
  self.class.new flop_array
end
flop_array() click to toggle source
# File lib/grobber/square_matrix.rb, line 42
def flop_array
  array_length.times.map do |i|
    array[index_for(length - 1 - x_coord(i), y_coord(i))]
  end
end
index_for(x, y) click to toggle source
# File lib/grobber/square_matrix.rb, line 38
def index_for x, y
  length * y + x
end
length() click to toggle source
# File lib/grobber/square_matrix.rb, line 26
def length
  length_for array
end
length_for(array) click to toggle source
# File lib/grobber/square_matrix.rb, line 79
def length_for array
  (array.length ** 0.5).to_i
end
line(i) click to toggle source
# File lib/grobber/square_matrix.rb, line 88
def line i
  s_index = length * i
  array[s_index..s_index + length - 1]
end
right_add_array(square_matrix) click to toggle source
# File lib/grobber/square_matrix.rb, line 70
def right_add_array square_matrix
  length.times.flat_map do |i|
    line(i) + square_matrix.line(i)
  end
end
x_coord(index) click to toggle source
# File lib/grobber/square_matrix.rb, line 30
def x_coord index
  index % length
end
y_coord(index) click to toggle source
# File lib/grobber/square_matrix.rb, line 34
def y_coord index
  (index / length)
end