Melody and structure
Four ways to get a melody from a rule: set algebra, a self-similar series, a herd of cows and the permutations of church bells. Press ▶ Play and watch the picture. Then move the controls: the sound changes on the next loop. Each example's code is folded away under it: open Show the code to read or change it.
import%20math%0Afrom%20itertools%20import%20combinations%0A%0Aimport%20marimo%20as%20mo%0Aimport%20numpy%20as%20np%0Afrom%20eddy%20import%20n%2C%20note%2C%20s%2C%20scale%2C%20sequence%2C%20silence%2C%20slowcat%2C%20stack%2C%20timecat%0Afrom%20eddy.euclid%20import%20bjorklund%0Afrom%20eddy.marimo%20import%20Picture%2C%20Player%2C%20cursor%2C%20play%2C%20render%0A%0A%0Adef%20hits(bits%2C%20sound)%3A%0A%20%20%20%20%22%22%22A%20row%20of%200s%20and%201s%20as%20a%20rhythm%3A%20%60sound%60%20on%20every%201%2C%20a%20rest%20on%20every%200.%22%22%22%0A%20%20%20%20return%20s(%22%20%22.join(sound%20if%20bit%20else%20%22~%22%20for%20bit%20in%20bits))%0A%0A%0Adef%20steps(values)%3A%0A%20%20%20%20%22%22%22A%20list%20of%20numbers%20as%20equal%20steps%2C%20for%20%60note(...)%60%20or%20%60scale(...)%60.%20None%20is%20a%20rest.%22%22%22%0A%20%20%20%20return%20sequence(*%5B%22~%22%20if%20value%20is%20None%20else%20value%20for%20value%20in%20values%5D)
Show the setup code 18 lines import math
from itertools import combinations
import marimo as mo
import numpy as np
from eddy import n , note , s , scale , sequence , silence , slowcat , stack , timecat
from eddy.euclid import bjorklund
from eddy.marimo import Picture , Player , cursor , play , render
def hits ( bits , sound ):
"""A row of 0s and 1s as a rhythm: `sound` on every 1, a rest on every 0."""
return s ( " " . join ( sound if bit else "~" for bit in bits ))
def steps ( values ):
"""A list of numbers as equal steps, for `note(...)` or `scale(...)`. None is a rest."""
return sequence ( * [ "~" if value is None else value for value in values ])
Xenakis' sieves: scales by set algebra
Iannis Xenakis built scales from what he called
sieves .
The building block is a residue class, every \(a\) -th point of a line starting at \(i\) ,
\[
a_i = \{\, x : x \equiv i \pmod a \,\}
\]
and classes are combined with union \(\cup\) , intersection \(\cap\) and complement. If the
points are semitones, a sieve is a scale. Unlike the scales we are used to, it need not
repeat at the octave: two classes together repeat every \(\operatorname{lcm}(a, b)\)
semitones. Xenakis' own example is the major scale, written with the moduli 3 and 4 only:
\[
(\overline{3_2} \cap 4_0) \ \cup\ (\overline{3_1} \cap 4_1) \ \cup\ (3_2 \cap 4_2) \ \cup\ (\overline{3_0} \cap 4_3)
\]
Python's sets already have the operators. The run below plays three octaves; the row of
octave marks shows how the sieve drifts against them.
sieve_a%20%3D%20mo.ui.slider(2%2C%209%2C%20value%3D3%2C%20label%3D%22every%20a-th%22%2C%20show_value%3DTrue)%0Asieve_b%20%3D%20mo.ui.slider(2%2C%209%2C%20value%3D5%2C%20label%3D%22every%20b-th%22%2C%20show_value%3DTrue)%0Asieve_shift%20%3D%20mo.ui.slider(0%2C%208%2C%20value%3D1%2C%20label%3D%22b%20starts%20at%22%2C%20show_value%3DTrue)%0Asieve_op%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22%E2%88%AA%20%20in%20either%22%3A%20set.union%2C%0A%20%20%20%20%20%20%20%20%22%E2%88%A9%20%20in%20both%22%3A%20set.intersection%2C%0A%20%20%20%20%20%20%20%20%22%E2%88%96%20%20in%20a%20but%20not%20b%22%3A%20set.difference%2C%0A%20%20%20%20%20%20%20%20%22%E2%96%B3%20%20in%20exactly%20one%22%3A%20set.symmetric_difference%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20value%3D%22%E2%88%AA%20%20in%20either%22%2C%0A%20%20%20%20label%3D%22combine%22%2C%0A)%0Asieve_player%20%3D%20Player()%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5Bsieve_a%2C%20sieve_b%2C%20sieve_shift%2C%20sieve_op%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5%2C%20wrap%3DTrue)%2C%0A%20%20%20%20%20%20%20%20mo.ui.anywidget(sieve_player)%2C%0A%20%20%20%20%5D%0A)
Show the code 20 lines sieve_a = mo . ui . slider ( 2 , 9 , value = 3 , label = "every a-th" , show_value = True )
sieve_b = mo . ui . slider ( 2 , 9 , value = 5 , label = "every b-th" , show_value = True )
sieve_shift = mo . ui . slider ( 0 , 8 , value = 1 , label = "b starts at" , show_value = True )
sieve_op = mo . ui . dropdown (
{
"∪ in either" : set . union ,
"∩ in both" : set . intersection ,
"∖ in a but not b" : set . difference ,
"△ in exactly one" : set . symmetric_difference ,
},
value = "∪ in either" ,
label = "combine" ,
)
sieve_player = Player ()
mo . vstack (
[
mo . hstack ([ sieve_a , sieve_b , sieve_shift , sieve_op ], justify = "start" , gap = 1.5 , wrap = True ),
mo . ui . anywidget ( sieve_player ),
]
)
17 of 36 semitones. The two classes line up again every 15 semitones, so each octave gets different notes.
def%20residues(modulus%2C%20shift%3D0%2C%20size%3D36)%3A%0A%20%20%20%20%22%22%22Every%20%60modulus%60-th%20point%20starting%20at%20%60shift%60%3A%20Xenakis%20writes%20it%20modulus_shift.%22%22%22%0A%20%20%20%20return%20%7Bpoint%20for%20point%20in%20range(size)%20if%20point%20%25%20modulus%20%3D%3D%20shift%20%25%20modulus%7D%0A%0A%0A_a%2C%20_b%20%3D%20residues(sieve_a.value)%2C%20residues(sieve_b.value%2C%20sieve_shift.value)%0A_sieve%20%3D%20sieve_op.value(_a%2C%20_b)%0A_period%20%3D%20math.lcm(sieve_a.value%2C%20sieve_b.value)%0A%0A%23%20three%20octaves%20of%20semitones%2C%20bottom%20to%20top%3B%20the%20notes%20in%20the%20sieve%20sound%2C%20the%20rest%20are%20silent%0A_run%20%3D%20note(steps(%5Bpoint%20-%2012%20if%20point%20in%20_sieve%20else%20None%20for%20point%20in%20range(36)%5D)).s(%22pluck%22).room(0.3)%0A%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%5B(1%2C%20%22s1%22%20if%20point%20in%20_a%20else%20None)%20for%20point%20in%20range(36)%5D%2C%0A%20%20%20%20%20%20%20%20%5B(1%2C%20%22s2%22%20if%20point%20in%20_b%20else%20None)%20for%20point%20in%20range(36)%5D%2C%0A%20%20%20%20%20%20%20%20%5B(1%2C%20%22ink%22%20if%20point%20in%20_sieve%20else%20None)%20for%20point%20in%20range(36)%5D%2C%0A%20%20%20%20%20%20%20%20%5B(1%2C%20%22muted%22%20if%20point%20%25%2012%20%3D%3D%200%20else%20None)%20for%20point%20in%20range(36)%5D%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20labels%3D%5B%22a%22%2C%20%22b%22%2C%20%22the%20sieve%22%2C%20%22octaves%22%5D%2C%0A%20%20%20%20times%3D%5B(0%2C%201)%5D%20*%204%2C%0A)%0Asieve_player.load(render(_run%2C%20cycles%3D1%2C%20cps%3D0.2)%2C%20_picture)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20_picture%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blen(_sieve)%7D%20of%2036%20semitones.%20The%20two%20classes%20line%20up%20again%20every%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7B_period%7D**%20semitones%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%2C%20so%20every%20octave%20gets%20the%20same%20notes.%22%20if%2012%20%25%20_period%20%3D%3D%200%20else%20%22%2C%20so%20each%20octave%20gets%20different%20notes.%22)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 34 lines def residues ( modulus , shift = 0 , size = 36 ):
"""Every `modulus`-th point starting at `shift`: Xenakis writes it modulus_shift."""
return { point for point in range ( size ) if point % modulus == shift % modulus }
_a , _b = residues ( sieve_a . value ), residues ( sieve_b . value , sieve_shift . value )
_sieve = sieve_op . value ( _a , _b )
_period = math . lcm ( sieve_a . value , sieve_b . value )
# three octaves of semitones, bottom to top; the notes in the sieve sound, the rest are silent
_run = note ( steps ([ point - 12 if point in _sieve else None for point in range ( 36 )])) . s ( "pluck" ) . room ( 0.3 )
_picture = blocks (
[
[( 1 , "s1" if point in _a else None ) for point in range ( 36 )],
[( 1 , "s2" if point in _b else None ) for point in range ( 36 )],
[( 1 , "ink" if point in _sieve else None ) for point in range ( 36 )],
[( 1 , "muted" if point % 12 == 0 else None ) for point in range ( 36 )],
],
labels = [ "a" , "b" , "the sieve" , "octaves" ],
times = [( 0 , 1 )] * 4 ,
)
sieve_player . load ( render ( _run , cycles = 1 , cps = 0.2 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
f " { len ( _sieve ) } of 36 semitones. The two classes line up again every "
f "** { _period } ** semitones"
+ ( ", so every octave gets the same notes." if 12 % _period == 0 else ", so each octave gets different notes." )
),
]
)
_mod3%20%3D%20%5Bresidues(3%2C%20shift%2C%2012)%20for%20shift%20in%20range(3)%5D%0A_mod4%20%3D%20%5Bresidues(4%2C%20shift%2C%2012)%20for%20shift%20in%20range(4)%5D%0Amajor_scale%20%3D%20(_mod4%5B0%5D%20-%20_mod3%5B2%5D)%20%7C%20(_mod4%5B1%5D%20-%20_mod3%5B1%5D)%20%7C%20(_mod4%5B2%5D%20%26%20_mod3%5B2%5D)%20%7C%20(_mod4%5B3%5D%20-%20_mod3%5B0%5D)%0Aassert%20major_scale%20%3D%3D%20%7B0%2C%202%2C%204%2C%205%2C%207%2C%209%2C%2011%7D%0Amo.md(f%22%60%7Bsorted(major_scale)%7D%60%20are%20the%20semitones%20of%20C%20D%20E%20F%20G%20A%20B.%22)
Show the code 5 lines _mod3 = [ residues ( 3 , shift , 12 ) for shift in range ( 3 )]
_mod4 = [ residues ( 4 , shift , 12 ) for shift in range ( 4 )]
major_scale = ( _mod4 [ 0 ] - _mod3 [ 2 ]) | ( _mod4 [ 1 ] - _mod3 [ 1 ]) | ( _mod4 [ 2 ] & _mod3 [ 2 ]) | ( _mod4 [ 3 ] - _mod3 [ 0 ])
assert major_scale == { 0 , 2 , 4 , 5 , 7 , 9 , 11 }
mo . md ( f "` { sorted ( major_scale ) } ` are the semitones of C D E F G A B." )
The infinity series: a tune that contains itself
The Danish composer Per Nørgård found his infinity series in 1959 and built pieces such
as Voyage into the Golden Screen and his Second Symphony on it. Three short rules
define it:
\[
a_0 = 0, \qquad a_{2n} = -a_n, \qquad a_{2n+1} = a_n + 1
\]
It is A004718 in the OEIS, and
Shallit and Drexler-Lemire have studied it. Read as
steps of a scale, the series is a melody with a strange property, which follows from
applying the first rule twice:
\[
a_{4n} = a_n \quad \text{(every fourth note is the tune again)}, \qquad
a_{2n} = -a_n \quad \text{(every second note is the tune upside down)}
\]
Add either one underneath and listen to them meet. The series' odd and even numbers also
spell out the Thue–Morse sequence, which makes a good drum part.
under%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22nothing%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%22every%204th%20note%3A%20the%20same%20tune%2C%204%C3%97%20slower%22%3A%204%2C%0A%20%20%20%20%20%20%20%20%22every%202nd%20note%3A%20the%20tune%20upside%20down%2C%202%C3%97%20slower%22%3A%202%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20value%3D%22every%204th%20note%3A%20the%20same%20tune%2C%204%C3%97%20slower%22%2C%0A%20%20%20%20label%3D%22underneath%22%2C%0A)%0Adrums%20%3D%20mo.ui.checkbox(value%3DTrue%2C%20label%3D%22Thue%E2%80%93Morse%20drums%22)%0Ainfinity_player%20%3D%20Player()%0Amo.vstack(%5Bmo.hstack(%5Bunder%2C%20drums%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5)%2C%20mo.ui.anywidget(infinity_player)%5D)
Show the code 12 lines under = mo . ui . dropdown (
{
"nothing" : 0 ,
"every 4th note: the same tune, 4× slower" : 4 ,
"every 2nd note: the tune upside down, 2× slower" : 2 ,
},
value = "every 4th note: the same tune, 4× slower" ,
label = "underneath" ,
)
drums = mo . ui . checkbox ( value = True , label = "Thue–Morse drums" )
infinity_player = Player ()
mo . vstack ([ mo . hstack ([ under , drums ], justify = "start" , gap = 1.5 ), mo . ui . anywidget ( infinity_player )])
6 -5 0 note 0: 0 note 1: 1 note 2: -1 note 3: 2 note 4: 1 note 5: 0 note 6: -2 note 7: 3 note 8: -1 note 9: 2 note 10: 0 note 11: 1 note 12: 2 note 13: -1 note 14: -3 note 15: 4 note 16: 1 note 17: 0 note 18: -2 note 19: 3 note 20: 0 note 21: 1 note 22: -1 note 23: 2 note 24: -2 note 25: 3 note 26: 1 note 27: 0 note 28: 3 note 29: -2 note 30: -4 note 31: 5 note 32: -1 note 33: 2 note 34: 0 note 35: 1 note 36: 2 note 37: -1 note 38: -3 note 39: 4 note 40: 0 note 41: 1 note 42: -1 note 43: 2 note 44: 1 note 45: 0 note 46: -2 note 47: 3 note 48: 2 note 49: -1 note 50: -3 note 51: 4 note 52: -1 note 53: 2 note 54: 0 note 55: 1 note 56: -3 note 57: 4 note 58: 2 note 59: -1 note 60: 4 note 61: -3 note 62: -5 note 63: 6 The first 64 numbers, as a melody. The dots are the notes the slower voice plays.
def%20infinity(count)%3A%0A%20%20%20%20%22%22%22Per%20N%C3%B8rg%C3%A5rd's%20infinity%20series%3A%20a(2n)%20%3D%20-a(n)%20and%20a(2n%20%2B%201)%20%3D%20a(n)%20%2B%201.%22%22%22%0A%20%20%20%20a%20%3D%20%5B0%5D%0A%20%20%20%20for%20i%20in%20range(1%2C%20count)%3A%0A%20%20%20%20%20%20%20%20a.append(-a%5Bi%20%2F%2F%202%5D%20if%20i%20%25%202%20%3D%3D%200%20else%20a%5Bi%20%2F%2F%202%5D%20%2B%201)%0A%20%20%20%20return%20a%0A%0A%0A_a%20%3D%20infinity(64)%0Aassert%20_a%5B%3A%3A4%5D%20%3D%3D%20_a%5B%3A16%5D%20%20%23%20every%204th%20note%20is%20the%20tune%20again%0Aassert%20_a%5B%3A%3A2%5D%20%3D%3D%20%5B-x%20for%20x%20in%20_a%5B%3A32%5D%5D%20%20%23%20every%202nd%20note%20is%20the%20tune%20upside%20down%0A%0A_layers%20%3D%20%5Bn(scale(%22dorian%22%2C%20steps(_a))).s(%22pluck%22)%5D%0Aif%20under.value%3A%0A%20%20%20%20_layers.append(n(scale(%22dorian%22%2C%20steps(_a%5B%3A%3A%20under.value%5D))%20-%2012).s(%22superpiano%22).gain(0.8))%0Aif%20drums.value%3A%0A%20%20%20%20%23%20even%20numbers%20get%20a%20kick%2C%20odd%20numbers%20a%20hi-hat%3A%20bd%20hh%20hh%20bd%20hh%20bd%20bd%20hh%20...%0A%20%20%20%20_layers%20%2B%3D%20%5Bhits(%5Bx%20%25%202%20%3D%3D%200%20for%20x%20in%20_a%5D%2C%20%22bd%22).gain(0.8)%2C%20hits(%5Bx%20%25%202%20for%20x%20in%20_a%5D%2C%20%22hh%22).gain(0.5)%5D%0A%0A_picture%20%3D%20curve(_a%2C%20marks%3Drange(0%2C%2064%2C%20under.value)%20if%20under.value%20else%20()%2C%20span%3D(0%2C%204))%0Ainfinity_player.load(render(stack(*_layers).slow(4)%2C%20cycles%3D4%2C%20cps%3D0.4)%2C%20_picture)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20_picture%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22The%20first%2064%20numbers%2C%20as%20a%20melody.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22The%20dots%20are%20the%20notes%20the%20slower%20voice%20plays.%22%20if%20under.value%20else%20%22%22)%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 31 lines def infinity ( count ):
"""Per Nørgård's infinity series: a(2n) = -a(n) and a(2n + 1) = a(n) + 1."""
a = [ 0 ]
for i in range ( 1 , count ):
a . append ( - a [ i // 2 ] if i % 2 == 0 else a [ i // 2 ] + 1 )
return a
_a = infinity ( 64 )
assert _a [:: 4 ] == _a [: 16 ] # every 4th note is the tune again
assert _a [:: 2 ] == [ - x for x in _a [: 32 ]] # every 2nd note is the tune upside down
_layers = [ n ( scale ( "dorian" , steps ( _a ))) . s ( "pluck" )]
if under . value :
_layers . append ( n ( scale ( "dorian" , steps ( _a [:: under . value ])) - 12 ) . s ( "superpiano" ) . gain ( 0.8 ))
if drums . value :
# even numbers get a kick, odd numbers a hi-hat: bd hh hh bd hh bd bd hh ...
_layers += [ hits ([ x % 2 == 0 for x in _a ], "bd" ) . gain ( 0.8 ), hits ([ x % 2 for x in _a ], "hh" ) . gain ( 0.5 )]
_picture = curve ( _a , marks = range ( 0 , 64 , under . value ) if under . value else (), span = ( 0 , 4 ))
infinity_player . load ( render ( stack ( * _layers ) . slow ( 4 ), cycles = 4 , cps = 0.4 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
"The first 64 numbers, as a melody. "
+ ( "The dots are the notes the slower voice plays." if under . value else "" )
),
]
)
Narayana's cows
In 1356 the Indian mathematician Narayana Pandita asked: a cow has one calf every year,
and each calf has a calf of her own every year from her fourth year on. How many animals
are there after 20 years? Each year's herd is last year's herd plus the calves of every
cow that was alive three years ago:
\[
N_n = N_{n-1} + N_{n-3} \qquad \longrightarrow \qquad 1,\ 1,\ 1,\ 2,\ 3,\ 4,\ 6,\ 9,\ 13,\ 19,\ \dots
\]
(A000930 .) Tom Johnson turned the problem into
Narayana's Cows (1989), which plays the herd year
by year: a long note for every cow, a short note for every calf. Writing \(C\) for a cow
and \(c_1, c_2, c_3\) for a calf in her first, second and third year, each year rewrites
the herd by the rule
\[
C \to C\,c_1, \qquad c_1 \to c_2, \qquad c_2 \to c_3, \qquad c_3 \to C\,c_1
\]
and that rule is the whole composition. The pitches are this notebook's choice, not
Johnson's score: each generation sits one step higher, and calves sing an octave above
their mothers.
years%20%3D%20mo.ui.slider(1%2C%209%2C%20value%3D6%2C%20label%3D%22years%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue)%0Acows_player%20%3D%20Player()%0Amo.vstack(%5Byears%2C%20mo.ui.anywidget(cows_player)%5D)
Show the code 3 lines years = mo . ui . slider ( 1 , 9 , value = 6 , label = "years" , show_value = True , debounce = True )
cows_player = Player ()
mo . vstack ([ years , mo . ui . anywidget ( cows_player )])
year 1 year 2 year 3 year 4 year 5 year 6 the first cow her daughters granddaughters great-granddaughters
Wide blocks are cows, narrow blocks are calves. Herd sizes: 2, 3, 4, 6, 9, 13.
def%20next_year(herd)%3A%0A%20%20%20%20%22%22%22An%20animal%20is%20(age%2C%20generation).%20At%20age%203%2C%20her%20fourth%20year%2C%20she%20is%20a%20cow%20and%20has%20a%20calf%20every%20year.%22%22%22%0A%20%20%20%20grown%20%3D%20%5B%5D%0A%20%20%20%20for%20age%2C%20generation%20in%20herd%3A%0A%20%20%20%20%20%20%20%20age%20%3D%20min(age%20%2B%201%2C%203)%0A%20%20%20%20%20%20%20%20grown.append((age%2C%20generation))%0A%20%20%20%20%20%20%20%20if%20age%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20grown.append((0%2C%20generation%20%2B%201))%0A%20%20%20%20return%20grown%0A%0A%0A_herds%20%3D%20%5B%5B(3%2C%200)%2C%20(0%2C%201)%5D%5D%20%20%23%20year%201%3A%20the%20first%20cow%20and%20her%20first%20calf%0Afor%20_%20in%20range(years.value%20-%201)%3A%0A%20%20%20%20_herds.append(next_year(_herds%5B-1%5D))%0A%0A%23%20a%20cow%20is%20a%20long%20note%20and%20a%20calf%20a%20short%20one%3B%20a%20breath%20between%20years%0A_score%2C%20_when%20%3D%20%5B%5D%2C%20%5B%5D%0Afor%20_herd%20in%20_herds%3A%0A%20%20%20%20_begin%20%3D%20sum(length%20for%20length%2C%20_%20in%20_score)%0A%20%20%20%20for%20_age%2C%20_generation%20in%20_herd%3A%0A%20%20%20%20%20%20%20%20_degree%20%3D%20_generation%20if%20_age%20%3D%3D%203%20else%20_generation%20%2B%205%0A%20%20%20%20%20%20%20%20_score.append((2%20if%20_age%20%3D%3D%203%20else%201%2C%20n(scale(%22minpent%22%2C%20_degree)).s(%22superpiano%22)))%0A%20%20%20%20_when.append((_begin%20%2F%208%2C%20sum(length%20for%20length%2C%20_%20in%20_score)%20%2F%208))%20%20%23%20this%20year%2C%20in%20cycles%0A%20%20%20%20_score.append((2%2C%20silence()))%0A%0A%23%208%20beats%20to%20a%20cycle%3A%20pad%20the%20end%20so%20the%20loop%20is%20a%20whole%20number%20of%20cycles%0A_beats%20%3D%20sum(length%20for%20length%2C%20_%20in%20_score)%0A_cycles%20%3D%20math.ceil(_beats%20%2F%208)%0Aif%20_cycles%20*%208%20%3E%20_beats%3A%0A%20%20%20%20_score.append((_cycles%20*%208%20-%20_beats%2C%20silence()))%0A%0A_picture%20%3D%20blocks(%0A%20%20%20%20%5B%5B(2%20if%20age%20%3D%3D%203%20else%201%2C%20f%22s%7Bgeneration%20%2B%201%7D%22)%20for%20age%2C%20generation%20in%20herd%5D%20for%20herd%20in%20_herds%5D%2C%0A%20%20%20%20labels%3D%5Bf%22year%20%7Byear%20%2B%201%7D%22%20for%20year%20in%20range(len(_herds))%5D%2C%0A%20%20%20%20key%3D%5B%0A%20%20%20%20%20%20%20%20(f%22s%7Bg%20%2B%201%7D%22%2C%20name)%0A%20%20%20%20%20%20%20%20for%20g%2C%20name%20in%20enumerate(%5B%22the%20first%20cow%22%2C%20%22her%20daughters%22%2C%20%22granddaughters%22%2C%20%22great-granddaughters%22%5D)%0A%20%20%20%20%5D%2C%0A%20%20%20%20times%3D_when%2C%0A)%0Acows_player.load(render(timecat(*_score).slow(_cycles)%2C%20cycles%3D_cycles%2C%20cps%3D1)%2C%20_picture)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20_picture%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Wide%20blocks%20are%20cows%2C%20narrow%20blocks%20are%20calves.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Herd%20sizes%3A%20%7B'%2C%20'.join(str(len(herd))%20for%20herd%20in%20_herds)%7D.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 51 lines def next_year ( herd ):
"""An animal is (age, generation). At age 3, her fourth year, she is a cow and has a calf every year."""
grown = []
for age , generation in herd :
age = min ( age + 1 , 3 )
grown . append (( age , generation ))
if age == 3 :
grown . append (( 0 , generation + 1 ))
return grown
_herds = [[( 3 , 0 ), ( 0 , 1 )]] # year 1: the first cow and her first calf
for _ in range ( years . value - 1 ):
_herds . append ( next_year ( _herds [ - 1 ]))
# a cow is a long note and a calf a short one; a breath between years
_score , _when = [], []
for _herd in _herds :
_begin = sum ( length for length , _ in _score )
for _age , _generation in _herd :
_degree = _generation if _age == 3 else _generation + 5
_score . append (( 2 if _age == 3 else 1 , n ( scale ( "minpent" , _degree )) . s ( "superpiano" )))
_when . append (( _begin / 8 , sum ( length for length , _ in _score ) / 8 )) # this year, in cycles
_score . append (( 2 , silence ()))
# 8 beats to a cycle: pad the end so the loop is a whole number of cycles
_beats = sum ( length for length , _ in _score )
_cycles = math . ceil ( _beats / 8 )
if _cycles * 8 > _beats :
_score . append (( _cycles * 8 - _beats , silence ()))
_picture = blocks (
[[( 2 if age == 3 else 1 , f "s { generation + 1 } " ) for age , generation in herd ] for herd in _herds ],
labels = [ f "year { year + 1 } " for year in range ( len ( _herds ))],
key = [
( f "s { g + 1 } " , name )
for g , name in enumerate ([ "the first cow" , "her daughters" , "granddaughters" , "great-granddaughters" ])
],
times = _when ,
)
cows_player . load ( render ( timecat ( * _score ) . slow ( _cycles ), cycles = _cycles , cps = 1 ), _picture )
mo . vstack (
[
_picture ,
mo . md (
"Wide blocks are cows, narrow blocks are calves. "
f "Herd sizes: { ', ' . join ( str ( len ( herd )) for herd in _herds ) } ."
),
]
)
Change ringing: every order of the bells
English church bells swing through a full circle and are far too heavy to play tunes on,
so since the 1600s ringers have rung permutations instead
(change ringing ). The rules: start and
end with the bells in order from highest to lowest, never repeat a row, and let a bell
move at most one place from one row to the next.
Plain Bob Minimus rings all 24 orders of four bells using just three swaps of
neighbouring places, \(a = (1\,2)(3\,4)\) , \(b = (2\,3)\) and \(c = (3\,4)\) :
\[
\big( (ab)^3 \, a \, c \big)^3 = e
\]
Three times "\(a\,b\,a\,b\,a\,b\,a\,c\) ", and you are back where you started, having passed
through every element of the symmetric group \(S_4\) exactly once. In today's words it is a
Hamiltonian cycle on a Cayley graph of \(S_4\) , worked out by ringers long before group
theory had a name. Follow one colour to see a bell weave through the others.
lead 1 lead 2 lead 3 row 1: bell 1 in place 1 row 2: bell 1 in place 2 row 3: bell 1 in place 3 row 4: bell 1 in place 4 row 5: bell 1 in place 4 row 6: bell 1 in place 3 row 7: bell 1 in place 2 row 8: bell 1 in place 1 row 9: bell 1 in place 1 row 10: bell 1 in place 2 row 11: bell 1 in place 3 row 12: bell 1 in place 4 row 13: bell 1 in place 4 row 14: bell 1 in place 3 row 15: bell 1 in place 2 row 16: bell 1 in place 1 row 17: bell 1 in place 1 row 18: bell 1 in place 2 row 19: bell 1 in place 3 row 20: bell 1 in place 4 row 21: bell 1 in place 4 row 22: bell 1 in place 3 row 23: bell 1 in place 2 row 24: bell 1 in place 1 row 25: bell 1 in place 1 1 1 row 1: bell 2 in place 2 row 2: bell 2 in place 1 row 3: bell 2 in place 1 row 4: bell 2 in place 2 row 5: bell 2 in place 3 row 6: bell 2 in place 4 row 7: bell 2 in place 4 row 8: bell 2 in place 3 row 9: bell 2 in place 4 row 10: bell 2 in place 3 row 11: bell 2 in place 2 row 12: bell 2 in place 1 row 13: bell 2 in place 1 row 14: bell 2 in place 2 row 15: bell 2 in place 3 row 16: bell 2 in place 4 row 17: bell 2 in place 3 row 18: bell 2 in place 4 row 19: bell 2 in place 4 row 20: bell 2 in place 3 row 21: bell 2 in place 2 row 22: bell 2 in place 1 row 23: bell 2 in place 1 row 24: bell 2 in place 2 row 25: bell 2 in place 2 2 2 row 1: bell 3 in place 3 row 2: bell 3 in place 4 row 3: bell 3 in place 4 row 4: bell 3 in place 3 row 5: bell 3 in place 2 row 6: bell 3 in place 1 row 7: bell 3 in place 1 row 8: bell 3 in place 2 row 9: bell 3 in place 2 row 10: bell 3 in place 1 row 11: bell 3 in place 1 row 12: bell 3 in place 2 row 13: bell 3 in place 3 row 14: bell 3 in place 4 row 15: bell 3 in place 4 row 16: bell 3 in place 3 row 17: bell 3 in place 4 row 18: bell 3 in place 3 row 19: bell 3 in place 2 row 20: bell 3 in place 1 row 21: bell 3 in place 1 row 22: bell 3 in place 2 row 23: bell 3 in place 3 row 24: bell 3 in place 4 row 25: bell 3 in place 3 3 3 row 1: bell 4 in place 4 row 2: bell 4 in place 3 row 3: bell 4 in place 2 row 4: bell 4 in place 1 row 5: bell 4 in place 1 row 6: bell 4 in place 2 row 7: bell 4 in place 3 row 8: bell 4 in place 4 row 9: bell 4 in place 3 row 10: bell 4 in place 4 row 11: bell 4 in place 4 row 12: bell 4 in place 3 row 13: bell 4 in place 2 row 14: bell 4 in place 1 row 15: bell 4 in place 1 row 16: bell 4 in place 2 row 17: bell 4 in place 2 row 18: bell 4 in place 1 row 19: bell 4 in place 1 row 20: bell 4 in place 2 row 21: bell 4 in place 3 row 22: bell 4 in place 4 row 23: bell 4 in place 4 row 24: bell 4 in place 3 row 25: bell 4 in place 4 4 4 Rows run left to right; the top line is the first place in the row. Bell 1 is the highest note.
def%20plain_bob_minimus()%3A%0A%20%20%20%20%22%22%22All%2024%20orders%20of%20four%20bells.%20Every%20change%20swaps%20neighbours%20only.%22%22%22%0A%20%20%20%20cross%20%3D%20lambda%20row%3A%20%5Brow%5B1%5D%2C%20row%5B0%5D%2C%20row%5B3%5D%2C%20row%5B2%5D%5D%20%20%23%20a%3A%20both%20pairs%20swap%0A%20%20%20%20inside%20%3D%20lambda%20row%3A%20%5Brow%5B0%5D%2C%20row%5B2%5D%2C%20row%5B1%5D%2C%20row%5B3%5D%5D%20%20%23%20b%3A%20the%20middle%20pair%20swaps%0A%20%20%20%20behind%20%3D%20lambda%20row%3A%20%5Brow%5B0%5D%2C%20row%5B1%5D%2C%20row%5B3%5D%2C%20row%5B2%5D%5D%20%20%23%20c%3A%20the%20back%20pair%20swaps%0A%20%20%20%20row%2C%20rows%20%3D%20%5B1%2C%202%2C%203%2C%204%5D%2C%20%5B%5D%0A%20%20%20%20for%20lead%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20for%20change%20in%20%5Bcross%2C%20inside%5D%20*%203%20%2B%20%5Bcross%2C%20behind%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(row)%0A%20%20%20%20%20%20%20%20%20%20%20%20row%20%3D%20change(row)%0A%20%20%20%20return%20rows%0A%0A%0Abell_rows%20%3D%20plain_bob_minimus()%0Aassert%20len(%7Btuple(row)%20for%20row%20in%20bell_rows%7D)%20%3D%3D%2024%20%20%23%20every%20order%2C%20exactly%20once%0A%0A%23%20two%20rows%20per%20cycle%20and%20then%20a%20pause%2C%20as%20ringers%20do%3B%20bell%201%20is%20the%20highest%0A_peal%20%3D%20slowcat(%0A%20%20%20%20*%5B%0A%20%20%20%20%20%20%20%20n(scale(%22major%22%2C%20steps(%5B4%20-%20bell%20for%20bell%20in%20bell_rows%5Bi%5D%20%2B%20bell_rows%5Bi%20%2B%201%5D%5D%20%2B%20%5BNone%5D)))%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(0%2C%2024%2C%202)%0A%20%20%20%20%5D%0A).s(%22superpiano%22).room(0.3)%0A%0A_picture%20%3D%20braid(bell_rows%20%2B%20%5Bbell_rows%5B0%5D%5D%2C%20lead%3D8%2C%20per_cycle%3D2%2C%20rests%3D1)%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20_picture%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Rows%20run%20left%20to%20right%3B%20the%20top%20line%20is%20the%20first%20place%20in%20the%20row.%20Bell%201%20is%20the%20highest%20note.%22)%2C%0A%20%20%20%20%20%20%20%20play(_peal%2C%20cycles%3D12%2C%20cps%3D0.6%2C%20picture%3D_picture)%2C%0A%20%20%20%20%5D%0A)
Show the code 32 lines def plain_bob_minimus ():
"""All 24 orders of four bells. Every change swaps neighbours only."""
cross = lambda row : [ row [ 1 ], row [ 0 ], row [ 3 ], row [ 2 ]] # a: both pairs swap
inside = lambda row : [ row [ 0 ], row [ 2 ], row [ 1 ], row [ 3 ]] # b: the middle pair swaps
behind = lambda row : [ row [ 0 ], row [ 1 ], row [ 3 ], row [ 2 ]] # c: the back pair swaps
row , rows = [ 1 , 2 , 3 , 4 ], []
for lead in range ( 3 ):
for change in [ cross , inside ] * 3 + [ cross , behind ]:
rows . append ( row )
row = change ( row )
return rows
bell_rows = plain_bob_minimus ()
assert len ({ tuple ( row ) for row in bell_rows }) == 24 # every order, exactly once
# two rows per cycle and then a pause, as ringers do; bell 1 is the highest
_peal = slowcat (
* [
n ( scale ( "major" , steps ([ 4 - bell for bell in bell_rows [ i ] + bell_rows [ i + 1 ]] + [ None ])))
for i in range ( 0 , 24 , 2 )
]
) . s ( "superpiano" ) . room ( 0.3 )
_picture = braid ( bell_rows + [ bell_rows [ 0 ]], lead = 8 , per_cycle = 2 , rests = 1 )
mo . vstack (
[
_picture ,
mo . md ( "Rows run left to right; the top line is the first place in the row. Bell 1 is the highest note." ),
play ( _peal , cycles = 12 , cps = 0.6 , picture = _picture ),
]
)
import%20base64%0Aimport%20struct%0Aimport%20uuid%0Aimport%20zlib%0A%0Aimport%20anywidget%0Aimport%20traitlets%0A%0ASTYLE%20%3D%20%22%22%22%3Cstyle%3E%0A.mm%20%7B%20--s1%3A%232a78d6%3B%20--s2%3A%23eb6834%3B%20--s3%3A%231baf7a%3B%20--s4%3A%23eda100%3B%20--ink%3A%230b0b0b%3B%20--ink2%3A%2352514e%3B%0A%20%20--muted%3A%23898781%3B%20--grid%3A%23e1e0d9%3B%20--off%3Argba(11%2C11%2C11%2C.08)%3B%20--surface%3Avar(--background%2C%20%23fff)%3B%0A%20%20--glow%3A%231c5cab%3B%20--panel%3Argba(11%2C11%2C11%2C.05)%3B%20--eddy-cursor%3Avar(--ink)%3B%0A%20%20font%3A12px%2F1.5%20system-ui%2C%20-apple-system%2C%20%22Segoe%20UI%22%2C%20sans-serif%3B%20color%3Avar(--ink2)%3B%20flex%3Anone%3B%20%7D%0A.dark%20.mm%20%7B%20--s1%3A%233987e5%3B%20--s2%3A%23d95926%3B%20--s3%3A%23199e70%3B%20--s4%3A%23c98500%3B%20--ink%3A%23f0efec%3B%20--ink2%3A%23c3c2b7%3B%0A%20%20--grid%3A%232c2c2a%3B%20--off%3Argba(255%2C255%2C255%2C.1)%3B%20--surface%3Avar(--background%2C%20%231a1a19)%3B%0A%20%20--glow%3A%239ec5f4%3B%20--panel%3Argba(0%2C0%2C0%2C.45)%3B%20%7D%0A.mm%20svg%20%7B%20display%3Ablock%3B%20max-width%3A100%25%3B%20height%3Aauto%3B%20overflow%3Avisible%3B%20%7D%0A.mm%20text%20%7B%20fill%3Avar(--ink2)%3B%20font%3A11px%20system-ui%2C%20-apple-system%2C%20%22Segoe%20UI%22%2C%20sans-serif%3B%20%7D%0A.mm-key%20%7B%20display%3Aflex%3B%20flex-wrap%3Awrap%3B%20gap%3A4px%2016px%3B%20margin-top%3A8px%3B%20%7D%0A.mm-key%20i%20%7B%20display%3Ainline-block%3B%20width%3A10px%3B%20height%3A10px%3B%20border-radius%3A2px%3B%20margin-right%3A6px%3B%20%7D%0A%3C%2Fstyle%3E%22%22%22%0A%0A%0Adef%20figure(body%2C%20key%3DNone%2C%20marks%3D())%3A%0A%20%20%20%20%22%22%22Wrap%20a%20drawing%20in%20the%20shared%20styles%20and%20return%20it%20as%20an%20eddy%20Picture%2C%20with%20its%20cursor%20%60marks%60.%0A%20%20%20%20%60key%60%20is%20a%20legend%3A%20a%20list%20of%20(colour%2C%20label).%22%22%22%0A%20%20%20%20legend%20%3D%20%22%22%0A%20%20%20%20if%20key%3A%0A%20%20%20%20%20%20%20%20swatches%20%3D%20%22%22.join(f%22%3Cspan%3E%3Ci%20style%3D'background%3Avar(--%7Bc%7D)'%3E%3C%2Fi%3E%7Blabel%7D%3C%2Fspan%3E%22%20for%20c%2C%20label%20in%20key)%0A%20%20%20%20%20%20%20%20legend%20%3D%20f%22%3Cdiv%20class%3D'mm-key'%3E%7Bswatches%7D%3C%2Fdiv%3E%22%0A%20%20%20%20return%20Picture(f%22%7BSTYLE%7D%3Cdiv%20class%3D'mm'%3E%7Bbody%7D%7Blegend%7D%3C%2Fdiv%3E%22%2C%20marks)%0A%0A%0Adef%20blocks(rows%2C%20labels%3DNone%2C%20unit%3DNone%2C%20width%3D600%2C%20key%3DNone%2C%20times%3DNone%2C%20every%3DNone)%3A%0A%20%20%20%20%22%22%22Rows%20of%20blocks%20laid%20end%20to%20end.%20A%20block%20is%20(length%2C%20colour)%20or%20(length%2C%20colour%2C%20tooltip)%3B%0A%20%20%20%20colour%20None%20draws%20an%20empty%20slot.%20%60times%60%20gives%20each%20row%20the%20cycles%20during%20which%20it%20sounds%2C%0A%20%20%20%20as%20(from%2C%20to)%20or%20a%20list%20of%20them%3A%20the%20cursor%20crosses%20the%20row%20in%20that%20time.%22%22%22%0A%20%20%20%20total%20%3D%20max(sum(block%5B0%5D%20for%20block%20in%20row)%20for%20row%20in%20rows)%0A%20%20%20%20left%20%3D%200%20if%20labels%20is%20None%20else%2012%20%2B%206.2%20*%20max(len(str(label))%20for%20label%20in%20labels)%0A%20%20%20%20unit%20%3D%20unit%20or%20min(24%2C%20(width%20-%20left)%20%2F%20total)%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20r%2C%20row%20in%20enumerate(rows)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20left%2C%20r%20*%2018%0A%20%20%20%20%20%20%20%20if%20labels%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bleft%20-%208%3A.0f%7D'%20y%3D'%7By%20%2B%2012%7D'%20text-anchor%3D'end'%3E%7Blabels%5Br%5D%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20for%20length%2C%20colour%2C%20*tip%20in%20row%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20fill%20%3D%20f%22var(--%7Bcolour%7D)%22%20if%20colour%20else%20%22var(--off)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20title%20%3D%20f%22%3Ctitle%3E%7Btip%5B0%5D%7D%3C%2Ftitle%3E%22%20if%20tip%20else%20%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bx%20%2B%201%3A.1f%7D'%20y%3D'%7By%20%2B%201%7D'%20width%3D'%7Bmax(length%20*%20unit%20-%202%2C%201)%3A.1f%7D'%20height%3D'14'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22rx%3D'2'%20fill%3D'%7Bfill%7D'%3E%7Btitle%7D%3C%2Frect%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20x%20%2B%3D%20length%20*%20unit%0A%20%20%20%20%20%20%20%20if%20times%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20t0%2C%20t1%20in%20times%5Br%5D%20if%20isinstance(times%5Br%5D%2C%20list)%20else%20%5Btimes%5Br%5D%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.sweep(t0%2C%20t1%2C%20x0%3Dleft%2C%20x1%3Dx%2C%20top%3Dy%20-%201%2C%20bottom%3Dy%20%2B%2017%2C%20every%3Devery))%0A%20%20%20%20w%2C%20h%20%3D%20left%20%2B%20total%20*%20unit%2C%20len(rows)%20*%2018%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bw%3A.0f%7D'%20viewBox%3D'0%200%20%7Bw%3A.0f%7D%20%7Bh%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20key%2C%20marks)%0A%0A%0Adef%20ring(points%2C%20tick_labels%3DNone%2C%20ticks%3D12%2C%20polygon%3DFalse%2C%20size%3D240%2C%20key%3DNone%2C%20hand%3DNone%2C%20lit%3D())%3A%0A%20%20%20%20%22%22%22Dots%20on%20a%20circle.%20A%20point%20is%20(turn%2C%20colour)%20or%20(turn%2C%20colour%2C%20tooltip)%3B%20turn%200%20is%20the%20top%20and%0A%20%20%20%201%20is%20once%20round%2C%20clockwise.%20%60polygon%60%20joins%20the%20dots.%20%60hand%60%20is%20the%20number%20of%20cycles%20in%20which%20a%0A%20%20%20%20clock%20hand%20goes%20round%20once%3B%20%60lit%60%20is%20a%20list%20of%20(from%2C%20to%2C%20turn)%20that%20rings%20a%20spot%20while%20it%20sounds.%22%22%22%0A%20%20%20%20centre%2C%20radius%20%3D%20size%20%2F%202%2C%20size%20%2F%202%20-%2036%0A%0A%20%20%20%20def%20at(turn%2C%20r%3Dradius)%3A%0A%20%20%20%20%20%20%20%20angle%20%3D%202%20*%20math.pi%20*%20turn%0A%20%20%20%20%20%20%20%20return%20centre%20%2B%20r%20*%20math.sin(angle)%2C%20centre%20-%20r%20*%20math.cos(angle)%0A%0A%20%20%20%20parts%20%3D%20%5Bf%22%3Ccircle%20cx%3D'%7Bcentre%7D'%20cy%3D'%7Bcentre%7D'%20r%3D'%7Bradius%7D'%20fill%3D'none'%20stroke%3D'var(--grid)'%2F%3E%22%5D%0A%20%20%20%20for%20tick%20in%20range(ticks)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(tick%20%2F%20ticks)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'2'%20fill%3D'var(--muted)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20if%20tick_labels%20and%20tick_labels%5Btick%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(tick%20%2F%20ticks%2C%20radius%20%2B%2020)%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx%3A.1f%7D'%20y%3D'%7By%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%3E%7Btick_labels%5Btick%5D%7D%3C%2Ftext%3E%22)%0A%20%20%20%20if%20polygon%20and%20len(points)%20%3E%201%3A%0A%20%20%20%20%20%20%20%20outline%20%3D%20%22%20%22.join(f%22%7Bx%3A.1f%7D%2C%7By%3A.1f%7D%22%20for%20x%2C%20y%20in%20(at(point%5B0%5D)%20for%20point%20in%20points))%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolygon%20points%3D'%7Boutline%7D'%20fill%3D'var(--s1)'%20fill-opacity%3D'.1'%20stroke%3D'var(--s1)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-width%3D'2'%20stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20turn%2C%20colour%2C%20*tip%20in%20points%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(turn)%0A%20%20%20%20%20%20%20%20title%20%3D%20f%22%3Ctitle%3E%7Btip%5B0%5D%7D%3C%2Ftitle%3E%22%20if%20tip%20else%20%22%22%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'5'%20fill%3D'var(--%7Bcolour%7D)'%20stroke%3D'var(--surface)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke-width%3D'2'%3E%7Btitle%7D%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20marks%20%3D%20%5Bcursor.dot(t0%2C%20t1%2C%20*at(turn)%2C%20r%3D9)%20for%20t0%2C%20t1%2C%20turn%20in%20lit%5D%0A%20%20%20%20if%20hand%3A%0A%20%20%20%20%20%20%20%20marks.append(cursor.hand(0%2C%20hand%2C%20cx%3Dcentre%2C%20cy%3Dcentre%2C%20r%3Dradius%20%2B%208%2C%20every%3Dhand))%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bsize%7D'%20viewBox%3D'0%200%20%7Bsize%7D%20%7Bsize%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20key%2C%20marks)%0A%0A%0Adef%20curve(values%2C%20marks%3D()%2C%20width%3D600%2C%20height%3D140%2C%20span%3DNone)%3A%0A%20%20%20%20%22%22%22A%20melody%20as%20a%20stepped%20line%2C%20one%20step%20per%20note.%20%60marks%60%20are%20the%20indexes%20of%20notes%20to%20dot.%0A%20%20%20%20%60span%60%20is%20the%20(from%2C%20to)%20cycles%20in%20which%20the%20cursor%20crosses%20the%20whole%20line.%22%22%22%0A%20%20%20%20low%2C%20high%20%3D%20min(values)%2C%20max(values)%0A%20%20%20%20step%20%3D%20(width%20-%2028)%20%2F%20len(values)%0A%0A%20%20%20%20def%20x(i)%3A%0A%20%20%20%20%20%20%20%20return%2028%20%2B%20i%20*%20step%0A%0A%20%20%20%20def%20y(value)%3A%0A%20%20%20%20%20%20%20%20return%208%20%2B%20(height%20-%2016)%20*%20(high%20-%20value)%20%2F%20((high%20-%20low)%20or%201)%0A%0A%20%20%20%20path%20%3D%20%22%22.join(f%22%7B'L'%20if%20i%20else%20'M'%7D%7Bx(i)%3A.1f%7D%2C%7By(v)%3A.1f%7DH%7Bx(i%20%2B%201)%3A.1f%7D%22%20for%20i%2C%20v%20in%20enumerate(values))%0A%20%20%20%20parts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'20'%20y%3D'%7By(high)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E%7Bhigh%7D%3C%2Ftext%3E%22%2C%0A%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'20'%20y%3D'%7By(low)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E%7Blow%7D%3C%2Ftext%3E%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20if%20low%20%3C%200%20%3C%20high%3A%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Cline%20x1%3D'28'%20x2%3D'%7Bwidth%7D'%20y1%3D'%7By(0)%3A.1f%7D'%20y2%3D'%7By(0)%3A.1f%7D'%20stroke%3D'var(--grid)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'20'%20y%3D'%7By(0)%20%2B%204%3A.0f%7D'%20text-anchor%3D'end'%3E0%3C%2Ftext%3E%22)%0A%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20f%22%3Cpath%20d%3D'%7Bpath%7D'%20fill%3D'none'%20stroke%3D'var(--s1)'%20stroke-width%3D'2'%20stroke-linejoin%3D'round'%20%22%0A%20%20%20%20%20%20%20%20%22stroke-linecap%3D'round'%2F%3E%22%0A%20%20%20%20)%0A%20%20%20%20for%20i%20in%20marks%3A%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx(i%20%2B%200.5)%3A.1f%7D'%20cy%3D'%7By(values%5Bi%5D)%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s2)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--surface)'%20stroke-width%3D'2'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20i%2C%20value%20in%20enumerate(values)%3A%20%20%23%20invisible%20columns%20that%20carry%20the%20tooltips%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bx(i)%3A.1f%7D'%20y%3D'0'%20width%3D'%7Bstep%3A.1f%7D'%20height%3D'%7Bheight%7D'%20fill%3D'transparent'%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctitle%3Enote%20%7Bi%7D%3A%20%7Bvalue%7D%3C%2Ftitle%3E%3C%2Frect%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20sweep%20%3D%20%5Bcursor.sweep(*span%2C%20x0%3D28%2C%20x1%3Dwidth%2C%20top%3D0%2C%20bottom%3Dheight)%5D%20if%20span%20else%20%5B%5D%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bheight%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20sweep)%0A%0A%0Adef%20cloud(dots%2C%20x_range%2C%20marker%3DNone%2C%20marker_dots%3D()%2C%20ticks%3D()%2C%20width%3D600%2C%20height%3D220%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22Many%20small%20dots%20(x%2C%20y)%2C%20with%20y%20from%200%20to%201.%20%60marker%60%20draws%20a%20vertical%20line%20at%20that%20x%2C%0A%20%20%20%20carrying%20the%20bigger%20%60marker_dots%60%20(y%20values).%20%60each%60%20is%20how%20many%20cycles%20each%20of%20those%20lasts%0A%20%20%20%20when%20they%20are%20played%20in%20order.%22%22%22%0A%20%20%20%20x0%2C%20x1%20%3D%20x_range%0A%0A%20%20%20%20def%20px(x)%3A%0A%20%20%20%20%20%20%20%20return%20(x%20-%20x0)%20%2F%20(x1%20-%20x0)%20*%20width%0A%0A%20%20%20%20def%20py(y)%3A%0A%20%20%20%20%20%20%20%20return%206%20%2B%20(1%20-%20y)%20*%20(height%20-%2012)%0A%0A%20%20%20%20seen%20%3D%20sorted(%7B(round(px(x))%2C%20round(py(y)))%20for%20x%2C%20y%20in%20dots%7D)%0A%20%20%20%20parts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%3Cpath%20d%3D'%7B''.join(f'M%7Bx%7D%2C%7By%7Dh.01'%20for%20x%2C%20y%20in%20seen)%7D'%20fill%3D'none'%20stroke%3D'var(--muted)'%20%22%0A%20%20%20%20%20%20%20%20%22stroke-width%3D'1.6'%20stroke-linecap%3D'round'%2F%3E%22%2C%0A%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'0'%20x2%3D'%7Bwidth%7D'%20y1%3D'%7Bheight%7D'%20y2%3D'%7Bheight%7D'%20stroke%3D'var(--grid)'%2F%3E%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20for%20tick%20in%20ticks%3A%0A%20%20%20%20%20%20%20%20anchor%20%3D%20%22start%22%20if%20px(tick)%20%3C%2012%20else%20%22end%22%20if%20px(tick)%20%3E%20width%20-%2012%20else%20%22middle%22%20%20%23%20keep%20edge%20labels%20inside%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bpx(tick)%3A.0f%7D'%20y%3D'%7Bheight%20%2B%2014%7D'%20text-anchor%3D'%7Banchor%7D'%3E%7Btick%3Ag%7D%3C%2Ftext%3E%22)%0A%20%20%20%20marks%20%3D%20%5B%5D%0A%20%20%20%20if%20marker%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'%7Bpx(marker)%3A.1f%7D'%20x2%3D'%7Bpx(marker)%3A.1f%7D'%20y1%3D'0'%20y2%3D'%7Bheight%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--s2)'%20stroke-width%3D'2'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20value%20in%20sorted(set(round(value%2C%203)%20for%20value%20in%20marker_dots))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bpx(marker)%3A.1f%7D'%20cy%3D'%7Bpy(value)%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s2)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke%3D'var(--surface)'%20stroke-width%3D'2'%3E%3Ctitle%3Ex%20%3D%20%7Bvalue%3A.3f%7D%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20marks%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cursor.dot(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20x%3Dpx(marker)%2C%20y%3Dpy(value))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20k%2C%20value%20in%20enumerate(marker_dots)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bheight%20%2B%2018%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20braid(rows%2C%20lead%3DNone%2C%20width%3D600%2C%20per_cycle%3DNone%2C%20rests%3D0)%3A%0A%20%20%20%20%22%22%22Change-ringing%20rows%20as%20paths%3A%20one%20line%20per%20bell%2C%20rows%20left%20to%20right%2C%20first%20place%20at%20the%20top.%0A%20%20%20%20With%20%60per_cycle%60%20rows%20and%20%60rests%60%20silent%20steps%20in%20each%20cycle%2C%20the%20cursor%20rings%20each%20bell%20as%20it%20sounds.%22%22%22%0A%20%20%20%20step%20%3D%20(width%20-%2036)%20%2F%20(len(rows)%20-%201)%0A%20%20%20%20places%20%3D%20len(rows%5B0%5D)%0A%0A%20%20%20%20def%20x(column)%3A%0A%20%20%20%20%20%20%20%20return%2018%20%2B%20column%20*%20step%0A%0A%20%20%20%20def%20y(place)%3A%0A%20%20%20%20%20%20%20%20return%2024%20%2B%20place%20*%2024%0A%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20if%20lead%3A%0A%20%20%20%20%20%20%20%20for%20column%20in%20range(0%2C%20len(rows)%20-%201%2C%20lead)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cline%20x1%3D'%7Bx(column)%3A.1f%7D'%20x2%3D'%7Bx(column)%3A.1f%7D'%20y1%3D'14'%20y2%3D'%7By(places%20-%201)%20%2B%2010%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--grid)'%2F%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx(column)%20%2B%204%3A.1f%7D'%20y%3D'10'%3Elead%20%7Bcolumn%20%2F%2F%20lead%20%2B%201%7D%3C%2Ftext%3E%22)%0A%20%20%20%20for%20bell%20in%20sorted(rows%5B0%5D)%3A%0A%20%20%20%20%20%20%20%20points%20%3D%20%5B(x(column)%2C%20y(row.index(bell)))%20for%20column%2C%20row%20in%20enumerate(rows)%5D%0A%20%20%20%20%20%20%20%20line%20%3D%20%22%20%22.join(f%22%7Bpx%3A.1f%7D%2C%7Bpy%3A.1f%7D%22%20for%20px%2C%20py%20in%20points)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolyline%20points%3D'%7Bline%7D'%20fill%3D'none'%20stroke%3D'var(--s%7Bbell%7D)'%20stroke-width%3D'2'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-linejoin%3D'round'%20stroke-linecap%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20column%2C%20(px%2C%20py)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bpx%3A.1f%7D'%20cy%3D'%7Bpy%3A.1f%7D'%20r%3D'4'%20fill%3D'var(--s%7Bbell%7D)'%20stroke%3D'var(--surface)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22stroke-width%3D'2'%3E%3Ctitle%3Erow%20%7Bcolumn%20%2B%201%7D%3A%20bell%20%7Bbell%7D%20in%20place%20%7Brows%5Bcolumn%5D.index(bell)%20%2B%201%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'4'%20y%3D'%7Bpoints%5B0%5D%5B1%5D%20%2B%204%3A.0f%7D'%20text-anchor%3D'middle'%3E%7Bbell%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bwidth%20-%204%7D'%20y%3D'%7Bpoints%5B-1%5D%5B1%5D%20%2B%204%3A.0f%7D'%20text-anchor%3D'middle'%3E%7Bbell%7D%3C%2Ftext%3E%22)%0A%20%20%20%20if%20per_cycle%3A%0A%20%20%20%20%20%20%20%20beats%20%3D%20per_cycle%20*%20places%20%2B%20rests%20%20%23%20steps%20in%20one%20cycle%0A%20%20%20%20%20%20%20%20for%20column%20in%20range(len(rows))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20place%20in%20range(places)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20column%20%2F%2F%20per_cycle%20%2B%20((column%20%25%20per_cycle)%20*%20places%20%2B%20place)%20%2F%20beats%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.dot(start%2C%20start%20%2B%201%20%2F%20beats%2C%20x%3Dx(column)%2C%20y%3Dy(place)))%0A%20%20%20%20h%20%3D%20y(places%20-%201)%20%2B%2016%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bh%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20lattice(triangles%2C%20vertices%2C%20width%3D760%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22A%20walk%20on%20a%20triangular%20grid.%20%60triangles%60%20is%20%5B(three%20(i%2C%20j)%20corners%2C%20label)%5D%20in%20walk%20order%20and%0A%20%20%20%20%60vertices%60%20maps%20(i%2C%20j)%20to%20the%20label%20drawn%20at%20that%20grid%20point.%20%60each%60%20is%20how%20many%20cycles%20each%0A%20%20%20%20triangle%20lasts%3A%20the%20cursor%20outlines%20the%20one%20that%20is%20sounding.%22%22%22%0A%0A%20%20%20%20def%20flat(i%2C%20j)%3A%20%20%23%20grid%20point%20to%20the%20plane%3A%2060-degree%20rows%0A%20%20%20%20%20%20%20%20return%20i%20%2B%20j%20%2F%202%2C%20-j%20*%20math.sqrt(3)%20%2F%202%0A%0A%20%20%20%20spots%20%3D%20%5Bflat(*corner)%20for%20corner%20in%20vertices%5D%0A%20%20%20%20x0%2C%20x1%20%3D%20min(x%20for%20x%2C%20_%20in%20spots)%2C%20max(x%20for%20x%2C%20_%20in%20spots)%0A%20%20%20%20y0%2C%20y1%20%3D%20min(y%20for%20_%2C%20y%20in%20spots)%2C%20max(y%20for%20_%2C%20y%20in%20spots)%0A%20%20%20%20unit%20%3D%20min(64%2C%20(width%20-%2036)%20%2F%20(x1%20-%20x0))%0A%0A%20%20%20%20def%20at(i%2C%20j)%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20flat(i%2C%20j)%0A%20%20%20%20%20%20%20%20return%2018%20%2B%20(x%20-%20x0)%20*%20unit%2C%2018%20%2B%20(y%20-%20y0)%20*%20unit%0A%0A%20%20%20%20parts%2C%20marks%2C%20seen%20%3D%20%5B%5D%2C%20%5B%5D%2C%20set()%0A%20%20%20%20for%20index%2C%20(three%2C%20label)%20in%20enumerate(triangles)%3A%0A%20%20%20%20%20%20%20%20points%20%3D%20%5Bat(*corner)%20for%20corner%20in%20three%5D%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.outline(index%20*%20each%2C%20(index%20%2B%201)%20*%20each%2C%20points))%0A%20%20%20%20%20%20%20%20if%20frozenset(three)%20in%20seen%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20seen.add(frozenset(three))%0A%20%20%20%20%20%20%20%20cx%2C%20cy%20%3D%20sum(x%20for%20x%2C%20_%20in%20points)%20%2F%203%2C%20sum(y%20for%20_%2C%20y%20in%20points)%20%2F%203%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolygon%20points%3D'%7B'%20'.join(f'%7Bx%3A.1f%7D%2C%7By%3A.1f%7D'%20for%20x%2C%20y%20in%20points)%7D'%20fill%3D'var(--s1)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22fill-opacity%3D'%7B0.45%20if%20index%20%3D%3D%200%20else%200.14%7D'%20stroke%3D'var(--surface)'%20stroke-width%3D'2'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctext%20x%3D'%7Bcx%3A.1f%7D'%20y%3D'%7Bcy%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%20style%3D'fill%3Avar(--ink)'%3E%7Blabel%7D%3C%2Ftext%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20corner%2C%20label%20in%20vertices.items()%3A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20at(*corner)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'9'%20fill%3D'var(--surface)'%2F%3E%22)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bx%3A.1f%7D'%20y%3D'%7By%20%2B%204%3A.1f%7D'%20text-anchor%3D'middle'%20style%3D'font-size%3A10px'%3E%7Blabel%7D%3C%2Ftext%3E%22)%0A%20%20%20%20w%2C%20h%20%3D%2036%20%2B%20(x1%20-%20x0)%20*%20unit%2C%2036%20%2B%20(y1%20-%20y0)%20*%20unit%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bw%3A.0f%7D'%20viewBox%3D'0%200%20%7Bw%3A.0f%7D%20%7Bh%3A.0f%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0A%0Adef%20ink(visited%2C%20size%3D300)%3A%0A%20%20%20%20%22%22%22Count%20how%20often%20each%20pixel%20of%20the%20square%20from%20-1%20to%201%20is%20visited%2C%20and%20return%20the%20counts%20as%20a%0A%20%20%20%20greyscale%20PNG%20(a%20data%20URI)%3A%20the%20more%20visits%2C%20the%20brighter.%20%60visited%60%20is%20a%20list%20of%20(x%2C%20y)%20arrays.%22%22%22%0A%20%20%20%20counts%20%3D%20np.zeros(size%20*%20size)%0A%20%20%20%20for%20x%2C%20y%20in%20visited%3A%0A%20%20%20%20%20%20%20%20column%20%3D%20np.minimum(((x%20%2B%201)%20%2F%202%20*%20size).astype(int)%2C%20size%20-%201)%0A%20%20%20%20%20%20%20%20row%20%3D%20np.minimum(((1%20-%20y)%20%2F%202%20*%20size).astype(int)%2C%20size%20-%201)%0A%20%20%20%20%20%20%20%20counts%20%2B%3D%20np.bincount(row%20*%20size%20%2B%20column%2C%20minlength%3Dsize%20*%20size)%0A%20%20%20%20counts%20%3D%20np.log1p(counts)%0A%20%20%20%20grey%20%3D%20(255%20*%20(counts%20%2F%20(counts.max()%20or%201))%20**%200.5).astype(np.uint8).reshape(size%2C%20size)%0A%0A%20%20%20%20def%20chunk(tag%2C%20data)%3A%0A%20%20%20%20%20%20%20%20return%20struct.pack(%22%3EI%22%2C%20len(data))%20%2B%20tag%20%2B%20data%20%2B%20struct.pack(%22%3EI%22%2C%20zlib.crc32(tag%20%2B%20data))%0A%0A%20%20%20%20png%20%3D%20(%0A%20%20%20%20%20%20%20%20b%22%5Cx89PNG%5Cr%5Cn%5Cx1a%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IHDR%22%2C%20struct.pack(%22%3EIIBBBBB%22%2C%20size%2C%20size%2C%208%2C%200%2C%200%2C%200%2C%200))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IDAT%22%2C%20zlib.compress(b%22%22.join(b%22%5Cx00%22%20%2B%20row.tobytes()%20for%20row%20in%20grey)%2C%209))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IEND%22%2C%20b%22%22)%0A%20%20%20%20)%0A%20%20%20%20return%20%22data%3Aimage%2Fpng%3Bbase64%2C%22%20%2B%20base64.b64encode(png).decode()%0A%0A%0Adef%20tiles(images%2C%20paths%2C%20columns%3D4%2C%20width%3D760%2C%20gap%3D6%2C%20each%3DNone)%3A%0A%20%20%20%20%22%22%22A%20grid%20of%20square%20pictures%20of%20the%20plane%20from%20-1%20to%201%2C%20each%20with%20a%20path%20of%20dots%20drawn%20over%20it.%0A%20%20%20%20%60images%60%20are%20greyscale%20data%20URIs%2C%20shown%20as%20ink%20in%20the%20theme's%20colour%3B%20%60paths%60%20are%20lists%20of%20(x%2C%20y).%0A%20%20%20%20%60each%60%20is%20how%20many%20cycles%20each%20picture%20lasts%3A%20the%20cursor%20frames%20it%20and%20follows%20its%20path.%22%22%22%0A%20%20%20%20side%20%3D%20(width%20-%20(columns%20-%201)%20*%20gap)%20%2F%20columns%0A%20%20%20%20mask%20%3D%20f%22mm-%7Buuid.uuid4().hex%5B%3A8%5D%7D%22%0A%20%20%20%20parts%2C%20marks%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20k%2C%20(image%2C%20path)%20in%20enumerate(zip(images%2C%20paths))%3A%0A%20%20%20%20%20%20%20%20left%2C%20top%20%3D%20(k%20%25%20columns)%20*%20(side%20%2B%20gap)%2C%20(k%20%2F%2F%20columns)%20*%20(side%20%2B%20gap)%0A%20%20%20%20%20%20%20%20points%20%3D%20%5B(left%20%2B%20(x%20%2B%201)%20%2F%202%20*%20side%2C%20top%20%2B%20(1%20-%20y)%20%2F%202%20*%20side)%20for%20x%2C%20y%20in%20path%5D%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20height%3D'%7Bside%3A.1f%7D'%20rx%3D'4'%20fill%3D'var(--panel)'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cmask%20id%3D'%7Bmask%7D-%7Bk%7D'%3E%3Cimage%20href%3D'%7Bimage%7D'%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22height%3D'%7Bside%3A.1f%7D'%2F%3E%3C%2Fmask%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Crect%20x%3D'%7Bleft%3A.1f%7D'%20y%3D'%7Btop%3A.1f%7D'%20width%3D'%7Bside%3A.1f%7D'%20height%3D'%7Bside%3A.1f%7D'%20fill%3D'var(--glow)'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22mask%3D'url(%23%7Bmask%7D-%7Bk%7D)'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cpolyline%20points%3D'%7B'%20'.join(f'%7Bx%3A.1f%7D%2C%7By%3A.1f%7D'%20for%20x%2C%20y%20in%20points)%7D'%20fill%3D'none'%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22stroke%3D'var(--s2)'%20stroke-width%3D'.75'%20stroke-opacity%3D'.3'%20stroke-linejoin%3D'round'%2F%3E%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20step%2C%20(x%2C%20y)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ccircle%20cx%3D'%7Bx%3A.1f%7D'%20cy%3D'%7By%3A.1f%7D'%20r%3D'2.5'%20fill%3D'var(--s2)'%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Ctitle%3Epicture%20%7Bk%20%2B%201%7D%2C%20step%20%7Bstep%20%2B%201%7D%3C%2Ftitle%3E%3C%2Fcircle%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20parts.append(f%22%3Ctext%20x%3D'%7Bleft%20%2B%207%3A.1f%7D'%20y%3D'%7Btop%20%2B%2015%3A.1f%7D'%3E%7Bk%20%2B%201%7D%3C%2Ftext%3E%22)%0A%20%20%20%20%20%20%20%20if%20each%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20box%20%3D%20%5B(left%2C%20top)%2C%20(left%20%2B%20side%2C%20top)%2C%20(left%20%2B%20side%2C%20top%20%2B%20side)%2C%20(left%2C%20top%20%2B%20side)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.outline(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20box))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20step%2C%20(x%2C%20y)%20in%20enumerate(points)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20(k%20%2B%20step%20%2F%20len(points))%20*%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marks.append(cursor.dot(start%2C%20start%20%2B%20each%20%2F%20len(points)%2C%20x%3Dx%2C%20y%3Dy))%0A%20%20%20%20rows%20%3D%20-(-len(images)%20%2F%2F%20columns)%0A%20%20%20%20h%20%3D%20rows%20*%20side%20%2B%20(rows%20-%201)%20*%20gap%0A%20%20%20%20return%20figure(f%22%3Csvg%20width%3D'%7Bwidth%7D'%20viewBox%3D'0%200%20%7Bwidth%7D%20%7Bh%3A.0f%7D'%3E%7B''.join(parts)%7D%3C%2Fsvg%3E%22%2C%20None%2C%20marks)%0A%0Aclass%20LogisticPicker(anywidget.AnyWidget)%3A%0A%20%20%20%20%22%22%22The%20logistic%20map's%20bifurcation%20diagram%2C%20with%20an%20orange%20line%20you%20drag%20sideways%20to%20pick%20r.%0A%0A%20%20%20%20The%20line%20and%20the%20orbit's%20dots%20move%20in%20the%20browser%20as%20you%20drag%3B%20%60r%60%20is%20sent%20to%20Python%0A%20%20%20%20when%20you%20let%20go.%20%60picture(orbit%2C%20each)%60%20is%20the%20Picture%20a%20Player%20follows%3A%20its%20cursor%0A%20%20%20%20rings%20the%20orbit's%20dots%20on%20this%20plot%2C%20one%20after%20another.%0A%20%20%20%20%22%22%22%0A%0A%20%20%20%20_esm%20%3D%20r%22%22%22%0A%20%20%20%20function%20render(%7B%20model%2C%20el%20%7D)%20%7B%0A%20%20%20%20%20%20const%20NS%20%3D%20%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3B%0A%20%20%20%20%20%20const%20root%20%3D%20document.createElement(%22div%22)%3B%0A%20%20%20%20%20%20root.className%20%3D%20%22lp%22%3B%0A%20%20%20%20%20%20root.id%20%3D%20model.get(%22target%22)%3B%0A%20%20%20%20%20%20root.innerHTML%20%3D%20model.get(%22html%22)%3B%0A%20%20%20%20%20%20el.appendChild(root)%3B%0A%20%20%20%20%20%20const%20svg%20%3D%20root.querySelector(%22svg%22)%3B%0A%20%20%20%20%20%20const%20%5Blo%2C%20hi%5D%20%3D%20model.get(%22x_range%22)%2C%20%5BrMin%2C%20rMax%2C%20step%5D%20%3D%20model.get(%22limits%22)%3B%0A%20%20%20%20%20%20const%20W%20%3D%20model.get(%22width%22)%2C%20H%20%3D%20model.get(%22height%22)%3B%0A%20%20%20%20%20%20const%20px%20%3D%20r%20%3D%3E%20(r%20-%20lo)%20%2F%20(hi%20-%20lo)%20*%20W%2C%20py%20%3D%20x%20%3D%3E%206%20%2B%20(1%20-%20x)%20*%20(H%20-%2012)%3B%0A%20%20%20%20%20%20const%20layer%20%3D%20document.createElementNS(NS%2C%20%22g%22)%3B%0A%20%20%20%20%20%20svg.appendChild(layer)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22tabindex%22%2C%20%220%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22role%22%2C%20%22slider%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-label%22%2C%20%22r%3A%20drag%20the%20orange%20line%22)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-valuemin%22%2C%20rMin)%3B%0A%20%20%20%20%20%20svg.setAttribute(%22aria-valuemax%22%2C%20rMax)%3B%0A%0A%20%20%20%20%20%20let%20r%20%3D%20model.get(%22r%22)%2C%20dragging%20%3D%20false%3B%0A%20%20%20%20%20%20function%20orbit(r)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20the%20same%20steps%20as%20in%20Python%0A%20%20%20%20%20%20%20%20let%20x%20%3D%200.3%3B%20const%20out%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20%20%20for%20(let%20i%20%3D%200%3B%20i%20%3C%20300%20%2B%2032%3B%20i%2B%2B)%20%7B%20x%20%3D%20r%20*%20x%20*%20(1%20-%20x)%3B%20if%20(i%20%3E%3D%20300)%20out.push(x)%3B%20%7D%0A%20%20%20%20%20%20%20%20return%20%5B...new%20Set(out.map(v%20%3D%3E%20v.toFixed(3)))%5D.map(Number)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20function%20draw()%20%7B%0A%20%20%20%20%20%20%20%20const%20x%20%3D%20px(r)%3B%0A%20%20%20%20%20%20%20%20const%20dots%20%3D%20orbit(r).map(v%20%3D%3E%0A%20%20%20%20%20%20%20%20%20%20%60%3Ccircle%20cx%3D%22%24%7Bx%7D%22%20cy%3D%22%24%7Bpy(v)%7D%22%20r%3D%224%22%20fill%3D%22var(--s2)%22%20stroke%3D%22var(--surface)%22%20stroke-width%3D%222%22%2F%3E%60).join(%22%22)%3B%0A%20%20%20%20%20%20%20%20layer.innerHTML%20%3D%0A%20%20%20%20%20%20%20%20%20%20%60%3Cline%20x1%3D%22%24%7Bx%7D%22%20x2%3D%22%24%7Bx%7D%22%20y1%3D%220%22%20y2%3D%22%24%7BH%7D%22%20stroke%3D%22var(--s2)%22%20stroke-width%3D%222%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Cline%20x1%3D%22%24%7Bx%7D%22%20x2%3D%22%24%7Bx%7D%22%20y1%3D%220%22%20y2%3D%22%24%7BH%7D%22%20stroke%3D%22transparent%22%20stroke-width%3D%2218%22%20style%3D%22cursor%3Aew-resize%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20dots%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Crect%20x%3D%22%24%7Bx%20-%2030%7D%22%20y%3D%22-22%22%20width%3D%2260%22%20height%3D%2218%22%20rx%3D%229%22%20fill%3D%22var(--s2)%22%20style%3D%22cursor%3Aew-resize%22%2F%3E%60%20%2B%0A%20%20%20%20%20%20%20%20%20%20%60%3Ctext%20x%3D%22%24%7Bx%7D%22%20y%3D%22-9%22%20text-anchor%3D%22middle%22%20style%3D%22fill%3A%23fff%3Bfont-weight%3A600%22%3Er%20%3D%20%24%7Br.toFixed(3).replace(%2F0%2B%24%2F%2C%20%22%22).replace(%2F%5C.%24%2F%2C%20%22%22)%7D%3C%2Ftext%3E%60%3B%0A%20%20%20%20%20%20%20%20svg.setAttribute(%22aria-valuenow%22%2C%20r)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20const%20clamp%20%3D%20v%20%3D%3E%20Math.min(rMax%2C%20Math.max(rMin%2C%20Math.round(v%20%2F%20step)%20*%20step))%3B%0A%20%20%20%20%20%20function%20fromPointer(e)%20%7B%0A%20%20%20%20%20%20%20%20const%20pt%20%3D%20svg.createSVGPoint()%3B%0A%20%20%20%20%20%20%20%20pt.x%20%3D%20e.clientX%3B%20pt.y%20%3D%20e.clientY%3B%0A%20%20%20%20%20%20%20%20const%20p%20%3D%20pt.matrixTransform(svg.getScreenCTM().inverse())%3B%0A%20%20%20%20%20%20%20%20r%20%3D%20clamp(lo%20%2B%20p.x%20%2F%20W%20*%20(hi%20-%20lo))%3B%0A%20%20%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20function%20commit()%20%7B%0A%20%20%20%20%20%20%20%20if%20(Math.abs(r%20-%20model.get(%22r%22))%20%3E%201e-9)%20%7B%20model.set(%22r%22%2C%20r)%3B%20model.save_changes()%3B%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20svg.addEventListener(%22pointerdown%22%2C%20e%20%3D%3E%20%7B%20dragging%20%3D%20true%3B%20svg.setPointerCapture(e.pointerId)%3B%20fromPointer(e)%3B%20%7D)%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointermove%22%2C%20e%20%3D%3E%20%7B%20if%20(dragging)%20fromPointer(e)%3B%20%7D)%3B%0A%20%20%20%20%20%20const%20release%20%3D%20()%20%3D%3E%20%7B%20if%20(dragging)%20%7B%20dragging%20%3D%20false%3B%20commit()%3B%20%7D%20%7D%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointerup%22%2C%20release)%3B%0A%20%20%20%20%20%20svg.addEventListener(%22pointercancel%22%2C%20release)%3B%0A%20%20%20%20%20%20let%20keyTimer%3B%0A%20%20%20%20%20%20svg.addEventListener(%22keydown%22%2C%20e%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20const%20k%20%3D%20%7B%20ArrowLeft%3A%20-1%2C%20ArrowDown%3A%20-1%2C%20ArrowRight%3A%201%2C%20ArrowUp%3A%201%20%7D%5Be.key%5D%3B%0A%20%20%20%20%20%20%20%20if%20(!k)%20return%3B%0A%20%20%20%20%20%20%20%20e.preventDefault()%3B%0A%20%20%20%20%20%20%20%20r%20%3D%20clamp(r%20%2B%20k%20*%20step%20*%20(e.shiftKey%20%3F%2010%20%3A%201))%3B%0A%20%20%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20%20%20clearTimeout(keyTimer)%3B%20keyTimer%20%3D%20setTimeout(commit%2C%20300)%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20model.on(%22change%3Ar%22%2C%20()%20%3D%3E%20%7B%20if%20(!dragging)%20%7B%20r%20%3D%20model.get(%22r%22)%3B%20draw()%3B%20%7D%20%7D)%3B%0A%0A%20%20%20%20%20%20%2F%2F%20light%20or%20dark%2C%20from%20the%20text%20colour%20the%20page%20gives%20this%20widget%0A%20%20%20%20%20%20const%20theme%20%3D%20()%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20const%20m%20%3D%20getComputedStyle(el).color.match(%2F%5B%5Cd.%5D%2B%2Fg)%3B%0A%20%20%20%20%20%20%20%20root.classList.toggle(%22dark%22%2C%20!!m%20%26%26%200.299%20*%20m%5B0%5D%20%2B%200.587%20*%20m%5B1%5D%20%2B%200.114%20*%20m%5B2%5D%20%3E%20140)%3B%0A%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20theme()%3B%0A%20%20%20%20%20%20const%20themeTimer%20%3D%20setInterval(theme%2C%201500)%3B%0A%20%20%20%20%20%20draw()%3B%0A%20%20%20%20%20%20return%20()%20%3D%3E%20clearInterval(themeTimer)%3B%0A%20%20%20%20%7D%0A%20%20%20%20export%20default%20%7B%20render%20%7D%3B%0A%20%20%20%20%22%22%22%0A%20%20%20%20_css%20%3D%20%22%22%22%0A%20%20%20%20.lp%20%7B%20padding-top%3A%2024px%3B%20%7D%0A%20%20%20%20.lp%20svg%20%7B%20touch-action%3A%20none%3B%20cursor%3A%20crosshair%3B%20outline%3A%20none%3B%20%7D%0A%20%20%20%20.lp%20svg%3Afocus-visible%20%7B%20outline%3A%202px%20solid%20var(--s2)%3B%20outline-offset%3A%204px%3B%20border-radius%3A%204px%3B%20%7D%0A%20%20%20%20%22%22%22%0A%20%20%20%20r%20%3D%20traitlets.Float(3.5).tag(sync%3DTrue)%0A%20%20%20%20html%20%3D%20traitlets.Unicode(%22%22).tag(sync%3DTrue)%0A%20%20%20%20target%20%3D%20traitlets.Unicode(%22%22).tag(sync%3DTrue)%0A%20%20%20%20x_range%20%3D%20traitlets.List(%5B2.8%2C%204.0%5D).tag(sync%3DTrue)%0A%20%20%20%20limits%20%3D%20traitlets.List(%5B2.8%2C%203.99%2C%200.005%5D).tag(sync%3DTrue)%0A%20%20%20%20width%20%3D%20traitlets.Float(600).tag(sync%3DTrue)%0A%20%20%20%20height%20%3D%20traitlets.Float(220).tag(sync%3DTrue)%0A%0A%20%20%20%20def%20__init__(self%2C%20settled%2C%20r%3D3.5%2C%20x_range%3D(2.8%2C%204.0)%2C%20width%3D600%2C%20height%3D220)%3A%0A%20%20%20%20%20%20%20%20backdrop%20%3D%20cloud(settled%2C%20x_range%2C%20ticks%3D%5B2.8%2C%203.0%2C%203.2%2C%203.4%2C%203.6%2C%203.8%2C%204.0%5D%2C%20width%3Dwidth%2C%20height%3Dheight)%0A%20%20%20%20%20%20%20%20super().__init__(%0A%20%20%20%20%20%20%20%20%20%20%20%20r%3Dr%2C%20html%3Dbackdrop.html%2C%20target%3Df%22mm-%7Buuid.uuid4().hex%5B%3A10%5D%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x_range%3Dlist(x_range)%2C%20width%3Dwidth%2C%20height%3Dheight%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20picture(self%2C%20orbit%2C%20each)%3A%0A%20%20%20%20%20%20%20%20%22%22%22A%20Picture%20on%20this%20plot%20whose%20cursor%20rings%20each%20value%20of%20%60orbit%60%20in%20turn%2C%20%60each%60%20cycles%20apiece.%22%22%22%0A%20%20%20%20%20%20%20%20(lo%2C%20hi)%2C%20w%2C%20h%20%3D%20self.x_range%2C%20self.width%2C%20self.height%0A%20%20%20%20%20%20%20%20x%20%3D%20(self.r%20-%20lo)%20%2F%20(hi%20-%20lo)%20*%20w%0A%20%20%20%20%20%20%20%20marks%20%3D%20%5Bcursor.dot(k%20*%20each%2C%20(k%20%2B%201)%20*%20each%2C%20x%3Dx%2C%20y%3D6%20%2B%20(1%20-%20v)%20*%20(h%20-%2012))%20for%20k%2C%20v%20in%20enumerate(orbit)%5D%0A%20%20%20%20%20%20%20%20picture%20%3D%20Picture(%22%22%2C%20marks)%0A%20%20%20%20%20%20%20%20picture.id%20%3D%20self.target%20%20%23%20the%20player%20draws%20on%20this%20widget's%20plot%0A%20%20%20%20%20%20%20%20return%20picture