Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created October 30, 2011 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/1326405 to your computer and use it in GitHub Desktop.
Save takehiko/1326405 to your computer and use it in GitHub Desktop.
Four fives: Method A
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
class Rot90a
def initialize
@field_s = <<EOS
1*
**
1*1*1*
******
1*
**
EOS
init_field
end
def start
print_field
# xy_rot90_test
puts
rotate_superpose("1", "2")
rotate_superpose("2", "3")
rotate_superpose("3", "4")
print_field
end
def init_field
@field = []
@width = 0
@height = 0
s = @field_s.split(/\n/)
s.each do |line|
t = line.split(//)
@field << t
@width = [@width, t.length].max
@height += 1
end
@x_center = (@width - 1) * 0.5
@y_center = (@height - 1) * 0.5
end
def print_field
print *(@field.map {|line_a| line_a.join + "\n"})
end
def xy_rot90(x_from, y_from)
x1 = x_from - @x_center
y1 = y_from - @y_center
x2 = y1
y2 = -x1
x_to = (x2 + @x_center).to_i
y_to = (y2 + @y_center).to_i
[x_to, y_to]
end
def rotate_superpose(c_from, c_to)
@height.times do |y|
@width.times do |x|
if @field[y][x] == c_from
x_to, y_to = xy_rot90(x, y)
@field[y_to][x_to] = c_to
end
end
end
end
def xy_rot90_test
@height.times do |y|
@width.times do |x|
x_to, y_to = xy_rot90(x, y)
puts "(#{x},#{y}) => (#{x_to},#{y_to})"
end
end
end
end
if __FILE__ == $0
Rot90a.new.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment