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