packages feed

zwirn-0.2.2.0: app/zwirn-plot/Main.hs

{-# OPTIONS_GHC -Wno-orphans #-}

module Main where

import Control.Monad (void)
import Control.Monad.Identity (Identity (..))
import Graphics.EasyPlot
import Zwirn.Core.Lib.Modulate
import Zwirn.Core.Lib.Number (range, sine)
import Zwirn.Core.Query
import Zwirn.Core.Time
import Zwirn.Core.Types

-- | the most simple zwirn - no stacks, no state, no value
type Zwirn = ZwirnT Identity () () ()

-- | silence will just throw an error
instance HasSilence Identity where
  silence = error "no silence"

-- | interpret a zwirn as a transformation of time
fromZwirn :: Zwirn -> Double -> Double
fromZwirn z d = fromRational $ tTime $ time $ fst $ unId $ unzwirn z (Time (toRational d) 1) ()
  where
    unId (Identity a) = a

-- | extract the zeroes of the fractional part of the inner time of a zwirn
toData :: Double -> Double -> Zwirn -> [(Double, Double)]
toData st en z = map (\(t, Value _ t2 _, _) -> (fromRational $ tTime t, fromRational $ tTime t2)) xs
  where
    (xs, _) = findAllBreakpoints 0.005 (Time (toRational st) 1) (Time (toRational en) 1) () z

-- | plots both the zwirns breakpoints and inner time
plotZwirn :: Zwirn -> [Graph2D Double Double]
plotZwirn z = [Function2D [] [Range 0 4, Step 0.005] (fromZwirn z), Data2D [Color Red] [] (toData 0 4 z)]

basic :: Zwirn
basic = pure ()

fastcatEx :: Zwirn
fastcatEx = fastcat [basic, basic]

timeLoopEx :: Zwirn
timeLoopEx = timeloop (pure 0.5) basic

zoomEx :: Zwirn
zoomEx = zoom (pure 0.5) (pure 1) fastcatEx

loopEx :: Zwirn
loopEx = loop (pure 0.5) (pure 1) fastcatEx

ribbonEx :: Zwirn
ribbonEx = ribbon (pure 0.5) (pure 0.75) fastcatEx

swingByEx :: Zwirn
swingByEx = swingBy (pure 0.5) (pure 4) (fast (pure 8) $ pure ())

revEx :: Zwirn
revEx = rev basic

weird :: Zwirn
weird = fast (range (pure 1) (pure 2) sine) basic

main :: IO ()
main = void $ plot X11 $ plotZwirn basic