You might have seen John Scholes’ quietly famous implementation (in Dyalog APL) of Conway’s Game of Life. We’re going to explore just one of the array-language features in it, indexing one array with another.

You can experiment with the expressions at tryapl.org. To animate them you need a Dyalog interpreter, with its editor and delay function.

The principle seems simple enough.

      'abcdef'[1 3 4 6]
acdf

The hidden surprise is that the result of such an expression has the same shape as the array in the index brackets.

      2 2⍴1 3 4 6
1 3
4 6
      'abcdef'[2 2⍴1 3 4 6]
ac
df

snow00

Snow Crash, the title of Neal Stephenson’s acclaimed debut novel, is a reference to the random visual ‘white noise’ on the screen of a completely crashed computer. Let’s make some snow.

      '⎕⌹'[?2 2⍴2]
⌹⌹
⌹⎕
      '⎕⌹'[?2 2⍴2]
⎕⌹
⌹⌹

Now let’s follow John’s example and animate it.

      pic←''
      ⎕ed 'pic'
      {} {pic∘←'⎕⌹'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8

snow01

Five seconds of ‘snow crash’. Perhaps it would look more snowy with snowflakes – for which we’ll use the five-pointed APL star.

      {} {pic∘←' *'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8

snow02

Nice. Except real snow doesn’t flicker like that. It falls. It drifts.

Park that thought. Raise the temperature.

Oh, westron wind, when wilt thou blow
That the small rain down may rain?
Christ, that my love were in my arms
And I in my bed again!

wrote Henry VIII. Here we go.

      {} {pic∘←' |'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8

snow03

Uh. Wasn’t that wind supposed to be blowing from the west?

{} {pic∘←' \'[?30 100⍴2]⊣⎕DL÷⍵ ⋄ ⍵} ⍣40 ⊢8

snow04

Enough rain. Back to snowflakes. And fewer of them.

      pic←' *'[1+1=?30 100⍴20]

snow05

Flakes must fall.We need a skyfall algorithm. Drop the bottom row, prefix new flakes at the top:

      pic←{' *'[1+1=?100/20]⍪¯1 0↓⍵} pic

snow06

And some animation.

      next←{' *'[1+1=?100/20]⍪¯1 0↓⍵} 
      {} {pic∘←next ⍵⊣⎕DL÷8} ⍣40 ⊢pic

This snow falls straight down, which isn’t quite right. We expect a little wind. From the west, perhaps.

next←{
   (L T)←{' *'[1+⍵]}¨1=?((⍴⍵)-0 1)⍴¨20
   L,T⍪¯1 ¯1↓⍵
} 
{} {pic∘←next ⍵⊣⎕DL÷8} ⍣200 ⊢pic

The snowfield is 3-D. Nearer flakes should be larger – and fewer – than distant ones.

next←{
   (L T)←{'⍟**∘∘∘...... '[⍵]}¨13⌊?(0 ¯1+⍴⍵)⍴¨100
   L,T⍪¯1 ¯1↓⍵
}
{} {pic∘←next ⍵⊣⎕DL÷8} ⍣200 ⊢pic

snow08

There we go. 25 seconds of simulated snowfall.

We’re missing some features. Turbulence produces a certain amount of random jiggling for the flakes. Distant flakes should fall more slowly than near flakes. We’ve reached the limit of our simple representation of a snowfield as 2-D character array. We might (in a later post) represent the flakes differently, and project their representations onto a visual plane.

Watch the whole thing on YouTube!

For now, a Merry Christmas and – let it snow!

Advertisements

6 thoughts on “Let it snow

      1. Yes,
        at 800 meters MSL we had +8 °C today, 😦
        we are in desperate need of all the snow we can get

        Greetings from Austria.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s