class Array

see en.wikipedia.org/wiki/Examples_of_groups#The_symmetry_group_of_a_square:_dihedral_group_of_order_8

Public Class Methods

coord_square(n) click to toggle source

Return a square array in which each entry is its own coordinates, i.e. [rol, col]

# File lib/array-symmetries.rb, line 36
def self.coord_square(n)
  return ArgumentError unless n.is_a?(Integer) && n > 0
  Array.new(n) do |i|
    Array.new(n) do |j|
      [i, j]
    end
  end
end
patterns(n) click to toggle source

Return the array of the eight symmetries of the coordinates square

# File lib/array-symmetries.rb, line 46
def self.patterns(n)
  coord_square(n).symmetries
end

Public Instance Methods

a() click to toggle source

Returns the array obtained by rotating the square 90° clockwise.

# File lib/array-symmetries.rb, line 18
def a
  raise ArgumentError unless square?
  b.transpose
end
b() click to toggle source

Returns the array obtained by flipping the square array vertically.

# File lib/array-symmetries.rb, line 12
def b
  raise ArgumentError unless square?
  reverse
end
corners() click to toggle source

Returns a 2x2 array consisting of the four corners of the original square.

# File lib/array-symmetries.rb, line 30
def corners
  raise ArgumentError unless square?
  [[first.first, first.last], [last.first, last.last]]
end
square?() click to toggle source

Returns `true` if the array is square, which is to say, the length of each row is equal to the number of rows. Otherwise returns `false`.

# File lib/array-symmetries.rb, line 6
def square?
  all? {|e| e.length == length}
end
symmetries() click to toggle source

Returns an array containing the eight symmetries of the square array.

# File lib/array-symmetries.rb, line 24
def symmetries
  raise ArgumentError unless square?
  [self, b, a, a.a, a.a.a, a.b, a.a.b, a.a.a.b]
end