Last active
June 21, 2018 16:48
-
-
Save fryguybob/96d8473453b8a01ce5c59e786916f730 to your computer and use it in GitHub Desktop.
Measuring distance on a diagram using names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> import Diagrams.Prelude | |
Prelude Diagrams.Prelude> :set -XFlexibleContexts | |
Prelude Diagrams.Prelude> let a = circle 1 # named "a" | |
Prelude Diagrams.Prelude> let b = circle 2 # named "b" | |
Prelude Diagrams.Prelude> let d = beside unitX a b :: D V2 Double | |
Prelude Diagrams.Prelude> names d | |
[(toName "a",[P (V2 0.0 0.0)]),(toName "b",[P (V2 2.9999999999999996 0.0)])] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
import Diagrams.Prelude | |
newtype M = M { unM :: Last (P2 Double) } | |
type instance V M = V2 | |
type instance N M = Double | |
instance Enveloped M where | |
getEnvelope _ = mempty | |
instance HasOrigin M where | |
moveOriginTo x (M (Last p)) = M (Last (moveOriginTo x p)) | |
instance Semigroup M where | |
M a <> M b = M (a <> b) | |
sconcat = M . sconcat . fmap unM | |
stimes b = M . stimes b . unM | |
here :: M | |
here = M (Last (p2 (0,0))) | |
hereToThere :: M -> V2 Double | |
hereToThere (M (Last p)) = p .-. p2 (0,0) | |
main = do | |
let a = circle 1 :: D V2 Double | |
b = circle 2 :: D V2 Double | |
d = hereToThere . snd $ beside unitX (a, here) (b, here) | |
print d | |
-- *Main> :main | |
-- V2 2.9999999999999996 0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a utility function that does this using named points:
Kind of ugly but it works!