NB. Script for Fibonacci and Golden Spirals
NB. by Cliff Reiter, August 2013

pos_fib_seq=:3 : '(,[:+/ _2&{.)^:(y-2) 1 1'

pos_fib_seq 8

]fs_sq1=:#:0 2 3 1

load '~addons/graphics/fvj3/dwin2.ijs'

load '~addons/media/image3/image3.ijs'

draw_fs_sqs=:3 : 0
fibs=.pos_fib_seq y
fs_wsen=: 0 0 0 1
fs_sqs=: i. 0 4 2
fs_arcs=: i.0 8
for_J. i. y do.
  F_j=:J{fibs
  select. 4|J
    case. 0 do. NB. add onto west
    _1 0 0 0 (0 1 nx_fs_sq 0 1) 2 0
    case. 1 do. NB. add onto  south
    0 _1 0 0 (0 1 nx_fs_sq 0 0) 3 1
    case. 2 do. NB. add onto  east
    0 0 1 0 (2 3 nx_fs_sq 1 0) 0 2
    case. 3 do. NB. add onto  north
    0 0 0 1 (2 3 nx_fs_sq 1 1) 1 3
  end.
end.
range=.(2 3{fs_wsen)-0 1{fs_wsen
WIN_WH=:range*<.<./0.8*(_2{.".wd'qm')%range
fs_wsen dwin 'Fibonacci Spiral'
(Hue *:(i.%])#fs_sqs) dpoly fs_sqs
)

NB. x is direction fs_wsen changes
NB. m is indices of the corner of the new square
NB. n is used to pick the lower left corner of the square bounding the arc
NB. y is the position in the new square that gives the beginning and end of the arc
nx_fs_sq=:2 : 0
:
fs_wsen=:fs_wsen+x*F_j
nsq=.((m{fs_wsen)-F_j*m-:2 3)+"1 fs_sq1*F_j
fs_sqs=:fs_sqs,nsq
fs_arcs=: fs_arcs,((0{nsq)-n*F_j),(2#2*F_j),,y{nsq
)

draw_fs_sqs 8

fs_wsen

<"2 fs_sqs

darc=:3 : 0"1
(0 0 0;1 0) darc y
:
wd 'psel ',WIN_nam
glrgb >{.x
glpen >{:x
'a b c d'=.4 2$y
'A B C D'=.SC a,(a+b),c,:d
glarc x:^:_1 A,(B-A),C,D
glpaint''
)

draw_fs_sqs 8

(0 0 0;3 0) darc fs_arcs

draw_fs_sqs 8

(0 0 0;3 0) darc 0 1 2 3 6 7 4 5{"1 fs_arcs

draw_golden_spiral=:3 : 0
(0 0 0;1 0)draw_golden_spiral y
:
wd 'psel ',WIN_nam
glrgb >{.x
glpen >{:x
gr=.-:>:%:5
]b=.(^. gr)%1r2p1
]r=. ^@:(b&*)
X=.r * cos
Y=.r * sin
]a=. %2*gr
z=. a *(X,.Y) y *(i.%])1000
gllines ,x:^:_1 SC 2{."1 z
glpaint''
)

draw_fs_sqs 8

(0 0 0;3 0) darc fs_arcs

(255 255 255;2 0) draw_golden_spiral 5p1

draw_fs_sqs 12

(0 0 0;3 0) darc fs_arcs

(255 255 255;2 0) draw_golden_spiral 7p1