​​
Hi,

Playing around with spiral generation I came up with this bit of "clunky
code" :

spg=: 4 : 0

NB. x is number of rings excluding center

NB. y is center start value

fill=. (y+1)+i.<:*~dp=.1+dx=.2*x

sp=. (2$dp)$y

NB. right, up, left, down

ruld=.4 2$ 0 1 _1 0 0 _1 1 0

NB. coordinate increments follow a pattern r u l l d d r r r u u u l l l l

fill ((<"1) (x,x) +"1+/\(dx,~2#1+i.dx)#(dx+dp)$ruld) } sp

)

Not quite as elegant as the code from
http://www.jsoftware.com/help/phrases/grids.htm :

WRAP=: wrap : (wrap@]^:[)

wrap=: RL@(,.(next+i.@#))^:4

RL=: |.@|:

next=: >:@(>./)@,

RR=: |:@|.

wrapr=: RR@(,.(next+i.@#))^:4

WRAPR=: wrapr : (wrapr@]^:[)


but faster at the expense of memory :

100 ts '120 WRAPR 1'

0.05675277 2624128

100 ts '120 spg 1'

0.01644345 14819456

100 ts '240 WRAPR 1'

0.65856677 10488448

100 ts '240 spg 1'

0.07111388 59072640



Any suggestions to reduce memory usage or "clunkyness" ?



Thanks
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to