Skip to content

Commit

Permalink
Add special case in juxtaposeDefault for empty envelope (see #37)
Browse files Browse the repository at this point in the history
In particular, juxtaposeDefault is now the identity on the second
object if either one has an empty envelope.  In particular this means
that mempty is now an identity element for beside and friends.
  • Loading branch information
Brent Yorgey committed Aug 31, 2012
1 parent 7823658 commit 0a457da
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Graphics/Rendering/Diagrams/Juxtapose.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Graphics.Rendering.Diagrams.Juxtapose
( Juxtaposable(..), juxtaposeDefault
) where

import Data.Functor ((<$>))
import qualified Data.Map as M
import qualified Data.Set as S

Expand All @@ -40,11 +41,15 @@ class Juxtaposable a where
juxtapose :: V a -> a -> a -> a

-- | Default implementation of 'juxtapose' for things which are
-- instances of 'Enveloped' and 'HasOrigin'.
-- instances of 'Enveloped' and 'HasOrigin'. If either envelope is
-- empty, the second object is returned unchanged.
juxtaposeDefault :: (Enveloped a, HasOrigin a) => V a -> a -> a -> a
juxtaposeDefault v a1 a2 = moveOriginBy (v1 ^+^ v2) a2
where v1 = negateV (envelopeV v a1)
v2 = envelopeV (negateV v) a2
juxtaposeDefault v a1 a2 =
case (mv1, mv2) of
(Just v1, Just v2) -> moveOriginBy (v1 ^+^ v2) a2
_ -> a2
where mv1 = negateV <$> envelopeVMay v a1
mv2 = envelopeVMay (negateV v) a2

instance (InnerSpace v, OrderedField (Scalar v)) => Juxtaposable (Envelope v) where
juxtapose = juxtaposeDefault
Expand Down

0 comments on commit 0a457da

Please sign in to comment.