module LSystem.Turtle where -- ta = turning angle, tai = turning angle increment, loc = location, -- hdg = heading, ll = line length, ls = length scalar, stk = turtle -- stack, lw = line width, lwi = line width increment, type Pt = (Double, Double) data Turtle = Turtle { ta :: Double, tai :: Double, loc :: Pt, hdg :: Double, ll :: Double, ls :: Double, stk :: [Turtle] } turnRight :: Turtle -> Turtle turnRight t = t { hdg = hdg t + (ta t) } turnLeft :: Turtle -> Turtle turnLeft t = t { hdg = hdg t - (ta t) } turnBack :: Turtle -> Turtle turnBack t = t { hdg = hdg t + pi } incrLine :: Turtle -> Turtle incrLine t = t { ll = ll t * ls t } decrLine :: Turtle -> Turtle decrLine t = t { ll = ll t / ls t } forward :: Turtle -> Turtle forward t = t { loc = shift (loc t) (ll t) (hdg t) } where shift (x, y) r d = (x + r * cos d, y + r * sin d) push :: Turtle -> Turtle push t = t { stk = t : stk t } pop :: Turtle -> Turtle pop t = head (stk t) stepTurtle :: (t -> Pt -> Pt -> b) -> Turtle -> t -> (Turtle, b) stepTurtle f t i = (t', i') where p = loc t t' = forward t p' = loc t' i' = f i p p'