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.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-03 14:50
(Received via mailing list)
The three rules of Ruby Quiz:

1.  Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message.

2.  Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3.  Enjoy!

Suggestion:  A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby Talk follow the discussion.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Enrique Meza C

You may have noticed that if you have a Golden Rectangle and you cut off
a
square with side lengths equal to the length shorter rectangle side,
then what
remains is another Golden Rectangle.

This could go on forever. You can just keep cutting off these big
squares and
getting smaller and smaller Golden rectangles.

The idea with the Fibonacci series is to do the same thing in reverse.
So the
quiz:

What you do is start with a square (1 by 1), find the longer side, and
add a
square of that size to the whole thing to form a new rectangle.

So when we start with a 1 by 1 square the longest side is one, so we add
another
square to it.  Now we have a 2 by 1 rectangle

Then the longest side is two, so we connect a 2 by 2 square to our 2 by
1
rectangle to get a 3 by 2 rectangle.  This continues, and the sides of
the
rectangle will always be a successive Fibonacci number.  Eventually the
rectangle will be very close to a Golden Rectangle.

I will do a few steps to let you see it in action:

	###
	# #    1 by 1, so we add 1 by 1 to get...
	###

	######
	# ## #  Now it is 2 by 1, so we add 2 by 2 to get......
	######

	######
	# ## #
	######
	######
	#    #   Now it is 2 by 3, so we add a 3 by 3 to get.......
	#    #
	#    #
	#    #
	######

	###############
	# ## ##       #
	#######       #
	#    ##       #
	#    ##       #  Now it is 3 by 5, so we would add a 5 by 5 square.
	#    ##       #
	#    ##       #
	###############
66e5b65d1fe0183ca04053fda407a185?d=identicon&s=25 Rudolfs Osins (Guest)
on 2006-03-03 15:08
(Received via mailing list)
So the task is visualisation of the Fibonacci series ?
8aab717743d4f58a4453adb8b6855e1e?d=identicon&s=25 Robert Retzbach (Guest)
on 2006-03-03 15:26
(Received via mailing list)
Hi,
the description is really exciting :>
But the main task is for me not clear too.

--verbose please
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-03 15:26
(Received via mailing list)
On Mar 3, 2006, at 8:06 AM, Rudolfs Osins wrote:

> So the task is visualisation of the Fibonacci series ?

Correct.  In block format.

James Edward Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-03 15:35
(Received via mailing list)
On Mar 3, 2006, at 8:25 AM, Robert Retzbach wrote:

> Hi,
> the description is really exciting :>
> But the main task is for me not clear too.

Basically the goal is to visualize the sequence.  Perhaps you want to
take a parameter and show the blocks that many steps in, or maybe you
want to make it more like geek cinema and watch the sequence progress
block by block...

James Edward Gray II
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-03-03 18:53
(Received via mailing list)
On 3/3/06, James Edward Gray II <james@grayproductions.net> wrote:
>
> James Edward Gray II

Sample output for my solution, this may be a little clearer than
James' (still requires monospace font, of course):

$ ruby fibonacci_block.rb 6

###########################
# # #     #               #
#####     #               #
#   #     #               #
#   #     #               #
#   #     #               #
###########               #
#         #               #
#         #               #
#         #               #
#         #               #
#         #               #
#         #               #
#         #               #
#         #               #
#         #               #
###########################

Jacob Fugal
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-03 20:17
(Received via mailing list)
On Mar 3, 2006, at 11:50 AM, Jacob Fugal wrote:

>> block by block...
> #####     #               #
> #         #               #
> #         #               #
> #         #               #
> ###########################

It might be good to note that multiple layouts are possible.  For
example, the above could also spiral as follows:

###########################
#               #         #
#               #         #
#               #         #
#               #         #
#               #         #
#               #         #
#               #         #
#               #         #
#               #         #
#               ###########
#               # # #     #
#               #####     #
#               #   #     #
#               #   #     #
#               #   #     #
###########################

