summaryrefslogtreecommitdiff
path: root/idris/thing.idr
diff options
context:
space:
mode:
Diffstat (limited to 'idris/thing.idr')
-rw-r--r--idris/thing.idr214
1 files changed, 214 insertions, 0 deletions
diff --git a/idris/thing.idr b/idris/thing.idr
new file mode 100644
index 0000000..353523d
--- /dev/null
+++ b/idris/thing.idr
@@ -0,0 +1,214 @@
+import Data.Vect
+import Data.Fin
+import System as System
+
+import Graphics.SDL2 as SDL2
+
+infixr 2 <:>
+infixr 4 </>
+
+Point : Nat -> Type
+Point n = Vect n Double
+
+Simplex : Nat -> Type
+Simplex n = Vect (S n) $ Point (S n)
+
+(+) : Point n -> Point n -> Point n
+xs + ys = zipWith (+) xs ys
+
+(*) : Double -> Point n -> Point n
+n * xs = map ((*) n) xs
+
+average : Double -> Double -> Double
+average a b = (a + b) / 2
+
+midpoint : Point n -> Point n -> Point n
+midpoint a b = zipWith average a b
+
+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)
+
+RVect : Vect m Nat -> Type -> Type
+RVect [] t = t
+RVect (n::ns) t = Vect n (RVect ns t)
+
+Matrix : Nat -> Nat -> Type -> Type
+Matrix n m t = Vect m $ Vect n t
+
+RTuple : Vect n Type -> Type
+RTuple [] = ()
+RTuple [t] = t
+RTuple (t::ts) = (t, RTuple ts)
+
+BezierFunc : Nat -> Nat -> Type
+BezierFunc Z d = Point d
+BezierFunc (S n) d = (Double -> BezierFunc n d)
+
+-- bezier : {ds: Vect e Nat} -> RVect ds $ Point d -> BezierFunc e d
+
+(<:>) : Num a => Vect n a -> Vect n a -> a
+w <:> v = sum $ zipWith (*) w v
+
+(</>) : Num a => Matrix n m a -> Vect n a -> Vect m a
+m </> v = map (v <:>) m
+
+origin : (n: Nat) -> Point n
+origin Z = []
+origin (S k) = 0::(origin k)
+
+sump : Vect n $ Point d -> Point d
+sump (x::xs) = foldr (+) x xs
+
+comb : Vect n a -> Vect m b -> Vect (n * m) (a, b)
+comb (a::as) bs = (comb' a bs) ++ comb as bs where
+ comb' a bs = map (\x => (a, x)) bs
+
+vcomb : Vect n a -> Vect m a -> Vect (n * m) $ Vect 2 a
+vcomb as bs = map (\(a,b) => [a,b]) $ comb as bs
+
+vvcomb : Vect n a -> Vect m $ Vect o a -> Vect (n * m) $ Vect (S o) a
+vvcomb (x::xs) ys = (vvcomb' x ys) ++ vvcomb xs ys where
+ vvcomb' x ys = map ((::)x) ys
+
+Indices : Vect n Nat -> Type
+Indices [] = ()
+Indices (n::ns) = (Fin n, Indices ns)
+
+-- Create a combination of all numbers counting up to the number mentioned in
+-- the input list.
+-- For example, [4, 2] should produce
+-- `[[0,0],[0,1],[1,0],[1,1],[2,0],[2,1],[3,0],[3,1]]`
+natComb : (ds: Vect n Nat) -> Vect (product ds) $ Indices ds
+
+bezier : Vect n $ Point d -> Vect 1 Double -> BezierFunc 0 d
+bezier {n} ps [t] = sump $ zipWith bs ps $ nats n where
+ bs p i = (bernstein i (minus n 1) t) * p
+
+finer : Vect (S (S n)) Double -> Vect (S (2 * (S n))) Double
+
+index : {ds: Vect n Nat} -> Indices ds -> RVect ds a -> a
+index {ds = []} _ x = x
+index {ds = _::[]} (i, ()) rv = index i rv
+index {ds = _::_} is rv = Main.index (snd is) $ index (fst is) rv
+
+asVect : {ds: Vect n Nat} -> Indices ds -> Vect n Nat
+asVect {ds = []} _ = []
+asVect {ds = _::[]} (i, ()) = [finToNat i]
+asVect {ds = _::_} is = (finToNat $ Basics.fst is)::(asVect $ Basics.snd is)
+
+bezier' : {ds: Vect e Nat} -> RVect ds $ Point d -> Vect e Double -> BezierFunc 0 d
+bezier' {ds} x ts = sump $ map (\i => (bernsteins i ds ts) * (Main.index i x)) (natComb ds) where
+ bernsteins i ds ts = product $ zipWith3 bernstein (asVect i) ds ts
+
+bezier'' : {ds: Vect e Nat} -> RVect ds $ Point d -> BezierFunc e d
+
+-- 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
+
+test : Vect 4 $ Point 2
+test = [[179,381],[100,100],[447,342],[353,92]]
+
+divNat : Double -> Double -> Nat
+divNat a b =
+ if a < 0 then Z else S (divNat (a - b) b)
+
+-- between : (l: Double) -> (u: Double) -> (s: Double) -> Vect ((divNat (u - l) s) + 0) Double
+-- between lower upper step =
+-- if lower < upper then lower::(between (lower + step) upper step) else [upper]
+
+model : List $ Point 2
+model = map (\t => bezier test [t]) [(cast x) / 10 | x <- [1..10]]
+
+samplePoints : Vect n Double -> Matrix x y $ Vect 2 Double -> Matrix x y $ Vect (n + 2) Double
+samplePoints ts xy = map (\cs => map (\c => ts ++ c) cs) xy
+
+getSamples : {ds: Vect e Nat} -> RVect ds $ Point d -> Matrix x y $ Vect e Double -> Matrix x y $ Point d
+-- getSamples rv ts xy = map (\cs => map (\c => bezier' rv (ts ++ c)) cs) xy
+getSamples rv tss = map doRows tss where
+ doCols : Vect e Double -> Point d
+ doCols ts = bezier' rv ts
+ doRows : Vect x $ Vect e Double -> Vect x $ Point d
+ doRows cs = map doCols cs
+
+xrotator : Double -> Matrix 3 3 Double
+xrotator a = [[1,0,0],[0, cos a, -sin a],[0, sin a, cos a]]
+
+yrotator : Double -> Matrix 3 3 Double
+yrotator a = [[cos a, 0, sin a],[0,1,0],[-sin a, 0, cos a]]
+
+reduceTo3d : Point n -> Point 3
+reduceTo3d {n = Z} p = reduceTo3d [0]
+reduceTo3d {n = (S Z)} [x] = reduceTo3d [x, 0]
+reduceTo3d {n = (S (S Z))} [x, y] = [x, y, 0]
+reduceTo3d {n = (S (S (S _)))} p = take 3 p
+
+reduceTo2d : Point n -> Vect 2 Int
+reduceTo2d {n = Z} p = reduceTo2d [0]
+reduceTo2d {n = (S Z)} [x] = reduceTo2d [x, 0]
+reduceTo2d {n = (S (S _))} p = map cast $ take 2 p
+
+-- drawModel : Model -> (Double, Double) -> IO ()
+-- drawModel m (x,y) = do
+-- three <-
+
+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
+
+main : IO ()
+main = (do
+ renderer <- SDL2.init 500 500
+ loop renderer
+ SDL2.destroyRenderer renderer
+ quit)
+ where
+ loop : Renderer -> IO ()
+ loop renderer = do
+ False <- SDL2.pollEventsForQuit | pure ()
+ True <- SDL2.setRendererDrawColor renderer 0 0 0 255
+ | fail "setRendererDrawColor"
+ SDL2.renderClear renderer
+ drawPoints renderer test 255 0 0 255
+ drawBetween renderer model 255 255 255 255
+ SDL2.renderPresent renderer
+ loop renderer
+
+-- bezier : Vect m $ Vect (S 1) Double -> Vect 1 Double -> Vect (S 1) Double
+-- bezier [ p0 ] [ t ] = p0
+-- bezier ps [ t ] = sum $ zipWith bernstein' ps $ oneOnwards l where
+
+-- bezier ps [t] = sum (map (bernstein t l) ps)
+-- bezier xs (x :: ys) = ?bezier_rhs_2 where
+-- bezier' ps t = sum (map (bernstein t l) ps)
+
+-- bezier [p0] t = p0
+-- bezier ps@(_::_) t = (1 - t)*(bezier (init ps) t) + (t)*(bezier (tail ps) t)
+
+-- test : Vect 4 (Vect 2 Double)
+-- test = [ [ 100.0, 250.0], [ 100.0, 100.0 ], [ 400.0, 400.0 ], [ 400.0, 250.0 ] ]
+
+-- testOut : Vect 2 Double
+-- testOut = bezier test 0.5