Fibonacci word/fractal: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 14: Line 14:


This probably only works in Unicon. It also defaults to showing the factal for F_word<sub>25</sub> as
This probably only works in Unicon. It also defaults to showing the factal for F_word<sub>25</sub> as
larger fractals quickly exceed the size of window I can display, even with a line segment length of
larger Fibonacci words quickly exceed the size of window I can display, even with a line segment length of
a single pixel.
a single pixel.



Revision as of 15:03, 16 July 2013

Fibonacci word/fractal is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The Fibonacci word may be represented as a fratal as described here:

For F_word37 start with F_wordCharn=1
Draw a segment forward
If current F_wordChar is 0
Turn left if n is even
Turn right if n is odd
next n and iterate until end of F_word

For this task create and display a fractal similar to Fig 1.

Icon and Unicon

This probably only works in Unicon. It also defaults to showing the factal for F_word25 as larger Fibonacci words quickly exceed the size of window I can display, even with a line segment length of a single pixel.

<lang unicon>global width, height

procedure main(A)

   n := integer(A[1]) | 25			# F_word to use
   sl := integer(A[2]) | 1                     # Segment length
   width := integer(A[3]) | 1050               # Width of plot area
   height := integer(A[4]) | 1050              # Height of plot area
   w := fword(n)
   drawFractal(n,w,sl)

end

procedure fword(n)

   static fcache
   initial fcache := table()
   /fcache[n] := case n of {
                    1: "1"
                    2: "0"
                    default: fword(n-1)||fword(n-2)
                    }
   return fcache[n]

end

record loc(x,y)

procedure drawFractal(n,w,sl)

   wparms := ["FibFractal "||n,"g","bg=white","canvas=normal",
              "fg=black","size="||width||","||height,"dx=10","dy=10"]
   &window := open!wparms | stop("Unable to open window")
   p := loc(10,10)
   d := "north"
   every i := 1 to *w do {
      p := draw(p,d,sl)
      if w[i] == "0" then d := turn(d,i%2)
      }

   until Event() == &lpress
   WriteImage("FibFract"||n||".png")
   close(&window)

end

procedure draw(p,d,sl)

   if d == "north"      then p1 := loc(p.x,p.y+sl)
   else if d == "south" then p1 := loc(p.x,p.y-sl)
   else if d == "east"  then p1 := loc(p.x+sl,p.y)
   else                      p1 := loc(p.x-sl,p.y)
   DrawLine(p.x,p.y, p1.x,p1.y)
   return p1

end

procedure turn(d,n)

   return if n = 0 then turnLeft(d) else turnRight(d)

end

procedure turnLeft(d)

   return case d of {
          "north": "west"
          "west":  "south"
          "south": "east"
          "east":  "north"
          }

end

procedure turnRight(d)

   return case d of {
          "north": "east"
          "east":  "south"
          "south": "west"
          "west":  "north"
          }

end</lang>