Copyright | (c) 2011 Brent Yorgey |
---|---|
License | BSD-style (see LICENSE) |
Maintainer | byorgey@cis.upenn.edu |
Safe Haskell | None |
Language | Haskell2010 |
Generation of Apollonian gaskets. Any three mutually tangent circles uniquely determine exactly two others which are mutually tangent to all three. This process can be repeated, generating a fractal circle packing.
See J. Lagarias, C. Mallows, and A. Wilks, "Beyond the Descartes circle theorem", Amer. Math. Monthly 109 (2002), 338--361. http://arxiv.org/abs/math/0101066.
A few examples:
import Diagrams.TwoD.Apollonian apollonian1 = apollonianGasket 0.01 2 2 2
import Diagrams.TwoD.Apollonian apollonian2 = apollonianGasket 0.01 2 3 3
import Diagrams.TwoD.Apollonian apollonian3 = apollonianGasket 0.01 2 4 7
- data Circle = Circle {}
- mkCircle :: Double -> P2 -> Circle
- center :: Circle -> P2
- radius :: Circle -> Double
- descartes :: Floating a => [a] -> [a]
- other :: Num a => [a] -> a -> a
- initialConfig :: Double -> Double -> Double -> [Circle]
- apollonian :: Double -> [Circle] -> [Circle]
- drawCircle :: Renderable (Path R2) b => Double -> Circle -> Diagram b R2
- drawGasket :: Renderable (Path R2) b => [Circle] -> Diagram b R2
- apollonianGasket :: Renderable (Path R2) b => Double -> Double -> Double -> Double -> Diagram b R2
Circles
Representation for circles that lets us quickly compute an Apollonian gasket.
Circle | |
|
Create a Circle
given a signed radius and a location for its center.
Descartes' Theorem
descartes :: Floating a => [a] -> [a] Source
Descartes' Theorem states that if b1
, b2
, b3
and b4
are
the bends of four mutually tangent circles, then
b1^2 + b2^2 + b3^2 + b4^2 = 1/2 * (b1 + b2 + b3 + b4)^2.
Surprisingly, if we replace each of the bi
with the product
of bi
and the center of the corresponding circle (represented
as a complex number), the equation continues to hold! (See the
paper referenced at the top of the module.)
descartes [b1,b2,b3]
solves for b4
, returning both solutions.
Notably, descartes
works for any instance of Floating
, which
includes both Double
(for bends), Complex Double
(for
bend/center product), and Circle
(for both at once).
other :: Num a => [a] -> a -> a Source
If we have four mutually tangent circles we can choose one of
them to replace; the remaining three determine exactly one other
circle which is mutually tangent. However, in this situation
there is no need to apply descartes
again, since the two
solutions b4
and b4'
satisfy
b4 + b4' = 2 * (b1 + b2 + b3)
Hence, to replace b4
with its dual, we need only sum the other
three, multiply by two, and subtract b4
. Again, this works for
bends as well as bend/center products.
initialConfig :: Double -> Double -> Double -> [Circle] Source
Generate an initial configuration of four mutually tangent circles, given just the signed bends of three of them.
Apollonian gasket generation
apollonian :: Double -> [Circle] -> [Circle] Source
Given a threshold radius and a list of four mutually tangent circles, generate the Apollonian gasket containing those circles. Stop the recursion when encountering a circle with an (unsigned) radius smaller than the threshold.
Diagram generation
drawCircle :: Renderable (Path R2) b => Double -> Circle -> Diagram b R2 Source
Draw a circle.
drawGasket :: Renderable (Path R2) b => [Circle] -> Diagram b R2 Source
Draw a generated gasket, using a line width 0.003 times the radius of the largest circle.