4import Control.ST.ImplicitCall
7interface TurtleGraphics (m : Type -> Type) where
10 start : Int -> Int -> ST m (Maybe Var) [addIfJust Turtle]
11 end : (t : Var) -> ST m () [Remove t Turtle]
13 fd : (t : Var) -> Int -> ST m () [t ::: Turtle]
14 rt : (t : Var) -> Int -> ST m () [t ::: Turtle]
16 penup : (t : Var) -> ST m () [t ::: Turtle]
17 pendown : (t : Var) -> ST m () [t ::: Turtle]
18 col : (t : Var) -> Col -> ST m () [t ::: Turtle]
20 -- Render the picture drawn so far in a window, and wait for a key press
21 render : (t : Var) -> ST m () [t ::: Turtle]
24Line = ((Int, Int), (Int, Int), Col)
26-- Implement turtle graphics in terms of existing stateful systems;
27-- 'Draw' provides a Surface to draw on, and three states.
28Draw m => TurtleGraphics m where
29 Turtle = Composite [Surface {m}, -- surface to draw on
30 State Col, -- pen colour
31 State (Int, Int, Int, Bool), -- pen location/direction/down
32 State (List Line)] -- lines to draw on render
34 start x y = with ST do
35 Just srf <- initWindow x y
36 | Nothing => pure Nothing
38 pos <- new (320, 200, 0, True)
41 combine turtle [srf, col, pos, lines]
44 end t = do [srf, col, pos, lines] <- split t
45 closeWindow srf; delete col; delete pos; delete lines; delete t
47 fd t dist = with ST do
48 [srf, col, pos, lines] <- split t
49 (x, y, d, p) <- read pos
50 let x' = cast x + cast dist * sin (rad d)
51 let y' = cast y + cast dist * cos (rad d)
54 write lines (if p then ((x, y), (cast x', cast y'), c) :: ls
56 write pos (cast x', cast y', d, p)
57 combine t [srf, col, pos, lines]
58 where rad : Int -> Double
59 rad x = (cast x * pi) / 180.0
61 rt t angle = with ST do
62 [srf, col, pos, lines] <- split t
63 (x, y, d, p) <- read pos
64 write pos (x, y, d + angle `mod` 360, p)
65 combine t [srf, col, pos, lines]
68 [srf, col, pos, lines] <- split t
69 (x, y, d, _) <- read pos
70 write pos (x, y, d, False)
71 combine t [srf, col, pos, lines]
72 pendown t = with ST do
73 [srf, col, pos, lines] <- split t
74 (x, y, d, _) <- read pos
75 write pos (x, y, d, True)
76 combine t [srf, col, pos, lines]
78 [srf, col, pos, lines] <- split t
80 combine t [srf, col, pos, lines]
83 [srf, col, pos, lines] <- split t
84 filledRectangle srf (0, 0) (640, 480) black
85 drawAll srf !(read lines)
87 combine t [srf, col, pos, lines]
93 where drawAll : (srf : Var) -> List Line -> ST m () [srf ::: Surface {m}]
94 drawAll srf [] = pure ()
95 drawAll srf ((start, end, col) :: xs)
96 = do drawLine srf start end col
99turtle : (ConsoleIO m, TurtleGraphics m) => ST m () []
101 Just t <- start 640 480
102 | Nothing => putStr "Can't make turtle\n"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list