Skip to content

Instantly share code, notes, and snippets.

@tazjin
Forked from possan/plasma.hs
Last active December 17, 2015 20:09
Show Gist options
  • Save tazjin/5665471 to your computer and use it in GitHub Desktop.
Save tazjin/5665471 to your computer and use it in GitHub Desktop.
-- haskell plasma attempt
-- compile with: ghc plasma.hs
-- run: plasma 80 50
import Control.Concurrent (forkIO, threadDelay)
import System.Environment
import System.Exit (exitSuccess)
coordValue :: Float -> Float -> Float -> Float
coordValue x y t =
3.5 + (3.5 * cos(sin x + 0.2 * x + 0.6 * cos (y * 3) + 0.5 * y + t + 0.7 * sin (x + t) + 0.2 * cos (t + (y * 4) + x * 3)))
drawCol :: Float -> Float -> Float -> Char
drawCol y t x = arr !! floor v
where
v = coordValue (x / 10) (y / 10) t
arr = " .:+o#$&"
drawRow :: Float -> Float -> Float -> String
drawRow w t y =
foldl (flip (:)) [] (map (drawCol y t) [0..w]) ++ "\n"
drawFrame :: Float -> Float -> Float -> String
drawFrame w h t =
"\27[H" ++ foldl1 (++) (fmap (drawRow w t) [0..h])
frloop :: Float -> Float -> Float -> IO ()
frloop w h t = do
putStrLn $ drawFrame w h t
threadDelay 10000
frloop w h $ t + 0.1
run :: Float -> Float -> IO ()
run w h = do
putStrLn "\27[2J"
forkIO $ frloop w h 0.0
getLine >> exitSuccess
main :: IO ()
main = do
args <- getArgs
case args of
[w, h] -> run (read w) (read h)
_ -> putStrLn "syntax: ./plasma [columns] [rows]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment