Harmony and tuning
Why twelve notes, and how chords move: stacked fifths that almost close, and walks on the grid of triads. 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 ])
Why twelve notes?
The sweetest interval after the octave (2:1) is the pure fifth (3:2). Stack fifths, fold
each one back into the octave, and you build scales the way the Pythagoreans did: 5 fifths
give a pentatonic scale, 7 a diatonic one. But the circle can never close, because no
power of 3/2 is a power of 2. It can only nearly close, when \(n\) fifths are almost \(m\)
octaves:
\[
\left(\tfrac{3}{2}\right)^{n} \approx 2^{m}
\quad\Longleftrightarrow\quad
\frac{m}{n} \approx \log_2 \tfrac{3}{2} = 0.58496\ldots
= \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{3 + \cdots}}}}}
\]
Cutting that continued fraction off early gives
the best such fractions: 1/2, 3/5, 7/12 , 24/41, 31/53. Twelve fifths miss seven
octaves by the Pythagorean comma ,
\[
\frac{(3/2)^{12}}{2^{7}} = \frac{3^{12}}{2^{19}} = 1.01364\ldots \approx 23.5 \ \text{cents, about a quarter of a semitone}
\]
and equal temperament hides it by shaving a little off every fifth. After twelve, the
next good stopping points are 41 and 53 notes per octave.
You hear the scale you have built, and then the first note together with the last fifth.
The closer the circle is to closing, the slower the two notes beat against each other.
fifths%20%3D%20mo.ui.slider(1%2C%2053%2C%20value%3D12%2C%20label%3D%22fifths%20stacked%22%2C%20show_value%3DTrue%2C%20debounce%3DTrue)%0Atuning_player%20%3D%20Player()%0Amo.vstack(%5Bfifths%2C%20mo.ui.anywidget(tuning_player)%5D)
Show the code 3 lines fifths = mo . ui . slider ( 1 , 53 , value = 12 , label = "fifths stacked" , show_value = True , debounce = True )
tuning_player = Player ()
mo . vstack ([ fifths , mo . ui . anywidget ( tuning_player )])
C D E F G A B fifth 1 fifth 2 fifth 3 fifth 4 fifth 5 fifth 6 fifth 7 fifth 8 fifth 9 fifth 10 fifth 11 the starting note fifth 12 start fifths the last fifth
12 pure fifths land 23.5 cents above the starting note. A semitone is 100 cents; the small grey ticks are today's 12 equal semitones.
Continued fraction of log₂(3/2): 1/2, 3/5, 7/12, 24/41, 31/53. Read 7/12 as: 12 fifths ≈ 7 octaves.
FIFTH%20%3D%20math.log2(3%20%2F%202)%20%20%23%20a%20pure%20fifth%20as%20a%20fraction%20of%20an%20octave%3A%200.58496...%0A%0A%0Adef%20convergents(x%2C%20count)%3A%0A%20%20%20%20%22%22%22The%20best%20fractions%20close%20to%20x%2C%20from%20its%20continued%20fraction.%22%22%22%0A%20%20%20%20found%2C%20(p0%2C%20q0%2C%20p1%2C%20q1)%20%3D%20%5B%5D%2C%20(0%2C%201%2C%201%2C%200)%0A%20%20%20%20for%20_%20in%20range(count)%3A%0A%20%20%20%20%20%20%20%20whole%20%3D%20math.floor(x)%0A%20%20%20%20%20%20%20%20p0%2C%20q0%2C%20p1%2C%20q1%20%3D%20p1%2C%20q1%2C%20whole%20*%20p1%20%2B%20p0%2C%20whole%20*%20q1%20%2B%20q0%0A%20%20%20%20%20%20%20%20found.append((p1%2C%20q1))%0A%20%20%20%20%20%20%20%20x%20%3D%201%20%2F%20(x%20-%20whole)%0A%20%20%20%20return%20found%0A%0A%0A_turns%20%3D%20%5B(k%20*%20FIFTH)%20%25%201%20for%20k%20in%20range(fifths.value%20%2B%201)%5D%20%20%23%20each%20fifth%2C%20folded%20into%20one%20octave%0A_gap%20%3D%20(_turns%5B-1%5D%20%2B%200.5)%20%25%201%20-%200.5%20%20%23%20how%20far%20the%20last%20one%20lands%20from%20the%20start%2C%20in%20octaves%0A%0A%23%20first%20the%20scale%3A%20every%20note%20so%20far%2C%20low%20to%20high.%20Then%20the%20start%20and%20the%20last%20fifth%20together.%0A_rising%20%3D%20sorted(_turns%5B%3A-1%5D)%0A_scale%20%3D%20note(steps(%5B12%20*%20turn%20-%2012%20for%20turn%20in%20_rising%5D)).s(%22pluck%22)%0A_pair%20%3D%20note(stack(-12%2C%2012%20*%20_gap%20-%2012)).s(%22sine%22).gain(0.8)%0A_bars%20%3D%20math.ceil(fifths.value%20%2F%2012)%0A%0A_dots%20%3D%20%5B(turn%2C%20%22s1%22%2C%20f%22fifth%20%7Bk%7D%22)%20for%20k%2C%20turn%20in%20enumerate(_turns)%5D%5B1%3A-1%5D%0A_dots%20%2B%3D%20%5B(0%2C%20%22ink%22%2C%20%22the%20starting%20note%22)%2C%20(_turns%5B-1%5D%2C%20%22s2%22%2C%20f%22fifth%20%7Bfifths.value%7D%22)%5D%0A_picture%20%3D%20ring(%0A%20%20%20%20_dots%2C%0A%20%20%20%20%5B%22C%22%2C%20%22%22%2C%20%22D%22%2C%20%22%22%2C%20%22E%22%2C%20%22F%22%2C%20%22%22%2C%20%22G%22%2C%20%22%22%2C%20%22A%22%2C%20%22%22%2C%20%22B%22%5D%2C%0A%20%20%20%20key%3D%5B(%22ink%22%2C%20%22start%22)%2C%20(%22s1%22%2C%20%22fifths%22)%2C%20(%22s2%22%2C%20%22the%20last%20fifth%22)%5D%2C%0A%20%20%20%20%23%20ring%20each%20note%20of%20the%20scale%20as%20it%20sounds%2C%20then%20the%20start%20and%20the%20last%20fifth%20together%0A%20%20%20%20lit%3D%5B(k%20*%20_bars%20%2F%20len(_rising)%2C%20(k%20%2B%201)%20*%20_bars%20%2F%20len(_rising)%2C%20turn)%20for%20k%2C%20turn%20in%20enumerate(_rising)%5D%0A%20%20%20%20%2B%20%5B(_bars%2C%20_bars%20%2B%201%2C%200)%2C%20(_bars%2C%20_bars%20%2B%201%2C%20_turns%5B-1%5D)%5D%2C%0A)%0Atuning_player.load(render(timecat((_bars%2C%20_scale)%2C%20(1%2C%20_pair)).slow(_bars%20%2B%201)%2C%20cycles%3D_bars%20%2B%201%2C%20cps%3D0.5)%2C%20_picture)%0A%0Amo.hstack(%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**%7Bfifths.value%7D%20pure%20fifths**%20land%20%7Babs(_gap)%20*%201200%3A.1f%7D%20cents%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B'above'%20if%20_gap%20%3E%200%20else%20'below'%7D%20the%20starting%20note.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22A%20semitone%20is%20100%20cents%3B%20the%20small%20grey%20ticks%20are%20today's%2012%20equal%20semitones.%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Continued%20fraction%20of%20log%E2%82%82(3%2F2)%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%7Bp%7D%2F%7Bq%7D%22%20for%20p%2C%20q%20in%20convergents(FIFTH%2C%207)%5B2%3A%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22.%20Read%207%2F12%20as%3A%2012%20fifths%20%E2%89%88%207%20octaves.%22%0A%20%20%20%20%20%20%20%20).style(%7B%22max-width%22%3A%20%22440px%22%7D)%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20justify%3D%22start%22%2C%0A%20%20%20%20align%3D%22center%22%2C%0A%20%20%20%20gap%3D2%2C%0A)
Show the code 51 lines FIFTH = math . log2 ( 3 / 2 ) # a pure fifth as a fraction of an octave: 0.58496...
def convergents ( x , count ):
"""The best fractions close to x, from its continued fraction."""
found , ( p0 , q0 , p1 , q1 ) = [], ( 0 , 1 , 1 , 0 )
for _ in range ( count ):
whole = math . floor ( x )
p0 , q0 , p1 , q1 = p1 , q1 , whole * p1 + p0 , whole * q1 + q0
found . append (( p1 , q1 ))
x = 1 / ( x - whole )
return found
_turns = [( k * FIFTH ) % 1 for k in range ( fifths . value + 1 )] # each fifth, folded into one octave
_gap = ( _turns [ - 1 ] + 0.5 ) % 1 - 0.5 # how far the last one lands from the start, in octaves
# first the scale: every note so far, low to high. Then the start and the last fifth together.
_rising = sorted ( _turns [: - 1 ])
_scale = note ( steps ([ 12 * turn - 12 for turn in _rising ])) . s ( "pluck" )
_pair = note ( stack ( - 12 , 12 * _gap - 12 )) . s ( "sine" ) . gain ( 0.8 )
_bars = math . ceil ( fifths . value / 12 )
_dots = [( turn , "s1" , f "fifth { k } " ) for k , turn in enumerate ( _turns )][ 1 : - 1 ]
_dots += [( 0 , "ink" , "the starting note" ), ( _turns [ - 1 ], "s2" , f "fifth { fifths . value } " )]
_picture = ring (
_dots ,
[ "C" , "" , "D" , "" , "E" , "F" , "" , "G" , "" , "A" , "" , "B" ],
key = [( "ink" , "start" ), ( "s1" , "fifths" ), ( "s2" , "the last fifth" )],
# ring each note of the scale as it sounds, then the start and the last fifth together
lit = [( k * _bars / len ( _rising ), ( k + 1 ) * _bars / len ( _rising ), turn ) for k , turn in enumerate ( _rising )]
+ [( _bars , _bars + 1 , 0 ), ( _bars , _bars + 1 , _turns [ - 1 ])],
)
tuning_player . load ( render ( timecat (( _bars , _scale ), ( 1 , _pair )) . slow ( _bars + 1 ), cycles = _bars + 1 , cps = 0.5 ), _picture )
mo . hstack (
[
_picture ,
mo . md (
f "** { fifths . value } pure fifths** land { abs ( _gap ) * 1200 : .1f } cents "
f " { 'above' if _gap > 0 else 'below' } the starting note. "
"A semitone is 100 cents; the small grey ticks are today's 12 equal semitones. \n\n "
"Continued fraction of log₂(3/2): "
+ ", " . join ( f " { p } / { q } " for p , q in convergents ( FIFTH , 7 )[ 2 :])
+ ". Read 7/12 as: 12 fifths ≈ 7 octaves."
) . style ({ "max-width" : "440px" }),
],
justify = "start" ,
align = "center" ,
gap = 2 ,
)
Walking the Tonnetz
Write the notes on a triangular grid so that a step to the right is a perfect fifth
(7 semitones) and a step up and to the right is a major third (4 semitones):
\[
\text{note}(i, j) = 7i + 4j \pmod{12}
\]
Every triangle is then a major or minor chord. Leonhard Euler drew this Tonnetz
("tone net") in 1739; since the 1980s
neo-Riemannian theory has used it
to study chord progressions.
Flipping a triangle over one of its edges keeps two notes and moves the third by a small
step. The three flips are called P , L and R . On a major chord with root \(r\) :
\[
\{r,\ r{+}4,\ r{+}7\} \ \xrightarrow{\ P\ }\ \{r,\ r{+}3,\ r{+}7\}, \qquad
\xrightarrow{\ L\ }\ \{r{-}1,\ r{+}4,\ r{+}7\}, \qquad
\xrightarrow{\ R\ }\ \{r,\ r{+}4,\ r{+}9\}
\]
A chord progression is a walk. Because note names repeat, the grid wraps around into a
torus, so walks come home: P and L alternating return after 6 chords, P and R after 8,
and R and L only after visiting all 24 major and minor chords. Richard Cohn's analysis of
the scherzo of Beethoven's Ninth finds 19 chords in a row along that last path.
The code keeps no table of chords. A chord is a triangle, and the notes are read off its
corners.
preset%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22RL%20%C3%97%209%3A%20the%20scherzo%20of%20Beethoven's%20Ninth%22%3A%20%22RL%22%20*%209%2C%0A%20%20%20%20%20%20%20%20%22RL%20%C3%97%2012%3A%20all%2024%20chords%22%3A%20%22RL%22%20*%2012%2C%0A%20%20%20%20%20%20%20%20%22PL%20%C3%97%203%3A%20home%20after%206%20chords%22%3A%20%22PL%22%20*%203%2C%0A%20%20%20%20%20%20%20%20%22PR%20%C3%97%204%3A%20home%20after%208%20chords%22%3A%20%22PR%22%20*%204%2C%0A%20%20%20%20%20%20%20%20%22LPR%20%C3%97%202%3A%20six%20chords%20around%20one%20note%22%3A%20%22LPR%22%20*%202%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20value%3D%22RL%20%C3%97%209%3A%20the%20scherzo%20of%20Beethoven's%20Ninth%22%2C%0A%20%20%20%20label%3D%22walk%22%2C%0A)%0Aword%20%3D%20mo.ui.text(placeholder%3D%22e.g.%20PLRLP%22%2C%20label%3D%22or%20type%20your%20own%2C%20with%20the%20letters%20P%2C%20L%20and%20R%22)%0Atonnetz_player%20%3D%20Player()%0Amo.vstack(%5Bmo.hstack(%5Bpreset%2C%20word%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5%2C%20wrap%3DTrue)%2C%20mo.ui.anywidget(tonnetz_player)%5D)
Show the code 14 lines preset = mo . ui . dropdown (
{
"RL × 9: the scherzo of Beethoven's Ninth" : "RL" * 9 ,
"RL × 12: all 24 chords" : "RL" * 12 ,
"PL × 3: home after 6 chords" : "PL" * 3 ,
"PR × 4: home after 8 chords" : "PR" * 4 ,
"LPR × 2: six chords around one note" : "LPR" * 2 ,
},
value = "RL × 9: the scherzo of Beethoven's Ninth" ,
label = "walk" ,
)
word = mo . ui . text ( placeholder = "e.g. PLRLP" , label = "or type your own, with the letters P, L and R" )
tonnetz_player = Player ()
mo . vstack ([ mo . hstack ([ preset , word ], justify = "start" , gap = 1.5 , wrap = True ), mo . ui . anywidget ( tonnetz_player )])
C Am F Dm B♭ Gm E♭ Cm A♭ Fm D♭ B♭m F♯ E♭m B A♭m E D♭m A C G E A F D B♭ G E♭ C A♭ F D♭ B♭ F♯ E♭ B A♭ E D♭ A C → Am → F → Dm → B♭ → Gm → E♭ → Cm → A♭ → Fm → D♭ → B♭m → F♯ → E♭m → B → A♭m → E → D♭m → A
19 different chords. The boldest triangle is the start.
FLIP%20%3D%20%7B%20%20%23%20which%20neighbour%20each%20letter%20flips%20to%0A%20%20%20%20(True%2C%20%22P%22)%3A%20(0%2C%20-1)%2C%20(True%2C%20%22R%22)%3A%20(-1%2C%200)%2C%20(True%2C%20%22L%22)%3A%20(0%2C%200)%2C%0A%20%20%20%20(False%2C%20%22P%22)%3A%20(0%2C%201)%2C%20(False%2C%20%22R%22)%3A%20(1%2C%200)%2C%20(False%2C%20%22L%22)%3A%20(0%2C%200)%2C%0A%7D%20%20%23%20fmt%3A%20skip%0ANOTES%20%3D%20%5B%22C%22%2C%20%22D%E2%99%AD%22%2C%20%22D%22%2C%20%22E%E2%99%AD%22%2C%20%22E%22%2C%20%22F%22%2C%20%22F%E2%99%AF%22%2C%20%22G%22%2C%20%22A%E2%99%AD%22%2C%20%22A%22%2C%20%22B%E2%99%AD%22%2C%20%22B%22%5D%0A%0A%0Adef%20pitch(i%2C%20j)%3A%0A%20%20%20%20%22%22%22The%20note%20at%20a%20grid%20point%3A%20a%20step%20right%20is%20a%20fifth%20(7%20semitones)%2C%20a%20step%20up-right%20a%20major%20third%20(4).%22%22%22%0A%20%20%20%20return%20(7%20*%20i%20%2B%204%20*%20j)%20%25%2012%0A%0A%0Adef%20corners(up%2C%20i%2C%20j)%3A%0A%20%20%20%20return%20%5B(i%2C%20j)%2C%20(i%20%2B%201%2C%20j)%2C%20(i%2C%20j%20%2B%201)%5D%20if%20up%20else%20%5B(i%20%2B%201%2C%20j)%2C%20(i%2C%20j%20%2B%201)%2C%20(i%20%2B%201%2C%20j%20%2B%201)%5D%0A%0A%0Adef%20walk(letters)%3A%0A%20%20%20%20%22%22%22Start%20on%20C%20major%20and%20flip%20once%20per%20letter.%22%22%22%0A%20%20%20%20up%2C%20i%2C%20j%20%3D%20True%2C%200%2C%200%0A%20%20%20%20path%20%3D%20%5B(up%2C%20i%2C%20j)%5D%0A%20%20%20%20for%20letter%20in%20letters%3A%0A%20%20%20%20%20%20%20%20di%2C%20dj%20%3D%20FLIP%5Bup%2C%20letter%5D%0A%20%20%20%20%20%20%20%20up%2C%20i%2C%20j%20%3D%20not%20up%2C%20i%20%2B%20di%2C%20j%20%2B%20dj%0A%20%20%20%20%20%20%20%20path.append((up%2C%20i%2C%20j))%0A%20%20%20%20return%20path%0A%0A%0A_letters%20%3D%20%5Bc%20for%20c%20in%20(word.value%20or%20preset.value).upper()%20if%20c%20in%20%22PLR%22%5D%5B%3A48%5D%0A_path%20%3D%20walk(_letters)%0A_chords%20%3D%20%5Bsorted(pitch(*corner)%20for%20corner%20in%20corners(*triangle))%20for%20triangle%20in%20_path%5D%0A_names%20%3D%20%5BNOTES%5Bpitch(i%2C%20j)%5D%20if%20up%20else%20NOTES%5Bpitch(i%2C%20j%20%2B%201)%5D%20%2B%20%22m%22%20for%20up%2C%20i%2C%20j%20in%20_path%5D%0A_roots%20%3D%20%5Bpitch(i%2C%20j)%20if%20up%20else%20pitch(i%2C%20j%20%2B%201)%20for%20up%2C%20i%2C%20j%20in%20_path%5D%0A%0A_cycles%20%3D%20math.ceil(len(_chords)%20%2F%202)%0A_harmony%20%3D%20stack(%0A%20%20%20%20note(steps(%5Bstack(*chord)%20for%20chord%20in%20_chords%5D)).s(%22superpiano%22).room(0.3)%2C%0A%20%20%20%20note(steps(%5Broot%20-%2024%20for%20root%20in%20_roots%5D)).s(%22bass%22).gain(0.9)%2C%0A).slow(_cycles)%0A%0A_picture%20%3D%20lattice(%0A%20%20%20%20%5B(corners(*triangle)%2C%20name)%20for%20triangle%2C%20name%20in%20zip(_path%2C%20_names)%5D%2C%0A%20%20%20%20%7Bcorner%3A%20NOTES%5Bpitch(*corner)%5D%20for%20triangle%20in%20_path%20for%20corner%20in%20corners(*triangle)%7D%2C%0A%20%20%20%20each%3D_cycles%20%2F%20len(_chords)%2C%20%20%23%20how%20long%20each%20chord%20lasts%2C%20in%20cycles%0A)%0Atonnetz_player.load(render(_harmony%2C%20cycles%3D_cycles%2C%20cps%3D0.5)%2C%20_picture)%0A%0A_home%20%3D%20_chords%5B-1%5D%20%3D%3D%20_chords%5B0%5D%20and%20len(_chords)%20%3E%201%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%22%20%E2%86%92%20%22.join(_names)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20f%22%5Cn%5Cn%7Blen(set(_names))%7D%20different%20chords%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%2C%20and%20the%20walk%20ends%20where%20it%20began.%22%20if%20_home%20else%20%22.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%20The%20boldest%20triangle%20is%20the%20start.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%0A)
Show the code 58 lines FLIP = { # which neighbour each letter flips to
( True , "P" ): ( 0 , - 1 ), ( True , "R" ): ( - 1 , 0 ), ( True , "L" ): ( 0 , 0 ),
( False , "P" ): ( 0 , 1 ), ( False , "R" ): ( 1 , 0 ), ( False , "L" ): ( 0 , 0 ),
} # fmt: skip
NOTES = [ "C" , "D♭" , "D" , "E♭" , "E" , "F" , "F♯" , "G" , "A♭" , "A" , "B♭" , "B" ]
def pitch ( i , j ):
"""The note at a grid point: a step right is a fifth (7 semitones), a step up-right a major third (4)."""
return ( 7 * i + 4 * j ) % 12
def corners ( up , i , j ):
return [( i , j ), ( i + 1 , j ), ( i , j + 1 )] if up else [( i + 1 , j ), ( i , j + 1 ), ( i + 1 , j + 1 )]
def walk ( letters ):
"""Start on C major and flip once per letter."""
up , i , j = True , 0 , 0
path = [( up , i , j )]
for letter in letters :
di , dj = FLIP [ up , letter ]
up , i , j = not up , i + di , j + dj
path . append (( up , i , j ))
return path
_letters = [ c for c in ( word . value or preset . value ) . upper () if c in "PLR" ][: 48 ]
_path = walk ( _letters )
_chords = [ sorted ( pitch ( * corner ) for corner in corners ( * triangle )) for triangle in _path ]
_names = [ NOTES [ pitch ( i , j )] if up else NOTES [ pitch ( i , j + 1 )] + "m" for up , i , j in _path ]
_roots = [ pitch ( i , j ) if up else pitch ( i , j + 1 ) for up , i , j in _path ]
_cycles = math . ceil ( len ( _chords ) / 2 )
_harmony = stack (
note ( steps ([ stack ( * chord ) for chord in _chords ])) . s ( "superpiano" ) . room ( 0.3 ),
note ( steps ([ root - 24 for root in _roots ])) . s ( "bass" ) . gain ( 0.9 ),
) . slow ( _cycles )
_picture = lattice (
[( corners ( * triangle ), name ) for triangle , name in zip ( _path , _names )],
{ corner : NOTES [ pitch ( * corner )] for triangle in _path for corner in corners ( * triangle )},
each = _cycles / len ( _chords ), # how long each chord lasts, in cycles
)
tonnetz_player . load ( render ( _harmony , cycles = _cycles , cps = 0.5 ), _picture )
_home = _chords [ - 1 ] == _chords [ 0 ] and len ( _chords ) > 1
mo . vstack (
[
_picture ,
mo . md (
" → " . join ( _names )
+ f " \n\n { len ( set ( _names )) } different chords"
+ ( ", and the walk ends where it began." if _home else "." )
+ " The boldest triangle is the start."
),
]
)
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