Galton box animation: Difference between revisions

J draft
(J draft)
Line 289:
FillArc(x, height-(ballcounts[x] +:= 1)*pegsize, pegsize, pegsize)
end</lang>
 
=={{header|J}}==
 
First, we need to a representation for our pins:
 
<lang j>initpins=: '* ' {~ '1'&i.@(-@|. |."_1 [: ":@-.&0"1 <:~/~)@i.</lang>
 
For example:
 
<lang j> initpins 4
*
* *
* * *
* * * *</lang>
 
Note that we could introduce other pin arrangements, for example a Sierpinski triangle:
 
<lang j>initSpins=: [: }.@|. (1- 2&^@>:) ]\ [: ,] (,~ ,.~)@]^:[ ,: bind '* '</lang>
 
... but this will not be too interesting to use, because of the lack of interior pins for the balls to bounce off of.
 
Anyways, once we have that, we can add balls to our picture:
 
<lang j>init=: ' ',. ' ',.~ ] ,~ ' ',~ ' o' {~ (# ' ' ~: 1&{.)</lang>
 
For example:
 
<lang j> 3 (init initpins) 4
o
o
o
*
* *
* * *
* * * * </lang>
 
Now we just need some way of updating our datastructure.
 
We will need a mechanism to shift a ball left or right if it's above a pin:
 
<lang>bounce=: (C.~ ] <"1@:+ 0 1 -~/~ ? @: (2"0))"1 [: I. 'o*'&E."1&.|:</lang>
 
And, a mechanism to make the balls fall:
 
<lang>shift=: 4 :0
fill=. {.0#,y
x |.!.fill y
)</lang>
 
And then we need to separate out the balls from the pins, so the balls fall and the pins do not. Note also that in this representation, balls will have to fall when they bounce because they cannot occupy the same space that a pin occupies.
 
We will also want some way of preventing the balls from falling forever. For this task it's probably sufficient to introduce a baseline sufficiently deep to hold the stacks and have balls instantly fall as close as they can to the baseline once they are past the pins.
 
<lang j>pins=: '*'&=
balls=: 'o'&=
 
bounce=: (C.~ 0 1 <@(-/~) [: (+ ?@2:"0) I.)"1
 
nxt=: ' ',~ [: clean ' *o' {~ pins + 2 * _1 shift balls bounce balls *. 1 shift pins
 
clean2=: ({. , -.&' '"1&.|:&.|.@}.)~ 1 + >./@(# | '*' i:~"1 |:)
clean1=: #~ 1 1 -.@E. *./"1@:=&' '
clean=: clean1@clean2</lang>
 
For example:
 
<lang j> nxt nxt 3 (init initpins) 4
o
o
o*
* *
* * *
* * * *
</lang>
 
Or, showing an entire animation sequence:
 
<lang j> nxt&.>^:a: <7 (init ' ',.' ',.~ initpins) 5
┌─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┐
│ o │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ o │ o │ o │ o │ o │ o │ o │ o │ *o │ * │ * │ * │ * │ * │
│ o │ o │ o │ o │ o │ o │ o │ *o │ *o* │ *o* │ * * │ * * │ * * │ * * │
│ o │ o │ o │ o │ o │ o │ *o │ * *o │ * * *o │ * *o* │ * *o* │ * * * │ * * * │ * * * │
│ o │ o │ o │ o │ o │ *o │ * *o │ * *o* │ * *o* * │ * * * *o │ * *o* * │ * *o* * │ * * * * │ * * * * │
│ o │ o │ o │ o │ o* │ o* * │ *o* * │ * *o* * │ * * *o* * │ * *o* * * │ * * * * *o │ * * *o* * │ * *o* * * │ * * * * * │
│ o │ o │ o │ *o │ *o* │ * *o* │ * * *o* │ * * *o* * │ o │ o │ o │ o │ o │ o │
│ │ o │ *o │ * *o │ * *o* │ * *o* * │ * * *o* * │ o │ o │ o │ o │ o │ o │ o │
│ * │ * │ * * │ * * * │ * * * * │ * * * * * │ │ │ │ o │ o o │ o o o │ o │ o o │
│ * * │ * * │ * * * │ * * * * │ * * * * * │ │ │ │ │ │ │ │ o o o │ o o o │
│ * * * │ * * * │ * * * * │ * * * * * │ │ │ │ │ │ │ │ │ │ │
│ * * * * │ * * * * │ * * * * * │ │ │ │ │ │ │ │ │ │ │ │
│ * * * * * │ * * * * * │ │ │ │ │ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │
└─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┘</lang>
 
=={{header|Ruby}}==
6,962

edits