diff options
| author | Tom Harley | 2019-04-19 16:11:26 +0100 |
|---|---|---|
| committer | Tom Harley | 2019-04-19 16:11:26 +0100 |
| commit | 0be2f6f8ce0ec9c17453c59ef9f9d150ac7c3c08 (patch) | |
| tree | 3b7a58041eb9d264b72c95c44a105a4cca9cd6d0 /idris/basic.idr | |
| download | bezier-master.tar.gz bezier-master.zip | |
Diffstat (limited to 'idris/basic.idr')
| -rw-r--r-- | idris/basic.idr | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/idris/basic.idr b/idris/basic.idr new file mode 100644 index 0000000..0d67f1d --- /dev/null +++ b/idris/basic.idr @@ -0,0 +1,86 @@ +import Data.Vect +import System as System + +import Graphics.SDL2 as SDL2 + +Point : Nat -> Type +Point n = Vect n Double + +(+) : Point n -> Point n -> Point n +xs + ys = zipWith (+) xs ys + +(*) : Double -> Point n -> Point n +n * xs = map ((*) n) xs + +nCr : Nat -> Nat -> Double +nCr n r = (cast $ fact n) / (cast $ fact r) / (cast $ fact $ minus n r) + +bernstein : Nat -> Nat -> Double -> Double +bernstein i n t = (nCr n i) * (pow t i) * (pow (1 - t) $ minus n i) + +nats : (n: Nat) -> Vect n Nat +nats (S k) = reverse $ zo k where + zo Z = Z::[] + zo (S k) = (S k)::(zo k) + +sump : Vect n $ Point d -> Point d +sump (x::xs) = foldr (+) x xs + +bezier : Vect n $ Point d -> Double -> Point d +bezier {n} ps t = sump $ zipWith bs ps $ nats n where + bs p i = (bernstein i (minus n 1) t) * p + +drawBetween : Renderer -> List $ Point 2 -> Int -> Int -> Int -> Int -> IO () +drawBetween _ [] _ _ _ _ = pure () +drawBetween _ [p] _ _ _ _ = pure () +drawBetween rend (p::q::ps) r g b a = do + SDL2.drawLine rend (cast $ head p) (cast $ last p) + (cast $ head q) (cast $ last q) r g b a + drawBetween rend (q::ps) r g b a + +drawPoints : Renderer -> Vect n $ Point 2 -> Int -> Int -> Int -> Int -> IO () +drawPoints _ [] _ _ _ _ = pure () +drawPoints rend (p::ps) r g b a = do + SDL2.filledEllipse rend (cast $ head p) (cast $ last p) 10 10 r g b a + drawPoints rend ps r g b a + +restOfTheOwl : Renderer -> Vect n $ Point 2 -> IO () +restOfTheOwl rend ps = (do + drawPoints rend ps 255 0 0 255 + drawBetween rend (map (\x => bezier ps x) samples) 255 255 255 255) where + samples : List Double + samples = [(cast x) / 10 | x <- [1..10]] + +-- Taken from https://github.com/steshaw/idris-sdl2/blob/master/test/hello.idr +fail : (msg: String) -> IO () +fail msg = do + err <- getError + fPutStr stderr $ msg ++ " failed:" ++ err + fflush stderr + System.exit 1 + +main : IO () +main = (do + renderer <- SDL2.init 500 500 + loop renderer []) + where + loop : Renderer -> Vect n $ Point 2 -> IO () + loop renderer cps = do + putStrLn "frame" + True <- SDL2.setRendererDrawColor renderer 0 0 0 255 + | fail "setRendererDrawColor" + SDL2.renderClear renderer + restOfTheOwl renderer cps + SDL2.renderPresent renderer + event <- SDL2.pollEvent + case event of + Just (SDL2.AppQuit) => do + pure () + Just (SDL2.KeyDown SDL2.KeyEsc) => do + pure () + Just (SDL2.MouseButtonDown Left a b) => do + putStrLn $ show a + putStrLn $ show b + loop renderer (cps ++ [[cast a, cast b]]) + _ => do + loop renderer cps |