James Edward Gray II
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-03-03 21:21
(Received via mailing list)
On 3/3/06, James Edward Gray II <james@grayproductions.net> wrote:
> #               #         #
> #               #         #
> #               #         #
> #               ###########
> #               # # #     #
> #               #####     #
> #               #   #     #
> #               #   #     #
> #               #   #     #
> ###########################

Good point. I actually like that one better, and have altered my code
to produce it. :)

Jacob Fugal
28ca4d5a4133c7c924a7ba464f5fc127?d=identicon&s=25 Brian Mattern (Guest)
on 2006-03-04 06:13
(Received via mailing list)
Jacob Fugal wrote:

>>#               #         #
>>#               #   #     #
>
Hi, I'm new to the ruby (and the quiz). I did a quick solution that has
a little cleaner output like so:

+---------------+---------+
|               |         |
|               |         |
|               |         |
|               |         |
|               |         |
|               |         |
|               |         |
|               |         |
|               |         |
|               +-+-+-----+
|               | | |     |
|               +-+-+     |
|               |   |     |
|               |   |     |
|               |   |     |
+---------------+---+-----+

i guess the next step is to animate it.
Ec39577f2cb42a95efc3f61b9a6c5c42?d=identicon&s=25 ToRA (Guest)
on 2006-03-05 12:05
(Received via mailing list)
Of course, when rendering large numbers of squares it can be nice to
shrink things a bit:

11:02:56 - tora@Eustacia:~/ruby/fibonacci
>ruby gfr.rb 8 1
+++--+-------+--------------------+
+++  |       |                    |
| |  |       |                    |
+-+--+       |                    |
|    |       |                    |
|    |       |                    |
|    |       |                    |
|    |       |                    |
+----+-------+                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
|            |                    |
+------------+--------------------+

or make them larger...
>ruby gfr.rb 3 3
+--+--+
|  |  |
|  |  |
+--+--+
|     |
|     |
|     |
|     |
|     |
+-----+

(crosses fingers that they render correctly)
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-03-07 20:58
(Received via mailing list)
On 3/3/06, Ruby Quiz <james@grayproductions.net> wrote:
> The idea with the Fibonacci series is to do the same thing in reverse. So the
> quiz:
>
> What you do is start with a square (1 by 1), find the longer side, and add a
> square of that size to the whole thing to form a new rectangle.

