NB. Generating a Fuzzy Automaton with 
NB. the Symmetry of a snowflake
NB. Angela Coxe and Cliff Reiter
NB. auxiliary to Fuzzy Hexagonal Automata and Snowflakes 
NB. October, 2002
NB. Requires the image3 addon for jsoftware

load 'addons\image3\prevare.ijs'

Pad=: 1 : '([:-$@]++:@[){.!.m.($@]+[){.!.m.]'

rep=:[##"1
hx_sh=:(e.&0 1@(4:|i.@#)|."0 1])@:(2&rep)

lin256=:<.@:(*&255.999)
PBY=:<.(|.0.65*Hue 1r3+1r6*(i.%<:)128),(|.Hue 1r6*(i.%<:)128)

perext2=:(_2&{.,],2&{.)"1@:(_2&{.,],2&{.)
perext2e=:(_2&{.,],2&{.)"1@:({:,],3&{.)

lnbr=:(12 6 7 13 17 16 11 1 2 3 8 14 18 23 22 21 15 10 5)&{@,
rnbr=:(12 7 8 13 18 17 11 1 2 3 9 14 19 23 22 21 16 10 6)&{@,
nnbri=:+/@:ice_q@(6&{.)@:}.
cen=:{.
ice_q=:0.5&<

not_ice=:-.@:ice_q@:cen

nnbr_p=: ((1+i.6)&{ +/ . * (7++:i.6)&{)@:ice_q

in_range=: 1: <. 0&>.

set_auto=:3 : 0
exp=.(1 6&#@:(2&{.) , 12&$@:(2&}.)@:}:)%{:
swt=.exp&> ({.,1 0 0 1"_;}.)y.
lhxauto=: +/ . * [: {&swt not_ice * [: >: (-:@*>:)@(3:<.nnbri) + 3: <. nnbr_p
hxauto=: ([: in_range [: ,/ [: 0 2&|: ((2 1 ,:5 5)&(lhxauto@:lnbr ;._3)@:perext2) ,:(2 1,:5 5)&(lhxauto@:rnbr ;._3)@:perext2e )f.
$swt
)

init_fz=:4 : 0
x. ({.y.)Pad  1 (<0 0)}2 2${.y.
)

NB. define parameters (in homogeneous coords)
pr=:300 1 0 300;12 2 _1 12;2 3 1 _1 10;1 8 _3 20;1 3 _1 10
pr=:pr,1 8 _3 22;1 8 _3 18;1 3 _1 10;1 8 _3 22;1 8 _3 18

NB. set them to define the automaton
set_auto pr

NB. Iterate
$b=.hxauto^:(128) 128 init_fz 0.188

NB. save image as a file
(PBY;lin256 b) write_image 'test.png'