-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsierpinski.hs
65 lines (46 loc) · 1.81 KB
/
sierpinski.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
63
64
{-
In the beginning, we simply print a triangle which points upwards.
There are 32 rows and 63 columns in this matrix.
The triangle is composed of underscores and ones as shown below.-}
{-# LANGUAGE OverloadedRecordDot #-}
import Data.Bits
import Data.Colour.CIE.Illuminant (c)
type CanvasLine = Integer
type Canvas = [CanvasLine]
data Triangle = Triangle { top :: Int, center :: Int, height :: Int , inverted :: Bool}
canvas :: Canvas
canvas = replicate 32 zeroBits
canvasLine :: [Int] -> CanvasLine
canvasLine = foldl setBit zeroBits
triangle :: Triangle -> [CanvasLine]
triangle t = align $ map lines (take t.height [0..]) where
lines i = canvasLine [t.center - i .. t.center + i]
align = if t.inverted then reverse else id
addTriangle :: Triangle -> Canvas -> Canvas
addTriangle t c = aboveLines ++ middleLines ++ belowLines where
aboveLines = take t.top c
middleLines = zipWith xor (triangle t) (drop t.top c)
belowLines = drop (t.top + t.height) c
-- takes a triangle and returns information for the 4 triangles it produces in next iteration
centerTriangle :: Triangle -> Triangle
centerTriangle = undefined
edgeTriangles :: Triangle -> [Triangle]
edgeTriangles = undefined
-- IO functions
main :: IO ()
main = do
let c = canvas
printCanvas c
let t1 = Triangle {top = 0, center = 31, height = 32, inverted = False}
c1 = addTriangle t1 c
printCanvas c1
let t2 = Triangle {top = 16, center = 31, height = 16, inverted = True}
c2 = addTriangle t2 c1
printCanvas c2
printCanvas :: Canvas -> IO ()
printCanvas t = newLine >> mapM_ printLine t >> newLine
printLine :: CanvasLine -> IO ()
printLine l = mapM_ (printPoint l) [0..62] >> newLine where
printPoint l i = putStr $ if l `testBit` i then "1" else "_"
newLine :: IO ()
newLine = putStrLn ""