I used this quiz as an excuses to get Ncurses-ruby running on my
Windows box.  It was fairly painless, once I found PDCurses.  I used
panel borders to draw the boxes, and animated the thing as it grows.
I also added a bunch of command line options for windows:
-resize [x y] will change the window size to fit a bigger rectangle.
-newwin [x y] will launch a new window, then resize (if you don't want
to mess up your current command window.
I didn't add the equivalent commands for *nix boxen, it's been too
long since i used one to remember the right commands.
The program works under windows and cygwin.  I'd appreciate it if
someone lets me know if it works ok under linux or mac.

-Adam

#----- goldrect.rb
#
#  Submission for Ruby Quiz #69
#  by Adam Shelly
#
require 'ncurses'


class FibBox
  Direction=[:up,:left,:down,:right]
  @@boxes = []

  def self.moveall y,x
    @@boxes.each{|f| f.moverel(y,x,true)}
    @@boxes.each{|b| b.unhide}
  end
  def self.animate isize=1
    size = [isize,isize]
    f = FibBox.new(Direction[dir=2],size,[0,0])
    while true
      n = size.max
      pos = [0,0]
      pos[0]+=size.min if dir==2
      pos[1]+=size.min if dir==3
      n==size[1] ? size[0]+=n : size[1]+=n
      break if (size[0] > Ncurses.LINES) || (size[1] > Ncurses.COLS)
      f = FibBox.new(Direction[dir],[n,n],pos)
      dir=(dir+1)%4
    end
  end

  def initialize dir,size,pos
    @size = size
    @pos = pos
    func = lambda{FibBox.moveall(0,1)} if dir==:left
    func = lambda{FibBox.moveall(1,0)} if dir==:up
    case dir
      when :left, :right
        target = @size[1]
        parms=[@size[0],1]+@pos
        animate=1
      when :down, :up
        target = @size[0]
        parms=[1,@size[1]]+@pos
        animate=0
    end
    func.call if func
    win = Ncurses::WINDOW.new(*parms)
    @panel=win.new_panel
    show
    while parms[animate]!=target
      sleep(0.01)
      parms[animate]+=1
      func.call if func
      Ncurses::Panel.update_panels
      Ncurses.doupdate()
      resize(*parms)
    end
    @@boxes<<self
  end

  def hide
      @panel.window.border(*([' '[0]]*8))
  end
  def unhide
      @panel.window.border(*([0]*8))
  end
  def show
      unhide
      Ncurses::Panel.update_panels
      Ncurses.doupdate()
      sleep(0.1/@size[0]) #sleep less when box takes longer to draw
  end
  def resize(sy,sx,ly,lx)
    nw=Ncurses::WINDOW.new(sy,sx,ly,lx)
    exit(0) if !nw
    w = @panel.window
    @panel.replace(nw)
    w.delete
    show
  end
  def moverel(dy,dx,keephidden=false)
    hide
    @pos[0]+=dy
    @pos[1]+=dx
    @panel.move(*@pos)
    show unless keephidden
  end
end


if __FILE__ == $0
  if (ARGV[0].to_i)>0
    size = ARGV.shift.to_i
  end
  if RUBY_PLATFORM =~ /mswin/
    case ARGV[0]
      when '-h', '-?'
        puts "usage: #{$0} [size] [-newwin||-resize] [x y]"
        puts " size: initial box size (default 1)"
        puts " -resize: resizes your screen to x,y (default 150x90)"
        puts " -newwin: launches program in a new resized window"
        exit
      when '-newwin'
      `start /WAIT ruby #{$0} #{size||1} -resize #{ARGV[1]} #{ARGV[2]}`
       exit
      when '-resize'
        `mode con cols=#{ARGV[1]||150} lines=#{ARGV[2]||90}`
    end
  end
  Ncurses.initscr
  Ncurses.noecho
  FibBox.animate size||1
  Ncurses.stdscr.getch
end
F1ae87d23c050dbad815489dbde2f226?d=identicon&s=25 G.Durga Prasad (Guest)
on 2006-03-08 17:36
(Received via mailing list)
Under linux, I have installed ncurses-ruby-1.0, just now and tried
this program.
The rectangles are drawn fairly satisfactorily , but I loose control
on the xterminal, and the bash prompt ovewrites on the rectangles. I
am forced to close the xterminal. I have to still understand the
program.
Prasad
633ddbbe5c812fd47d4d46f2009ad898?d=identicon&s=25 Enrique Meza C (Guest)
on 2006-03-08 20:07
(Received via mailing list)
El jue, 09-03-2006 a las 01:35 +0900, G.Durga Prasad escribió:
> Under linux, I have installed ncurses-ruby-1.0, just now and tried
> this program.
> The rectangles are drawn fairly satisfactorily , but I loose control
> on the xterminal, and the bash prompt ovewrites on the rectangles. I
> am forced to close the xterminal. I have to still understand the
> program.
> Prasad

Use  '#tset -s' to reset the xterminal.

I tried it and works fine.
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2006-03-08 21:05
(Received via mailing list)
On 3/8/06, Enrique Meza C <emeza@cdi.gob.mx> wrote:
> El jue, 09-03-2006 a las 01:35 +0900, G.Durga Prasad escribió:
> > Under linux, I have installed ncurses-ruby-1.0, just now and tried
> > this program.
> > The rectangles are drawn fairly satisfactorily ,

I forgot to mention that it looks best if you can set your terminal to
a square font, like Terminal 8x8 under Windows.

>> but I loose control
> > on the xterminal, and the bash prompt ovewrites on the rectangles. I
> > am forced to close the xterminal. I have to still understand the
> > program.
> > Prasad
>
> Use  '#tset -s' to reset the xterminal.

When should that be issued?  Should the ruby script call it before it
exits?

> I tried it and works fine.
>
Cool.  Thanks for checking.
This topic is locked and can not be replied to.