-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle.hs
63 lines (56 loc) · 2.05 KB
/
triangle.hs
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
module Main where
import System.Environment
import System.Random
import System.Console.ANSI
import System.Timeout
import System.IO
import Safe (readMay)
data Size = Size Int Int
type Point = (Float, Float)
-- given width and height as arguments
-- prints an ascii sierpinski triangle
main :: IO ()
main = do
args <- getArgs
if length args /= 2 then
putStrLn "Usage: triangle <width> <height>"
else do
let maybeSize = do
w <- readMay $ head args
h <- readMay $ args !! 1
return $ Size w h
maybeTriangle maybeSize
-- if arguments are valid, create and show triangle
maybeTriangle :: Maybe Size -> IO ()
maybeTriangle Nothing = putStrLn "You've provided an invalid argument"
maybeTriangle (Just s) = do
hSetBuffering stdin NoBuffering
clearScreen
triangle s (0, 0)
-- calculates the next Point, scales and prints it
-- if 'q' isn't pressed repeats recursively
triangle :: Size -> Point -> IO ()
triangle s @ (Size w h) p = do
(np, c) <- nextPoint p
let (x, y) = scale s np
setCursorPosition (h - round y) (round x)
setSGR [SetColor Foreground Vivid c]
putChar '^'
input <- timeout 1 getChar
case input of
Just i | i == 'q' -> setCursorPosition 0 0 >> setSGR [] >> clearScreen
| otherwise -> step np
Nothing -> step np
where step p = hFlush stdout >> triangle s p
-- scale a point to screen coordinates
scale :: Size -> Point -> Point
scale (Size w h) (x, y) = (x * fromIntegral w, y * fromIntegral h)
-- randomly selects the next tranformation
-- and returns the next Point with it's Color
nextPoint :: Point -> IO (Point, Color)
nextPoint (x, y) = do
r <- randomRIO (0,2) :: IO Int
return $ case r of
0 -> ((0.5 * x , 0.5 * y) , Yellow)
1 -> ((0.5 * x + 0.25 , 0.5 * y + sqrt 3 / 4) , Red)
2 -> ((0.5 * x + 0.5 , 0.5 * y) , Blue)