Skip to content

Commit

Permalink
Make cat' and friends accumulate seperation between empty diagrams
Browse files Browse the repository at this point in the history
closes #37
  • Loading branch information
bergey committed Sep 20, 2013
1 parent 75e4643 commit cad6082
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
25 changes: 18 additions & 7 deletions src/Diagrams/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ module Diagrams.Combinators

) where

import Data.AdditiveGroup
import Data.AdditiveGroup hiding (Sum, getSum)
import Data.AffineSpace ((.+^))
import Data.Default.Class
import Data.Semigroup
import Data.VectorSpace
import Data.VectorSpace hiding (Sum, getSum)
import Data.Monoid.Cut

import Diagrams.Core
import Diagrams.Core.Envelope
import Diagrams.Located
import Diagrams.Path
import Diagrams.Segment (straight)
Expand Down Expand Up @@ -323,7 +325,7 @@ instance Num (Scalar v) => Default (CatOpts v) where
--
-- See also 'cat'', which takes an extra options record allowing
-- certain aspects of the operation to be tweaked.
cat :: ( Juxtaposable a, Monoid' a, HasOrigin a
cat :: ( Enveloped a, Juxtaposable a, Monoid' a, HasOrigin a
, InnerSpace (V a), OrderedField (Scalar (V a))
)
=> V a -> [a] -> a
Expand All @@ -346,13 +348,22 @@ cat v = cat' v def
-- Note that @cat' v with {catMethod = Distrib} === mconcat@
-- (distributing with a separation of 0 is the same as
-- superimposing).
cat' :: ( Juxtaposable a, Monoid' a, HasOrigin a
cat' :: ( Enveloped a, Juxtaposable a, Monoid' a, HasOrigin a
, InnerSpace (V a), OrderedField (Scalar (V a))
)
=> V a -> CatOpts (V a) -> [a] -> a
cat' v (CatOpts { catMethod = Cat, sep = s }) = foldB comb mempty
where comb d1 d2 = d1 <> (juxtapose v d1 d2 # moveOriginBy vs)
vs = s *^ normalized (negateV v)
cat' v (CatOpts { catMethod = Cat, sep = s }) = snd . foldB comb mempty . map setSpace
where setSpace a = case getOption . unEnvelope . getEnvelope $ a of
Nothing -> (Uncut (Sum s), a)
Just _ -> (Sum 0 :||: Sum s, a)
unit = normalized (negateV v)
comb (s1,d1) (s2,d2) = (s1 <> s2, d1 <> juxtapose v d1 d2 # moveOriginBy vs) where
vs = spacing *^ unit
spacing = rhs s1 ^+^ lhs s2
rhs (Uncut a) = getSum a
rhs (_ :||: a) = getSum a
lhs (Uncut _) = 0
lhs (a :||: _) = getSum a

cat' v (CatOpts { catMethod = Distrib, sep = s }) =
position . zip (iterate (.+^ (s *^ normalized v)) origin)
8 changes: 4 additions & 4 deletions src/Diagrams/TwoD/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ atAngle th = beside (fromDirection th)
-- "Diagrams.TwoD.Align" before applying 'hcat'.
--
-- * For non-axis-aligned layout, see 'cat'.
hcat :: (Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
hcat :: (Enveloped a, Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
=> [a] -> a
hcat = hcat' def

-- | A variant of 'hcat' taking an extra 'CatOpts' record to control
-- the spacing. See the 'cat'' documentation for a description of
-- the possibilities.
hcat' :: (Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
hcat' :: (Enveloped a, Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
=> CatOpts R2 -> [a] -> a
hcat' = cat' unitX

Expand All @@ -131,14 +131,14 @@ hcat' = cat' unitX
-- "Diagrams.TwoD.Align" before applying 'vcat'.
--
-- * For non-axis-aligned layout, see 'cat'.
vcat :: (Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
vcat :: (Enveloped a, Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
=> [a] -> a
vcat = vcat' def

-- | A variant of 'vcat' taking an extra 'CatOpts' record to control
-- the spacing. See the 'cat'' documentation for a description of the
-- possibilities.
vcat' :: (Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
vcat' :: (Enveloped a, Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
=> CatOpts R2 -> [a] -> a
vcat' = cat' (negateV unitY)

Expand Down

0 comments on commit cad6082

Please sign in to comment.