mirror of
https://github.com/reanimate/reanimate.git
synced 2026-09-14 09:32:22 +00:00
Update fourier_draw example.
This commit is contained in:
parent
fd596dba85
commit
ec5967feb6
2 changed files with 43 additions and 20 deletions
|
|
@ -14,38 +14,47 @@ import Reanimate.Driver (reanimate)
|
||||||
import Reanimate.LaTeX
|
import Reanimate.LaTeX
|
||||||
import Reanimate.Monad
|
import Reanimate.Monad
|
||||||
import Reanimate.Svg
|
import Reanimate.Svg
|
||||||
|
import Reanimate.Combinators
|
||||||
|
|
||||||
waveMultiplier :: Int
|
waveMultiplier :: Int
|
||||||
-- waveMultiplier = 1 -- Sawtooth wave
|
-- waveMultiplier = 1 -- Sawtooth wave
|
||||||
waveMultiplier = 2 -- Square wave
|
waveMultiplier = 2 -- Square wave
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = reanimate $
|
main = reanimate $ pauseAtEnd 2 $
|
||||||
fourierAnimation 0 `before`
|
-- fourierAnimation 1 2
|
||||||
fourierAnimation 1 `before`
|
fourierAnimation 1 2 `before`
|
||||||
fourierAnimation 2 `before`
|
fourierAnimation 2 3 `before`
|
||||||
fourierAnimation 3 `before`
|
fourierAnimation 3 4 `before`
|
||||||
fourierAnimation 4 `before`
|
fourierAnimation 4 5 `before`
|
||||||
fourierAnimation 5 `before`
|
fourierAnimation 5 6 `before`
|
||||||
fourierAnimation 6 `before`
|
fourierAnimation 6 7 `before`
|
||||||
fourierAnimation 7 `before`
|
fourierAnimation 7 8 `before`
|
||||||
fourierAnimation 8 `before`
|
fourierAnimation 8 9 `before`
|
||||||
fourierAnimation 9 `before`
|
fourierAnimation 9 10 `before`
|
||||||
fourierAnimation 10 `before`
|
fourierAnimation 10 15 `before`
|
||||||
fourierAnimation 15 `before`
|
fourierAnimation 15 25 `before`
|
||||||
fourierAnimation 25 `before`
|
fourierAnimation 25 40 `before`
|
||||||
fourierAnimation 40
|
fourierAnimation 40 40
|
||||||
|
|
||||||
sWidth = 0.5
|
sWidth = 0.5
|
||||||
|
|
||||||
fourierAnimation :: Int -> Animation
|
applyMorph :: Int -> [Complex Double] -> Complex Double -> [Complex Double]
|
||||||
fourierAnimation nCircles = repeatAnimation 1 $ mkAnimation 10 $ do
|
applyMorph n coeffs mult =
|
||||||
|
map (*mult) (take n coeffs) ++
|
||||||
|
take (length coeffs - n * 2) (drop n coeffs) ++
|
||||||
|
map (*mult) (reverse $ take n $ reverse coeffs)
|
||||||
|
|
||||||
|
fourierAnimation :: Int -> Int -> Animation
|
||||||
|
fourierAnimation nCircles nextCircles = repeatAnimation 1 $ mkAnimation 10 $ do
|
||||||
emit $ mkBackground "black"
|
emit $ mkBackground "black"
|
||||||
phi <- signal 0 1
|
phi <- signal 0 1
|
||||||
|
|
||||||
let circles = findCoefficients nCircles
|
let circles = applyMorph (nextCircles - nCircles) (findCoefficients nextCircles) circleAlpha
|
||||||
|
circleAlpha = realToFrac (max 0 (phi-0.80) / 0.20)
|
||||||
|
|
||||||
emit $ withStrokeWidth (Num 1) $ withStrokeColor "green" $
|
emit $ withStrokeWidth (Num 1) $ withStrokeColor "green" $
|
||||||
mkCirclePath circles
|
mkLinePath $ mkCirclePath circles
|
||||||
drawNCircles $ rearrangeCircles $ rotateCircles phi circles
|
drawNCircles $ rearrangeCircles $ rotateCircles phi circles
|
||||||
|
|
||||||
emit $ withStrokeWidth (Num sWidth) $
|
emit $ withStrokeWidth (Num sWidth) $
|
||||||
|
|
@ -92,6 +101,7 @@ drawNCircles circles = do
|
||||||
worker circles
|
worker circles
|
||||||
emit $ withStrokeWidth (Num sWidth) $
|
emit $ withStrokeWidth (Num sWidth) $
|
||||||
withStrokeColor "white" $
|
withStrokeColor "white" $
|
||||||
|
withStrokeLineJoin JoinRound $
|
||||||
withFillOpacity 0 $
|
withFillOpacity 0 $
|
||||||
mkLinePath [ (x, y) | x :+ y <- scanl (+) 0 circles ]
|
mkLinePath [ (x, y) | x :+ y <- scanl (+) 0 circles ]
|
||||||
where
|
where
|
||||||
|
|
@ -106,7 +116,7 @@ drawNCircles circles = do
|
||||||
& circleRadius .~ Num radius
|
& circleRadius .~ Num radius
|
||||||
mapF (translate x y) $ worker rest
|
mapF (translate x y) $ worker rest
|
||||||
|
|
||||||
mkCirclePath circles = mkLinePath
|
mkCirclePath circles =
|
||||||
[ (x, y)
|
[ (x, y)
|
||||||
| idx <- [0 .. granularity]
|
| idx <- [0 .. granularity]
|
||||||
, let t = idx/granularity
|
, let t = idx/granularity
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,16 @@ lineToPath = map worker
|
||||||
worker (LineBezier [a]) = LineTo OriginAbsolute [a]
|
worker (LineBezier [a]) = LineTo OriginAbsolute [a]
|
||||||
worker LineEnd = EndPath
|
worker LineEnd = EndPath
|
||||||
|
|
||||||
|
lineToPoints :: Int -> [LineCommand] -> [RPoint]
|
||||||
|
lineToPoints nPoints cmds =
|
||||||
|
map lineEnd lineSegments
|
||||||
|
where
|
||||||
|
lineSegments = [ partialLine (fromIntegral n/ fromIntegral nPoints) cmds | n <- [0 .. nPoints-1] ]
|
||||||
|
totalLen = evalState (sum <$> mapM lineLength cmds) zero
|
||||||
|
lineEnd [LineBezier bezier] = last bezier
|
||||||
|
lineEnd (_:xs) = lineEnd xs
|
||||||
|
lineEnd _ = error "invalid line"
|
||||||
|
|
||||||
partialLine :: Double -> [LineCommand] -> [LineCommand]
|
partialLine :: Double -> [LineCommand] -> [LineCommand]
|
||||||
partialLine alpha cmds = evalState (worker 0 cmds) zero
|
partialLine alpha cmds = evalState (worker 0 cmds) zero
|
||||||
where
|
where
|
||||||
|
|
@ -474,6 +484,9 @@ mkColor name =
|
||||||
withStrokeColor :: String -> Tree -> Tree
|
withStrokeColor :: String -> Tree -> Tree
|
||||||
withStrokeColor color = strokeColor .~ pure (mkColor color)
|
withStrokeColor color = strokeColor .~ pure (mkColor color)
|
||||||
|
|
||||||
|
withStrokeLineJoin :: LineJoin -> Tree -> Tree
|
||||||
|
withStrokeLineJoin join = strokeLineJoin .~ pure join
|
||||||
|
|
||||||
withFillColor :: String -> Tree -> Tree
|
withFillColor :: String -> Tree -> Tree
|
||||||
withFillColor color = fillColor .~ pure (mkColor color)
|
withFillColor color = fillColor .~ pure (mkColor color)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue