Debian Codegraph @

src/Diagrams/TwoD/Arrow.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE RankNTypes #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TemplateHaskell #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE TypeOperators #-}
11 {-# LANGUAGE UndecidableInstances #-}
12
13 -----------------------------------------------------------------------------
14 -- |
15 -- Module : Diagrams.TwoD.Arrow
16 -- Copyright : (c) 2013-2015 diagrams-lib team (see LICENSE)
17 -- License : BSD-style (see LICENSE)
18 -- Maintainer : [email protected]
19 --
20 -- Drawing arrows in two dimensions. For a tutorial on drawing arrows
21 -- using this module, see the diagrams website:
22 -- <https://diagrams.github.io/doc/arrow.html>.
23 --
24 -----------------------------------------------------------------------------
25
26
27 module Diagrams.TwoD.Arrow
28 ( -- * Examples
29 -- ** Example 1
30 -- | <<diagrams/src_Diagrams_TwoD_Arrow_example1.svg#diagram=example1&width=500>>
31 --
32 -- > -- Connecting two diagrams at their origins.
33 -- >
34 -- > sq = square 2 # showOrigin # lc darkgray # lw ultraThick
35 -- > ds = (sq # named "left") ||| strutX 3 ||| (sq # named "right")
36 -- >
37 -- > shaft = cubicSpline False ( map p2 [(0, 0), (1, 0), (1, 0.2), (2, 0.2)])
38 -- >
39 -- > example1 = ds # connect' (with & arrowHead .~ dart & arrowTail .~ quill
40 -- > & arrowShaft .~ shaft
41 -- > & headLength .~ huge & tailLength .~ veryLarge)
42 -- > "left" "right" # pad 1.1
43
44 -- ** Example 2
45
46 -- | <<diagrams/src_Diagrams_TwoD_Arrow_example2.svg#diagram=example2&width=500>>
47 --
48 -- > -- Comparing connect, connectPerim, and arrowAt.
49 -- >
50 -- > oct = octagon 1 # lc darkgray # lw ultraThick # showOrigin
51 -- > dias = oct # named "first" ||| strut 3 ||| oct # named "second"
52 -- >
53 -- > -- Connect two diagrams and two points on their trails.
54 -- > ex12 = dias # connect' (with & lengths .~ veryLarge) "first" "second"
55 -- > # connectPerim' (with & lengths .~ veryLarge)
56 -- > "first" "second" (15/16 @@ turn) (9/16 @@ turn)
57 -- >
58 -- > -- Place an arrow at (0,0) the size and direction of (0,1).
59 -- > ex3 = arrowAt origin unit_Y
60 -- >
61 -- > example2 = (ex12 <> ex3) # centerXY # pad 1.1
62
63 -- * Creating arrows
64 arrowV
65 , arrowV'
66 , arrowAt
67 , arrowAt'
68 , arrowBetween
69 , arrowBetween'
70 , connect
71 , connect'
72 , connectPerim
73 , connectPerim'
74 , connectOutside
75 , connectOutside'
76
77 , arrow
78 , arrow'
79
80 , arrowFromLocatedTrail
81 , arrowFromLocatedTrail'
82
83 -- * Options
84 , ArrowOpts(..)
85
86 , arrowHead
87 , arrowTail
88 , arrowShaft
89 , headGap
90 , tailGap
91 , gaps, gap
92 , headTexture
93 , headStyle
94 , headLength
95 , tailTexture
96 , tailStyle
97 , tailLength
98 , lengths
99 , shaftTexture
100 , shaftStyle
101 , straightShaft
102
103 -- | See "Diagrams.TwoD.Arrowheads" for a list of standard
104 -- arrowheads and help creating your own.
105 , module Diagrams.TwoD.Arrowheads
106 ) where
107
108 import Control.Lens (Lens', Traversal',
109 generateSignatures, lensRules,
110 makeLensesWith, view, (%~), (&),
111 (.~), (^.))
112 import Data.Default
113 import Data.Maybe (fromMaybe)
114 import Data.Monoid.Coproduct (untangle)
115 import Data.Semigroup
116 import Data.Typeable
117
118 import Data.Colour hiding (atop)
119 import Diagrams.Core
120 import Diagrams.Core.Style (unmeasureAttrs)
121 import Diagrams.Core.Types (QDiaLeaf (..), mkQD')
122
123 import Diagrams.Angle
124 import Diagrams.Attributes
125 import Diagrams.Direction hiding (dir)
126 import Diagrams.Located (Located (..), unLoc)
127 import Diagrams.Parametric
128 import Diagrams.Path
129 import Diagrams.Solve.Polynomial (quadForm)
130 import Diagrams.Tangent (tangentAtEnd, tangentAtStart)
131 import Diagrams.Trail
132 import Diagrams.TwoD.Arrowheads
133 import Diagrams.TwoD.Attributes
134 import Diagrams.TwoD.Path (stroke, strokeT)
135 import Diagrams.TwoD.Transform (reflectY, translateX)
136 import Diagrams.TwoD.Types
137 import Diagrams.TwoD.Vector (unitX, unit_X)
138 import Diagrams.Util (( # ))
139
140 import Linear.Affine
141 import Linear.Metric
142 import Linear.Vector
143
144
145 data ArrowOpts n
146 = ArrowOpts
147 { _arrowHead :: ArrowHT n
148 , _arrowTail :: ArrowHT n
149 , _arrowShaft :: Trail V2 n
150 , _headGap :: Measure n
151 , _tailGap :: Measure n
152 , _headStyle :: Style V2 n
153 , _headLength :: Measure n
154 , _tailStyle :: Style V2 n
155 , _tailLength :: Measure n
156 , _shaftStyle :: Style V2 n
157 }
158
159 -- | Straight line arrow shaft.
160 straightShaft :: OrderedField n => Trail V2 n
161 straightShaft = trailFromOffsets [unitX]
162
163 instance TypeableFloat n => Default (ArrowOpts n) where
164 def = ArrowOpts
165 { _arrowHead = dart
166 , _arrowTail = noTail
167 , _arrowShaft = straightShaft
168 , _headGap = none
169 , _tailGap = none
170
171 -- See note [Default arrow style attributes]
172 , _headStyle = mempty
173 , _headLength = normal
174 , _tailStyle = mempty
175 , _tailLength = normal
176 , _shaftStyle = mempty
177 }
178
179 makeLensesWith (lensRules & generateSignatures .~ False) ''ArrowOpts
180
181 -- | A shape to place at the head of the arrow.
182 arrowHead :: Lens' (ArrowOpts n) (ArrowHT n)
183
184 -- | A shape to place at the tail of the arrow.
185 arrowTail :: Lens' (ArrowOpts n) (ArrowHT n)
186
187 -- | The trail to use for the arrow shaft.
188 arrowShaft :: Lens' (ArrowOpts n) (Trail V2 n)
189
190 -- | Distance to leave between the head and the target point.
191 headGap :: Lens' (ArrowOpts n) (Measure n)
192
193 -- | Distance to leave between the starting point and the tail.
194 tailGap :: Lens' (ArrowOpts n) (Measure n)
195
196 -- | Set both the @headGap@ and @tailGap@ simultaneously.
197 gaps :: Traversal' (ArrowOpts n) (Measure n)
198 gaps f opts = (\h t -> opts & headGap .~ h & tailGap .~ t)
199 <$> f (opts ^. headGap)
200 <*> f (opts ^. tailGap)
201
202 -- | Same as gaps, provided for backward compatiiblity.
203 gap :: Traversal' (ArrowOpts n) (Measure n)
204 gap = gaps
205
206 -- | Style to apply to the head. @headStyle@ is modified by using the lens
207 -- combinator @%~@ to change the current style. For example, to change
208 -- an opaque black arrowhead to translucent orange:
209 -- @(with & headStyle %~ fc orange . opacity 0.75)@.
210 headStyle :: Lens' (ArrowOpts n) (Style V2 n)
211
212 -- | Style to apply to the tail. See `headStyle`.
213 tailStyle :: Lens' (ArrowOpts n) (Style V2 n)
214
215 -- | Style to apply to the shaft. See `headStyle`.
216 shaftStyle :: Lens' (ArrowOpts n) (Style V2 n)
217
218 -- | The length from the start of the joint to the tip of the head.
219 headLength :: Lens' (ArrowOpts n) (Measure n)
220
221 -- | The length of the tail plus its joint.
222 tailLength :: Lens' (ArrowOpts n) (Measure n)
223
224 -- | Set both the @headLength@ and @tailLength@ simultaneously.
225 lengths :: Traversal' (ArrowOpts n) (Measure n)
226 lengths f opts =
227 (\h t -> opts & headLength .~ h & tailLength .~ t)
228 <$> f (opts ^. headLength)
229 <*> f (opts ^. tailLength)
230
231 -- | A lens for setting or modifying the texture of an arrowhead. For
232 -- example, one may write @... (with & headTexture .~ grad)@ to get an
233 -- arrow with a head filled with a gradient, assuming grad has been
234 -- defined. Or @... (with & headTexture .~ solid blue@ to set the head
235 -- color to blue. For more general control over the style of arrowheads,
236 -- see 'headStyle'.
237 headTexture :: TypeableFloat n => Lens' (ArrowOpts n) (Texture n)
238 headTexture = headStyle . _fillTexture
239
240 -- | A lens for setting or modifying the texture of an arrow
241 -- tail. This is *not* a valid lens (see 'committed').
242 tailTexture :: TypeableFloat n => Lens' (ArrowOpts n) (Texture n)
243 tailTexture = tailStyle . _fillTexture
244
245 -- | A lens for setting or modifying the texture of an arrow
246 -- shaft.
247 shaftTexture :: TypeableFloat n => Lens' (ArrowOpts n) (Texture n)
248 shaftTexture = shaftStyle . _lineTexture
249
250 -- Set the default shaft style of an `ArrowOpts` record by applying the
251 -- default style after all other styles have been applied.
252 -- The semigroup stucture of the lw attribute will insure that the default
253 -- is only used if it has not been set in @opts@.
254 shaftSty :: ArrowOpts n -> Style V2 n
255 shaftSty opts = opts^.shaftStyle
256
257 -- Set the default head style. See `shaftSty`.
258 headSty :: TypeableFloat n => ArrowOpts n -> Style V2 n
259 headSty opts = fc black (opts^.headStyle)
260
261 -- Set the default tail style. See `shaftSty`.
262 tailSty :: TypeableFloat n => ArrowOpts n -> Style V2 n
263 tailSty opts = fc black (opts^.tailStyle)
264
265 -- | Calculate the length of the portion of the horizontal line that passes
266 -- through the origin and is inside of p.
267 xWidth :: Floating n => (Traced t, V t ~ V2, N t ~ n) => t -> n
268 xWidth p = a + b
269 where
270 a = fromMaybe 0 (norm <$> traceV origin unitX p)
271 b = fromMaybe 0 (norm <$> traceV origin unit_X p)
272
273 -- | Get the line color from the shaft to use as the fill color for the joint.
274 -- And set the opacity of the shaft to the current opacity.
275 colorJoint :: TypeableFloat n => Style V2 n -> Style V2 n
276 colorJoint sStyle =
277 let c = fmap getLineTexture . getAttr $ sStyle
278 o = fmap getOpacity . getAttr $ sStyle
279 in
280 case (c, o) of
281 (Nothing, Nothing) -> fillColor black mempty
282 (Just t, Nothing) -> fillTexture t mempty
283 (Nothing, Just o') -> opacity o' . fillColor black $ mempty
284 (Just t, Just o') -> opacity o' . fillTexture t $ mempty
285
286 -- | Get line width from a style.
287 widthOfJoint :: forall n. TypeableFloat n => Style V2 n -> n -> n -> n
288 widthOfJoint sStyle gToO nToO =
289 fromMaybe
290 (fromMeasured gToO nToO medium) -- should be same as default line width
291 (fmap getLineWidth . getAttr . unmeasureAttrs gToO nToO $ sStyle)
292
293 -- | Combine the head and its joint into a single scale invariant diagram
294 -- and move the origin to the attachment point. Return the diagram
295 -- and its width.
296 mkHead :: (TypeableFloat n, Renderable (Path V2 n) b) =>
297 n -> ArrowOpts n -> n -> n -> Bool -> (QDiagram b V2 n Any, n)
298 mkHead = mkHT unit_X arrowHead headSty
299
300 mkTail :: (TypeableFloat n, Renderable (Path V2 n) b) =>
301 n -> ArrowOpts n -> n -> n -> Bool -> (QDiagram b V2 n Any, n)
302 mkTail = mkHT unitX arrowTail tailSty
303
304 mkHT
305 :: (TypeableFloat n, Renderable (Path V2 n) b)
306 => V2 n -> Lens' (ArrowOpts n) (ArrowHT n) -> (ArrowOpts n -> Style V2 n)
307 -> n -> ArrowOpts n -> n -> n -> Bool -> (QDiagram b V2 n Any, n)
308 mkHT xDir htProj styProj sz opts gToO nToO reflect
309 = ( (j <> ht)
310 # (if reflect then reflectY else id)
311 # moveOriginBy (jWidth *^ xDir) # lwO 0
312 , htWidth + jWidth
313 )
314 where
315 (ht', j') = (opts^.htProj) sz
316 (widthOfJoint (shaftSty opts) gToO nToO)
317 htWidth = xWidth ht'
318 jWidth = xWidth j'
319 ht = stroke ht' # applyStyle (styProj opts)
320 j = stroke j' # applyStyle (colorJoint (opts^.shaftStyle))
321
322 -- | @spine tr tw hw sz@ makes a trail with the same angles and offset
323 -- as an arrow with tail width @t@w, head width @hw@ and shaft @tr@,
324 -- such that the magnitude of the shaft offset is @sz@. Used for
325 -- calculating the offset of an arrow.
326 spine :: TypeableFloat n => Trail V2 n -> n -> n -> n -> Trail V2 n
327 spine tr tw hw sz = tS <> tr # scale sz <> hS
328 where
329 tSpine = trailFromOffsets [signorm . tangentAtStart $ tr] # scale tw
330 hSpine = trailFromOffsets [signorm . tangentAtEnd $ tr] # scale hw
331 hS = if hw > 0 then hSpine else mempty
332 tS = if tw > 0 then tSpine else mempty
333
334 -- | @scaleFactor tr tw hw t@ calculates the amount required to scale
335 -- a shaft trail @tr@ so that an arrow with head width @hw@ and tail
336 -- width @tw@ has offset @t@.
337 scaleFactor :: TypeableFloat n => Trail V2 n -> n -> n -> n -> n
338 scaleFactor tr tw hw t
339
340 -- Let tv be a vector representing the tail width, i.e. a vector
341 -- of length tw tangent to the trail's start; similarly for hv.
342 -- Let v be the vector offset of the trail.
343 --
344 -- Then we want to find k such that
345 --
346 -- || tv + k*v + hv || = t.
347 --
348 -- We can solve by squaring both sides and expanding the LHS as a
349 -- dot product, resulting in a quadratic in k.
350
351 = case quadForm
352 (quadrance v)
353 (2* (v `dot` (tv ^+^ hv)))
354 (quadrance (tv ^+^ hv) - t*t)
355 of
356 [] -> 1 -- no scale works, just return 1
357 [s] -> s -- single solution
358 ss -> maximum ss
359 -- we will usually get both a positive and a negative solution;
360 -- return the maximum (i.e. positive) solution
361 where
362 tv = tw *^ (tangentAtStart tr # signorm)
363 hv = hw *^ (tangentAtEnd tr # signorm)
364 v = trailOffset tr
365
366 -- Calculate the approximate envelope of a horizontal arrow
367 -- as if the arrow were made only of a shaft.
368 arrowEnv :: TypeableFloat n => ArrowOpts n -> n -> Envelope V2 n
369 arrowEnv opts len = getEnvelope horizShaft
370 where
371 horizShaft = shaft # rotate (negated (v ^. _theta)) # scale (len / m)
372 m = norm v
373 v = trailOffset shaft
374 shaft = opts ^. arrowShaft
375
376 -- | @arrow len@ creates an arrow of length @len@ with default
377 -- parameters, starting at the origin and ending at the point
378 -- @(len,0)@.
379 arrow :: (TypeableFloat n, Renderable (Path V2 n) b) => n -> QDiagram b V2 n Any
380 arrow = arrow' def
381
382 -- | @arrow' opts len@ creates an arrow of length @len@ using the
383 -- given options, starting at the origin and ending at the point
384 -- @(len,0)@. In particular, it scales the given 'arrowShaft' so
385 -- that the entire arrow has length @len@.
386 arrow' :: (TypeableFloat n, Renderable (Path V2 n) b) => ArrowOpts n -> n -> QDiagram b V2 n Any
387 arrow' opts len = mkQD' (DelayedLeaf delayedArrow)
388
389 -- Currently we approximate the envelope of an arrow by using the
390 -- envelope of its shaft (see 'arrowEnv'). The trace of an arrow is empty.
391 (arrowEnv opts len) mempty mempty mempty
392
393 where
394
395 -- Once we learn the global transformation context (da) and the two scale
396 -- factors, normal to output (n) and global to output (g), this arrow is
397 -- drawn in, we can apply it to the origin and (len,0) to find out
398 -- the actual final points between which this arrow should be
399 -- drawn. We need to know this to draw it correctly, since the
400 -- head and tail are scale invariant, and hence the precise points
401 -- between which we need to draw the shaft do not transform
402 -- uniformly as the transformation applied to the entire arrow.
403 -- See https://github.com/diagrams/diagrams-lib/issues/112.
404 delayedArrow da g n =
405 let (trans, globalSty) = maybe mempty untangle . fst $ da
406 in dArrow globalSty trans len g n
407
408 -- Build an arrow and set its endpoints to the image under tr of origin and (len,0).
409 dArrow sty tr ln gToO nToO = (h' <> t' <> shaft)
410 # moveOriginBy (tWidth *^ (unit_X # rotate tAngle))
411 # rotate (((q .-. p)^._theta) ^-^ (dir^._theta))
412 # moveTo p
413 where
414
415 p = origin # transform tr
416 q = origin # translateX ln # transform tr
417
418 -- Use the existing line color for head, tail, and shaft by
419 -- default (can be overridden by explicitly setting headStyle,
420 -- tailStyle, or shaftStyle). Also use existing global line width
421 -- for shaft if not explicitly set in shaftStyle.
422 globalLC = getLineTexture <$> getAttr sty
423 opts' = opts
424 & headStyle %~ maybe id fillTexture globalLC
425 & tailStyle %~ maybe id fillTexture globalLC
426 & shaftStyle %~ applyStyle sty . transform tr
427
428 -- The head size, tail size, head gap, and tail gap are obtained
429 -- from the style and converted to output units.
430 scaleFromMeasure = fromMeasured gToO nToO . scaleLocal (avgScale tr)
431 hSize = scaleFromMeasure $ opts ^. headLength
432 tSize = scaleFromMeasure $ opts ^. tailLength
433 hGap = scaleFromMeasure $ opts ^. headGap
434 tGap = scaleFromMeasure $ opts ^. tailGap
435
436 -- Make the head and tail and save their widths.
437 (h, hWidth') = mkHead hSize opts' gToO nToO (isReflection tr)
438 (t, tWidth') = mkTail tSize opts' gToO nToO (isReflection tr)
439
440 rawShaftTrail = opts^.arrowShaft
441 shaftTrail
442 = rawShaftTrail
443 -- rotate it so it is pointing in the positive X direction
444 # rotate (negated . view _theta . trailOffset $ rawShaftTrail)
445 -- apply the context transformation -- in case it includes
446 -- things like flips and shears (the possibility of shears
447 -- is why we must rotate it to a neutral position first)
448 # transform tr
449
450 -- Adjust the head width and tail width to take gaps into account
451 tWidth = tWidth' + tGap
452 hWidth = hWidth' + hGap
453
454 -- Calculate the angles that the head and tail should point.
455 tAngle = tangentAtStart shaftTrail ^. _theta
456 hAngle = tangentAtEnd shaftTrail ^. _theta
457
458 -- Calculte the scaling factor to apply to the shaft shaftTrail so that the entire
459 -- arrow will be of length len. Then apply it to the shaft and make the
460 -- shaft into a Diagram with using its style.
461 sf = scaleFactor shaftTrail tWidth hWidth (norm (q .-. p))
462 shaftTrail' = shaftTrail # scale sf
463 shaft = strokeT shaftTrail' # applyStyle (shaftSty opts')
464
465 -- Adjust the head and tail to point in the directions of the shaft ends.
466 h' = h # rotate hAngle
467 # moveTo (origin .+^ shaftTrail' `atParam` domainUpper shaftTrail')
468 t' = t # rotate tAngle
469
470 -- Find out what direction the arrow is pointing so we can set it back
471 -- to point in the direction unitX when we are done.
472 dir = direction (trailOffset $ spine shaftTrail tWidth hWidth sf)
473
474 -- | @arrowBetween s e@ creates an arrow pointing from @s@ to @e@
475 -- with default parameters.
476 arrowBetween :: (TypeableFloat n, Renderable (Path V2 n) b) => Point V2 n -> Point V2 n -> QDiagram b V2 n Any
477 arrowBetween = arrowBetween' def
478
479 -- | @arrowBetween' opts s e@ creates an arrow pointing from @s@ to
480 -- @e@ using the given options. In particular, it scales and
481 -- rotates @arrowShaft@ to go between @s@ and @e@, taking head,
482 -- tail, and gaps into account.
483 arrowBetween'
484 :: (TypeableFloat n, Renderable (Path V2 n) b) =>
485 ArrowOpts n -> Point V2 n -> Point V2 n -> QDiagram b V2 n Any
486 arrowBetween' opts s e = arrowAt' opts s (e .-. s)
487
488 -- | Create an arrow starting at s with length and direction determined by
489 -- the vector v.
490 arrowAt :: (TypeableFloat n, Renderable (Path V2 n) b) => Point V2 n -> V2 n -> QDiagram b V2 n Any
491 arrowAt = arrowAt' def
492
493 arrowAt'
494 :: (TypeableFloat n, Renderable (Path V2 n) b) =>
495 ArrowOpts n -> Point V2 n -> V2 n -> QDiagram b V2 n Any
496 arrowAt' opts s v = arrow' opts len
497 # rotate dir # moveTo s
498 where
499 len = norm v
500 dir = v ^. _theta
501
502 -- | @arrowV v@ creates an arrow with the direction and norm of
503 -- the vector @v@ (with its tail at the origin), using default
504 -- parameters.
505 arrowV :: (TypeableFloat n, Renderable (Path V2 n) b) => V2 n -> QDiagram b V2 n Any
506 arrowV = arrowV' def
507
508 -- | @arrowV' v@ creates an arrow with the direction and norm of
509 -- the vector @v@ (with its tail at the origin).
510 arrowV'
511 :: (TypeableFloat n, Renderable (Path V2 n) b)
512 => ArrowOpts n -> V2 n -> QDiagram b V2 n Any
513 arrowV' opts = arrowAt' opts origin
514
515 -- | Turn a located trail into a default arrow by putting an
516 -- arrowhead at the end of the trail.
517 arrowFromLocatedTrail
518 :: (Renderable (Path V2 n) b, RealFloat n, Typeable n)
519 => Located (Trail V2 n) -> QDiagram b V2 n Any
520 arrowFromLocatedTrail = arrowFromLocatedTrail' def
521
522 -- | Turn a located trail into an arrow using the given options.
523 arrowFromLocatedTrail'
524 :: (Renderable (Path V2 n) b, RealFloat n, Typeable n)
525 => ArrowOpts n -> Located (Trail V2 n) -> QDiagram b V2 n Any
526 arrowFromLocatedTrail' opts trail = arrowBetween' opts' start end
527 where
528 opts' = opts & arrowShaft .~ unLoc trail
529 start = atStart trail
530 end = atEnd trail
531
532 -- | Connect two diagrams with a straight arrow.
533 connect
534 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
535 => n1 -> n2 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
536 connect = connect' def
537
538 -- | Connect two diagrams with an arbitrary arrow.
539 connect'
540 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
541 => ArrowOpts n -> n1 -> n2 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
542 connect' opts n1 n2 =
543 withName n1 $ \sub1 ->
544 withName n2 $ \sub2 ->
545 let [s,e] = map location [sub1, sub2]
546 in atop (arrowBetween' opts s e)
547
548 -- | Connect two diagrams at point on the perimeter of the diagrams, choosen
549 -- by angle.
550 connectPerim
551 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
552 => n1 -> n2 -> Angle n -> Angle n
553 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
554 connectPerim = connectPerim' def
555
556 connectPerim'
557 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
558 => ArrowOpts n -> n1 -> n2 -> Angle n -> Angle n
559 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
560 connectPerim' opts n1 n2 a1 a2 =
561 withName n1 $ \sub1 ->
562 withName n2 $ \sub2 ->
563 let [os, oe] = map location [sub1, sub2]
564 s = fromMaybe os (maxTraceP os (unitX # rotate a1) sub1)
565 e = fromMaybe oe (maxTraceP oe (unitX # rotate a2) sub2)
566 in atop (arrowBetween' opts s e)
567
568 -- | Draw an arrow from diagram named "n1" to diagram named "n2". The
569 -- arrow lies on the line between the centres of the diagrams, but is
570 -- drawn so that it stops at the boundaries of the diagrams, using traces
571 -- to find the intersection points.
572 connectOutside
573 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
574 => n1 -> n2 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
575 connectOutside = connectOutside' def
576
577 connectOutside'
578 :: (TypeableFloat n, Renderable (Path V2 n) b, IsName n1, IsName n2)
579 => ArrowOpts n -> n1 -> n2 -> QDiagram b V2 n Any -> QDiagram b V2 n Any
580 connectOutside' opts n1 n2 =
581 withName n1 $ \b1 ->
582 withName n2 $ \b2 ->
583 let v = location b2 .-. location b1
584 midpoint = location b1 .+^ (v ^/ 2)
585 s' = fromMaybe (location b1) $ traceP midpoint (negated v) b1
586 e' = fromMaybe (location b2) $ traceP midpoint v b2
587 in
588 atop (arrowBetween' opts s' e')