Wave function collapse: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: use local names for intermediate results in wfc)
Line 10: Line 10:
wfc=: {{
wfc=: {{
M=. #.,"2 tiles
M=. #.,"2 tiles
opts=: (<each, <@<)M
opts=. (<each, <@<)M
adj=: y#.y|"1(y#:,i.y)+"1/0,<:3 3#:1 3 5 7
adj=. y#.y|"1(y#:,i.y)+"1/0,<:3 3#:1 3 5 7
horz=: ({."1 -:"1/ {:"1) m
horz=. ({."1 -:"1/ {:"1) m
vert=: ({."2 -:"1/ {:"2) m
vert=. ({."2 -:"1/ {:"2) m
id=: <@I.(=i.#tiles),1 NB. identity constraint
id=. <@I.(=i.#tiles),1 NB. identity constraint
north=: <@I.vert,1 NB. adj 1 constraint
north=. <@I.vert,1 NB. adj 1 constraint
south=: <@I.(|:vert),1 NB. adj 7 constraint
south=. <@I.(|:vert),1 NB. adj 7 constraint
west=: <@I.horz,1 NB. adj 3 constrint
west=. <@I.horz,1 NB. adj 3 constrint
east=: <@I.(|:horz),1 NB. adj 5 constraint
east=. <@I.(|:horz),1 NB. adj 5 constraint
allow=: id,north,west,east,:south
allow=. id,north,west,east,:south
i=: (?#m) (?#i)} i=: ,y$_1
i=. (?#m) (?#i)} i=. ,y$_1
while. #todo=: I._1=i do.
while. #todo=. I._1=i do.
wave=: ([-.-.)L:0/"1 (adj{i){"0 1"1 2 allow
wave=. ([-.-.)L:0/"1 (adj{i){"0 1"1 2 allow
entropy=: #@>wave
entropy=. #@>wave
min=. <./ E=: todo{entropy
min=. <./ E=. todo{entropy
if. 0=min do. EMPTY return. end.
if. 0=min do. EMPTY return. end.
ndx=: todo{~({~ ?@#) I.min=E
ndx=. todo{~({~ ?@#) I.min=E
i=: (({~?@#)ndx{::wave) ndx} i
i=. (({~?@#)ndx{::wave) ndx} i
end.
end.
,/"2,/0 2 1 3|:(y$i){m
,/"2,/0 2 1 3|:(y$i){m