From 61fd4963f106ef0df1b1673443ea164caeadf1a0 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Thu, 1 Feb 2018 22:05:45 +0000 Subject: [PATCH] Add new instruction: draw forward without drawing a line --- examples/cantor.json | 27 +++++++++++++++++++++++++++ src/Lib.hs | 4 ++++ 2 files changed, 31 insertions(+) create mode 100644 examples/cantor.json diff --git a/examples/cantor.json b/examples/cantor.json new file mode 100644 index 0000000..ccdf4ee --- /dev/null +++ b/examples/cantor.json @@ -0,0 +1,27 @@ +{ + "name": "cantor", + "alphabet": "AB", + "axiom": "A", + "rules": [ + [ + "A", + "ABA" + ], + [ + "B", + "BBB" + ] + ], + "angle": 60.0, + "distance": 10.0, + "representation": [ + [ + "A", + "Forward" + ], + [ + "B", + "ForwardNoDraw" + ] + ] +} diff --git a/src/Lib.hs b/src/Lib.hs index 76b8995..ebea1f0 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -43,6 +43,7 @@ instance (ToJSON a) => ToJSON (LSystem a) where -- | Instructions for displaying the L-system data Instruction = Forward -- ^ move forward + | ForwardNoDraw -- ^ move forward, do not draw a line | TurnRight -- ^ turn right by angle | TurnLeft -- ^ turn left by angle | Push -- ^ push a position on the stack @@ -54,6 +55,8 @@ instance FromJSON Instruction where parseJSON = withText "Instruction" $ \s -> if s `elem` ["Forward", "forward", "F", "f"] then pure Forward + else if s `elem` ["ForwardNoDraw", "forwardnodraw", "FN", "fn"] then + pure ForwardNoDraw else if s `elem` ["TurnRight", "Turnright", "turnright", "Right", "right", "R", "r"] then pure TurnRight else if s `elem` ["TurnLeft", "Turnleft", "turnleft", "Left", "left", "L", "l"] then @@ -94,6 +97,7 @@ turtle angle distance = go 90 (Line [(0,0)]) (Pictures []) [] go theta (Line path) (Pictures ps) stack (x:xs) = case x of Forward -> go theta (Line (p:path)) (Pictures ps) stack xs + ForwardNoDraw -> go theta (Line [p]) (Pictures (Line path : ps)) stack xs TurnRight -> go (theta + angle) (Line path) (Pictures ps) stack xs TurnLeft -> go (theta - angle) (Line path) (Pictures ps) stack xs Push -> go theta (Line path) (Pictures ps) ((head path, theta):stack) xs