Wave function collapse: Difference between revisions

J: bugfix
(C: bugfix (need to apply to J version also))
(J: bugfix)
Line 196:
horz=: ({."1 -:"1/ {:"1) m NB. horizontal tile pairs
vert=: ({."2 -:"1/ {:"2) m NB. vertical tile pairs
north=: 1,~|:vert NB. adj 1 constraint
south=: 1,~|:vert NB. adj 7 constraint
west=: 1,~|:horz NB. adj 3 constrint
east=: 1,~|:horz NB. adj 5 constraint
allow=: north,west,east,:south
i=: (?#m) (?#i)} i=: ,y$_1
Line 210:
i=: (({~?@#)I.ndx{wave) (ndx{todo)} i
end.
lap=. {{ y#~(+0=i.@#)-.;m$<n{.1 }}
({:y)lap({:$m)"1 ({.y)lap({:$m),/"2,/0 2 1 3|:(y$i){m
}}</lang>
 
Line 223 ⟶ 224:
Once we're set up, we drop into a loop: <code>todo</code> selects the unchosen tile locations, <code>wave</code>lists for each of the unchoosen tiles (for each <code>todo</code> value in <code>i</code> we select the tiles allowed by each of its adjacent locations and find the set intersection of all of those), <code>entropy</code> counts how many tiles are eligible for each of those location, and <code>min</code> is the smallest value in <code>entropy</code>. <code>ndx</code> is a randomly picked index into <code>todo</code> with minimal entropy and for that location we randomly pick one of the options and update <code>i</code> with it. (When there's only one option remaining, "randomly pick" here means we pick that option.)
 
Once we've assigned a tile to every location in <code>i</code>, we use those indices to assemble the result (note that the 3x3 tiles overlap at their borders so we introduce a mechanism to discard the redundant pixels).
 
For task purposes here, we will use space to represent a white pixel and "#" to represent a black pixel. Also, because characters are narrow, we will insert a space between each of these "pixels" to better approximate a square aspect ratio.
 
Task example (the initial tiles and three runs of wave function collapse (twothree, to illustrate randomness):<lang J> (<"2) 1j1#"1 ' #'{~ tiles
┌──────┬──────┬──────┬──────┬──────┐
│ │ │ # │ # │ # │
Line 235 ⟶ 236:
 
task=: {{ 1j1#"1 ' #'{~ tiles wfc 8 8}}
task&.>0 0 0
┌──────────────────────────────────┬──────────────────────────────────┬──────────────────────────────────┐
┌────────────────────────────────────────────────┬────────────────────────────────────────────────┐
# # # # # # # # # # # # # # # # # # #
│# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│ # # # # # # # # # # # # # # # # # # │
│ # # # # # # # # # # # # │ # # # # # # # # # # # # # # │# # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # │
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # # # # # #
│ # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # │ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
│ # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # #
│ # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # │
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # ## # # # # # # # # # # # # # # │# # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
└──────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┘</lang>
│ # # # # # │ # # # # # │
│ # # # # # # # │ # # # # # # │
│ # # # # # # # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # # │
│ # # # # # # # │ # # # │
│ # # # # │ # # # # │
│# # # # # # # # # # # # # # # # # │# # # # # # # # # # # # # # # # # # # # # # │
│ # # # # # # # │ # # # # # # │
└────────────────────────────────────────────────┴────────────────────────────────────────────────┘</lang>
 
=={{header|Phix}}==
6,962

edits