Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make cat' and friends accumulate seperation between empty diagrams #130

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/Diagrams/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ module Diagrams.Combinators

) where

import Data.AdditiveGroup hiding (Sum, getSum)
import Control.Lens ( (&), (%~), (.~), Lens', makeLensesWith
, lensRules, lensField, generateSignatures, unwrapping)
import Data.AdditiveGroup
import Data.AffineSpace ((.+^))
import Data.Default.Class
import Data.Proxy
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 @@ -342,7 +344,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 :: ( HasEmpty a, Juxtaposable a, Monoid' a, HasOrigin a
, InnerSpace (V a), OrderedField (Scalar (V a))
)
=> V a -> [a] -> a
Expand All @@ -365,13 +367,23 @@ 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' :: ( HasEmpty 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 = if isEmpty a
then (Uncut (Sum s), a)
else (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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise this looks good.


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 :: (HasEmpty 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' :: (HasEmpty 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 :: (HasEmpty 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' :: (HasEmpty a, Juxtaposable a, HasOrigin a, Monoid' a, V a ~ R2)
=> CatOpts R2 -> [a] -> a
vcat' = cat' (negateV unitY)

Expand Down