A visual proof of an identity involving triangular numbers, created for this blog post, which also contains several other similar proofs.
import Diagrams.Backend.SVG.CmdLine
{-# LANGUAGE NoMonomorphismRestriction #-}
import Diagrams.Prelude hiding (dot)
import Data.Colour
Draw a group of dots in a triangular array, all with the same color
and backed by a solid-colored triangle to visually group them. Note
how the dots are laid out by creating a trail called edge
, rotating
it 60 degrees, and using decorateTrail
to lay out the rows of dots.
= dots <> (strokeLoop edges # lc c # lw thin # fcA (c `withOpacity` 0.5))
mkTri c n where rows = map (hcat' (with & sep .~ 1 ))
. zipWith replicate [n,n-1..1]
. repeat
$ dot c
= cat' v (with & sep .~ 3 & catMethod .~ Distrib) rows
dots = rotateBy (1/6) unitX
v = fromOffsets . replicate (n-1) $ unitX # scale 3
edge = glueLine (edge <> rotateBy (1/3) edge <> rotateBy (2/3) edge)
edges
= unitCircle
dot c # lw none
# fc c
= height (rotateBy (1/6) $ strutY 1 :: D V2 Double) rowSpc
row k n s c
draws a row of k
size-n
triangles with color c
,
separated by enough space for s
dots.
= hcat' (with & sep .~ 1 + 3*s) (replicate k (mkTri c n)) row k n s c
The visual proof, which simply consists in assembling various
sub-triangles into a larger triangle, using appropriately transformed
and aligned instances of row
.
= vcat' (with & sep .~ rowSpc) (map tRow [1..k])
law4 k n c1 c2 where tRow k = (row k n 0 c1 # centerX # alignT)
<>
-1) (n-1) 1 c2 # reflectY # centerX # alignT) (row (k
Finally, create a row of diagrams showing the proof at different sizes.
= hcat' (with & sep .~ 4) . map f --(alignB . f)
exampleRow f
= exampleRow law4' [2..4]
law4Dia where law4' k = law4 k 3 purple gold
= law4Dia # frame 0.2 example
= mainWith (example :: Diagram B) main