Hi all,
Nothing fancy here either, I just tried to solve this in the simplest way
I could (first). Then I tried 'the Ruby way' (at least how I see it after
only 10 hours of playing with Ruby...).
The output uses a two characters to display one cell of data, that is
"##" not "#". Since a single character is 8x16 bits, two chars together
make a pretty good square, and thus the proportions in the final output
are more close to the reality. The blue rectangle is the current
rectangle, while the white square is the part that gets cut from it.
Needs ANSI capable terminal, don't know if it would work or not on
Windows...
--- cut here ---
cell, blank, clear = "\033[34;1m##", "\033[37;1m##", "\033[30;0m"
next_rect = lambda { |a,b| [[a,b].max, [a,b].min + [a,b].max] }
rect = next_rect
res = [1, 1]
(1..6).each do
p res
side = ''
res[0].times { side = side + cell }
res[1].times { side = side + blank }
res[1].times { puts side }
puts clear
res = rect.call(res[0], res[1])
end
--- cut here ---
the OO solution, using recursion
--- cut here ---
class GoldenRectangles
def initialize
@cell, @blank, @clear = "\033[34;1m##", "\033[37;1m##", "\033[30;0m"
end
def next_rectangle(a, b)
[[a,b].max, [a,b].min + [a,b].max]
end
def show_rectangles(rect, count)
if count > 0
p rect
side = ''
rect[0].times { side = side + @cell }
rect[1].times { side = side + @blank }
rect[1].times { puts side }
puts @clear
rect = next_rectangle(rect[0], rect[1])
show_rectangles(rect, count - 1)
end
end
end
GoldenRectangles.new.show_rectangles([1,1], 5)
--- cut here ---
Have a nice day all,
Alex