From f89c625f801fa6c0bf3e2423566249f6d483cd72 Mon Sep 17 00:00:00 2001 From: Daniel Slocombe Date: Wed, 21 Feb 2018 06:54:25 +0000 Subject: [PATCH] Add arrows for TikZ diagrams --- Text/LaTeX/Packages/TikZ/Simple.hs | 10 ++++++++++ Text/LaTeX/Packages/TikZ/Syntax.hs | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Text/LaTeX/Packages/TikZ/Simple.hs b/Text/LaTeX/Packages/TikZ/Simple.hs index afac703..cff0f5e 100644 --- a/Text/LaTeX/Packages/TikZ/Simple.hs +++ b/Text/LaTeX/Packages/TikZ/Simple.hs @@ -54,6 +54,8 @@ type Point = (Double,Double) -- | A figure in the plane. data Figure = Line [Point] -- ^ Line along a list of points. + | Arrow [Point] -- ^ Line along a list of points with an arrowhead . + | Curvy [Point] | Polygon [Point] -- ^ Line along a list of points, but the last point will be joined -- with the first one. | PolygonFilled [Point] -- ^ Same as 'Polygon', but the inner side will be filled with color. @@ -79,10 +81,18 @@ castpoint (x,y) = T.pointAtXY x y radiansToDegrees :: Double -> Double radiansToDegrees x = (180 * x) / pi +toTPoint :: Point -> T.TPoint +toTPoint = uncurry T.pointAtXY + -- | Translate a 'Figure' to a 'TikZ' script. figuretikz :: Figure -> TikZ figuretikz (Line []) = emptytikz figuretikz (Line (p:ps)) = T.draw $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps +figuretikz (Arrow []) = emptytikz +figuretikz (Arrow (p:ps)) = T.path [T.Draw, T.Arrow] $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps +figuretikz (Curvy []) = emptytikz +--figuretikz (Curvy (p:ps)) = T.path [T.Draw, T.Smooth] $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps +figuretikz (Curvy (ps)) = T.Curvy (toTPoint <$> ps) figuretikz (Polygon []) = emptytikz figuretikz (Polygon (p:ps)) = T.draw $ T.Cycle $ foldl (\y x -> y T.->- castpoint x) (T.Start $ castpoint p) ps figuretikz (PolygonFilled []) = emptytikz diff --git a/Text/LaTeX/Packages/TikZ/Syntax.hs b/Text/LaTeX/Packages/TikZ/Syntax.hs index 5d432b0..01d42d1 100644 --- a/Text/LaTeX/Packages/TikZ/Syntax.hs +++ b/Text/LaTeX/Packages/TikZ/Syntax.hs @@ -31,7 +31,7 @@ module Text.LaTeX.Packages.TikZ.Syntax ( , Color (..) , Word8 -- * TikZ - , TikZ + , TikZ (..) , emptytikz , path , scope @@ -245,13 +245,14 @@ instance Render Parameter where -- | A Ti/k/Z script. data TikZ = PathAction [ActionType] TPath + | Curvy [TPoint] | Scope [Parameter] TikZ | TikZSeq (S.Seq TikZ) deriving Show -- | Different types of actions that can be performed -- with a 'TPath'. See 'path' for more information. -data ActionType = Draw | Fill | Clip | Shade deriving Show +data ActionType = Draw | Fill | Clip | Shade | Arrow deriving Show -- | Just an empty script. emptytikz :: TikZ @@ -259,14 +260,20 @@ emptytikz = TikZSeq mempty instance Render TikZ where render (PathAction ts p) = "\\path" <> render ts <> " " <> render p <> " ; " + render (Curvy ps) = "\\draw[->] plot [smooth] coordinates { " <> (renderPoints ps) <> " } ; " render (Scope ps t) = "\\begin{scope}" <> render ps <> render t <> "\\end{scope}" render (TikZSeq ts) = foldMap render ts +renderPoints :: [TPoint] -> Text +renderPoints [] = "" +renderPoints (p:ps) = render p <> " " <> renderPoints ps + instance Render ActionType where render Draw = "draw" render Fill = "fill" render Clip = "clip" render Shade = "shade" + render Arrow = "->" -- | A path can be used in different ways. --