Forum: Ruby The Golden Fibonacci Ratio (#69)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David T. (Guest)
on 2006-03-05 23:08
(Received via mailing list)
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__
(Guest)
on 2006-03-06 03:45
(Received via mailing list)
Displaying rectangles in ASCII seemed a bit 20th century.  ;-)

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...

- Hitesh
http://www.jasani.org/
This topic is locked and can not be replied to.