summaryrefslogtreecommitdiff
path: root/idris/basic.idr
blob: 0d67f1d80c268914bca5c91ec2e32cd5caf9ef6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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