The Golden Fibonacci Ratio (#69)


#1

Simple Tk version.
“Inifinite” steps (rectangles);
press “Space” bar for each step;
automatic increase scroll region.

require ‘tk’

class GoldenRect
include Enumerable
def initialize
@w = @h = 1
end

def each
inc_w = true
loop do
yield [@w, @h]
inc_w ? (@w += @h) : (@h += @w)
inc_w = !inc_w
end
end
end

canvas = TkCanvas.new() {
xscrollbar(sb_x = TkScrollbar.new)
yscrollbar(sb_y = TkScrollbar.new)
Tk.grid( self, sb_y, :sticky=>:news )
Tk.grid( sb_x, ‘x’, :sticky=>:we )
TkGrid.rowconfigure(Tk.root, 0, :weight=>1)
TkGrid.columnconfigure(Tk.root, 0, :weight=>1)
}

thread = Thread.new(canvas) { |c|
mx = my = 5 # margin x, y
sx = sy = 10 # rectangle unit scale
GoldenRect.new.each do |w, h|
c.create(TkcRectangle, mx, my, mx + (w * sx), my + (h * sy))
c.scrollregion([0, 0, (2 * mx) + (w * sx), (2 * my) + (h * sy)])
sleep
end
}

TkRoot.bind(‘space’, proc { thread.wakeup })
Tk.mainloop

END


#2

Displaying rectangles in ASCII seemed a bit 20th century. :wink:

So I decided this was an excellent opportunity to play with the Ruby
PDF library. Code and example output at
http://www.jasani.org/articles/2006/03/06/rubyquiz-the-golden-fibonacci-ratio-69