Debian Codegraph @

src/Diagrams/TwoD/Arrowheads.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
6 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
7 -----------------------------------------------------------------------------
8 -- |
9 -- Module : Diagrams.TwoD.Arrowheads
10 -- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
11 -- License : BSD-style (see LICENSE)
12 -- Maintainer : [email protected]
13 --
14 -- Standard arrowheads and tails. Each arrowhead or tail is designed
15 -- to be drawn filled, with a line width of 0, and is normalized to
16 -- fit inside a circle of diameter 1.
17 --
18 -----------------------------------------------------------------------------
19
20 module Diagrams.TwoD.Arrowheads
21 (
22 -- * Arrowheads
23 -- ** Standard arrowheads
24 tri
25 , dart
26 , halfDart
27 , spike
28 , thorn
29 , lineHead
30 , noHead
31
32 -- ** Configurable arrowheads
33 -- | Creates arrowheads of the same shape as the standard heads but
34 -- where the angle parameter is used to specify the angle to the top
35 -- left point of the arrowhead.
36 , arrowheadTriangle
37 , arrowheadDart
38 , arrowheadHalfDart
39 , arrowheadSpike
40 , arrowheadThorn
41
42 -- * Arrow tails
43 -- ** Standard arrow tails
44 , tri'
45 , dart'
46 , halfDart'
47 , spike'
48 , thorn'
49 , lineTail
50 , noTail
51 , quill
52 , block
53
54 -- ** Configurable arrow tails
55
56 , arrowtailQuill
57 , arrowtailBlock
58
59 -- * Internals
60 , ArrowHT
61 ) where
62
63 import Control.Lens ((&), (.~), (<>~), (^.))
64 import Data.Default
65 import Data.Monoid (mempty, (<>))
66
67 import Diagrams.Angle
68 import Diagrams.Core
69
70 import Diagrams.Path
71 import Diagrams.Segment
72 import Diagrams.Trail
73 import Diagrams.TrailLike (fromOffsets)
74 import Diagrams.TwoD.Align
75 import Diagrams.TwoD.Arc (arc')
76 import Diagrams.TwoD.Path ()
77 import Diagrams.TwoD.Polygons
78 import Diagrams.TwoD.Shapes
79 import Diagrams.TwoD.Transform
80 import Diagrams.TwoD.Types
81 import Diagrams.TwoD.Vector (unitX, unit_X, xDir)
82 import Diagrams.Util (( # ))
83
84 import Linear.Affine
85 import Linear.Metric
86 import Linear.Vector
87
88 -----------------------------------------------------------------------------
89
90 type ArrowHT n = n -> n -> (Path V2 n, Path V2 n)
91
92 closedPath :: OrderedField n => Trail V2 n -> Path V2 n
93 closedPath = pathFromTrail . closeTrail
94
95 -- Heads ------------------------------------------------------------------
96 -- > drawHead h = arrowAt' (with & arrowHead .~ h & shaftStyle %~ lw none
97 -- > & headLength .~ local 0.5)
98 -- > origin (r2 (1, 0))
99 -- > <> square 0.5 # alignL # lw none # frame 0.1
100
101 -- | Isoceles triangle style. The above example specifies an angle of `2/5 Turn`.
102
103 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_tri25Ex.svg#diagram=tri25Ex&width=120>>
104
105 -- > tri25Ex = arrowAt' (with & arrowHead .~ arrowheadTriangle (2/5 @@ turn)
106 -- > & shaftStyle %~ lw none & headLength .~ local 0.5)
107 -- > origin (r2 (0.5, 0))
108 -- > <> square 0.6 # alignL # lw none # frame 0.1
109 arrowheadTriangle :: RealFloat n => Angle n -> ArrowHT n
110 arrowheadTriangle theta = aHead
111 where
112 aHead len _ = (p, mempty)
113 where
114 psi = pi - (theta ^. rad)
115 r = len / (1 + cos psi)
116 p = polygon (def & polyType .~ PolyPolar [theta, (-2) *^ theta]
117 (repeat r) & polyOrient .~ NoOrient) # alignL
118
119
120 -- | Isoceles triangle with linear concave base. Inkscape type 1 - dart like.
121 arrowheadDart :: RealFloat n => Angle n -> ArrowHT n
122 arrowheadDart theta len shaftWidth = (hd # scale sz, jt)
123 where
124 hd = snugL . pathFromTrail . glueTrail $ fromOffsets [t1, t2, b2, b1]
125 jt = pathFromTrail . glueTrail $ j <> reflectY j
126 j = closeTrail $ fromOffsets [V2 (-jLength) 0, V2 0 (shaftWidth / 2)]
127 v = rotate theta unitX
128 (t1, t2) = (unit_X ^+^ v, V2 (-0.5) 0 ^-^ v)
129 [b1, b2] = map (reflectY . negated) [t1, t2]
130 psi = pi - negated t2 ^. _theta . rad
131 jLength = shaftWidth / (2 * tan psi)
132
133 -- If the shaft is too wide, set the size to a default value of 1.
134 sz = max 1 ((len - jLength) / 1.5)
135
136 -- | Top half of an 'arrowheadDart'.
137 arrowheadHalfDart :: RealFloat n => Angle n -> ArrowHT n
138 arrowheadHalfDart theta len shaftWidth = (hd, jt)
139 where
140 hd = fromOffsets [t1, t2]
141 # closeTrail # pathFromTrail
142 # translateX 1.5 # scale sz
143 # translateY (-shaftWidth/2)
144 # snugL
145 jt = snugR . translateY (-shaftWidth/2) . pathFromTrail . closeTrail $ fromOffsets [V2 (-jLength) 0, V2 0 shaftWidth]
146 v = rotate theta unitX
147 (t1, t2) = (unit_X ^+^ v, (0.5 *^ unit_X) ^-^ v)
148 psi = pi - negated t2 ^. _theta . rad
149 jLength = shaftWidth / tan psi
150
151 -- If the shaft is too wide, set the size to a default value of 1.
152 sz = max 1 ((len - jLength) / 1.5)
153
154 -- | Isoceles triangle with curved concave base. Inkscape type 2.
155 arrowheadSpike :: RealFloat n => Angle n -> ArrowHT n
156 arrowheadSpike theta len shaftWidth = (hd # scale r, jt # scale r)
157 where
158 hd = snugL . closedPath $ l1 <> c <> l2
159 jt = alignR . centerY . pathFromTrail
160 . closeTrail $ arc' 1 (xDir & _theta <>~ negated phi) (2 *^ phi)
161 l1 = trailFromSegments [straight $ unit_X ^+^ v]
162 l2 = trailFromSegments [reverseSegment . straight $ (unit_X ^+^ reflectY v)]
163 c = arc' 1 (rotate α xDir) ((-2) *^ α)
164 α = (1/2 @@ turn) ^-^ theta
165 v = rotate theta unitX
166
167 -- The length of the head without its joint is, -2r cos theta and
168 -- the length of the joint is r - sqrt (r^2 - y^2). So the total
169 -- length of the arrow head is given by r(1 - 2 cos theta)-sqrt (r^2-y^2).
170 -- Solving the quadratic gives two roots, we want the larger one.
171
172 -- 1/4 turn < theta < 2/3 turn.
173 a = 1 - 2 * cos (theta ^. rad)
174 y = shaftWidth / 2
175
176 -- If the shaft is too wide for the head, we default the radius r to
177 -- 2/3 * len by setting d=1 and phi=pi/2.
178 d = max 1 (len**2 + (1 - a**2) * y**2)
179 r = (a * len + sqrt d) / (a**2 -1)
180 phi = asinA (min 1 (y/r))
181
182 -- | Curved sides, linear concave base. Illustrator CS5 #3
183 arrowheadThorn :: RealFloat n => Angle n -> ArrowHT n
184 arrowheadThorn theta len shaftWidth = (hd # scale sz, jt)
185 where
186 hd = snugL . pathFromTrail . glueTrail $ hTop <> reflectY hTop
187 hTop = closeTrail . trailFromSegments $ [c, l]
188 jt = pathFromTrail . glueTrail $ j <> reflectY j
189 j = closeTrail $ fromOffsets [V2 (-jLength) 0, V2 0 (shaftWidth / 2)]
190 c = curvedSide theta
191 v = rotate theta unitX
192 l = reverseSegment . straight $ t
193 t = v ^-^ V2 (-0.5) 0
194 psi = fullTurn ^/ 2 ^-^ (negated t ^. _theta)
195 jLength = shaftWidth / (2 * tanA psi)
196
197 -- If the shaft if too wide, set the size to a default value of 1.
198 sz = max 1 ((len - jLength) / 1.5)
199
200 -- | Make a side for the thorn head.
201 curvedSide :: Floating n => Angle n -> Segment Closed V2 n
202 curvedSide theta = bezier3 ctrl1 ctrl2 end
203 where
204 v0 = unit_X
205 v1 = rotate theta unitX
206 ctrl1 = v0
207 ctrl2 = v0 ^+^ v1
208 end = v0 ^+^ v1
209
210 -- Standard heads ---------------------------------------------------------
211 -- | A line the same width as the shaft.
212 lineHead :: RealFloat n => ArrowHT n
213 lineHead s w = (square 1 # scaleX s # scaleY w # alignL, mempty)
214
215 noHead :: ArrowHT n
216 noHead _ _ = (mempty, mempty)
217
218 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_triEx.svg#diagram=triEx&width=100>>
219
220 -- > triEx = drawHead tri
221 tri :: RealFloat n => ArrowHT n
222 tri = arrowheadTriangle (1/3 @@ turn)
223
224 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_spikeEx.svg#diagram=spikeEx&width=100>>
225
226 -- > spikeEx = drawHead spike
227 spike :: RealFloat n => ArrowHT n
228 spike = arrowheadSpike (3/8 @@ turn)
229
230 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_thornEx.svg#diagram=thornEx&width=100>>
231
232 -- > thornEx = drawHead thorn
233 thorn :: RealFloat n => ArrowHT n
234 thorn = arrowheadThorn (3/8 @@ turn)
235
236 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_dartEx.svg#diagram=dartEx&width=100>>
237
238 -- > dartEx = drawHead dart
239 dart :: RealFloat n => ArrowHT n
240 dart = arrowheadDart (2/5 @@ turn)
241
242 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_halfDartEx.svg#diagram=halfDartEx&width=100>>
243
244 -- > halfDartEx = drawHead halfDart
245 halfDart :: RealFloat n => ArrowHT n
246 halfDart = arrowheadHalfDart (2/5 @@ turn)
247
248 -- Tails ------------------------------------------------------------------
249 -- > drawTail t = arrowAt' (with & arrowTail .~ t & shaftStyle %~ lw none
250 -- > & arrowHead .~ noHead & tailLength .~ local 0.5)
251 -- > origin (r2 (1, 0))
252 -- > <> square 0.5 # alignL # lw none # frame 0.1
253
254 -- | Utility function to convert any arrowhead to an arrowtail, i.e.
255 -- attached at the start of the trail.
256 headToTail :: OrderedField n => ArrowHT n -> ArrowHT n
257 headToTail hd = tl
258 where
259 tl sz shaftWidth = (t, j)
260 where
261 (t', j') = hd sz shaftWidth
262 t = reflectX t'
263 j = reflectX j'
264
265 arrowtailBlock :: forall n. (RealFloat n) => Angle n -> ArrowHT n
266 arrowtailBlock theta = aTail
267 where
268 aTail len _ = (t, mempty)
269 where
270 t = rect len (len * x) # alignR
271 a' :: V2 n
272 a' = rotate theta unitX
273 a = a' ^-^ reflectY a'
274 x = norm a
275
276 -- | The angle is where the top left corner intersects the circle.
277 arrowtailQuill :: OrderedField n => Angle n -> ArrowHT n
278 arrowtailQuill theta = aTail
279 where
280 aTail len shaftWidth = (t, j)
281 where
282 t = closedPath (trailFromVertices [v0, v1, v2, v3, v4, v5, v0])
283 # scale sz # alignR
284 sz = len / 0.6
285 v0 = p2 (0.5, 0)
286 v2 = origin .+^ (rotate theta unitX # scale 0.5)
287 v1 = v2 # translateX (5/8)
288 v3 = p2 (-0.1, 0)
289 v4 = v2 # reflectY
290 v5 = v4 # translateX (5/8)
291 s = 1 - shaftWidth / norm (v1 .-. v5)
292 n1 = v0 # translateY (0.5 * shaftWidth)
293 n2 = v1 .-^ ((v1 .-. v0) # scale s)
294 n3 = v5 .-^ ((v5 .-. v0) # scale s)
295 n4 = n1 # reflectY
296 j = closedPath $ trailFromVertices [v0, n1, n2, v0, n3, n4, v0]
297
298 -- Standard tails ---------------------------------------------------------
299 -- | A line the same width as the shaft.
300 lineTail :: RealFloat n => ArrowHT n
301 lineTail s w = (square 1 # scaleY w # scaleX s # alignR, mempty)
302
303 noTail :: ArrowHT n
304 noTail _ _ = (mempty, mempty)
305
306 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_tri'Ex.svg#diagram=tri'Ex&width=100>>
307
308 -- > tri'Ex = drawTail tri'
309 tri' :: RealFloat n => ArrowHT n
310 tri' = headToTail tri
311
312 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_spike'Ex.svg#diagram=spike'Ex&width=100>>
313
314 -- > spike'Ex = drawTail spike'
315 spike' :: RealFloat n => ArrowHT n
316 spike' = headToTail spike
317
318 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_thorn'Ex.svg#diagram=thorn'Ex&width=100>>
319
320 -- > thorn'Ex = drawTail thorn'
321 thorn' :: RealFloat n => ArrowHT n
322 thorn' = headToTail thorn
323
324 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_dart'Ex.svg#diagram=dart'Ex&width=100>>
325
326 -- > dart'Ex = drawTail dart'
327 dart' :: RealFloat n => ArrowHT n
328 dart' = headToTail dart
329
330 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_halfDart'Ex.svg#diagram=halfDart'Ex&width=100>>
331
332 -- > halfDart'Ex = drawTail halfDart'
333 halfDart' :: RealFloat n => ArrowHT n
334 halfDart' = headToTail halfDart
335
336 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_quillEx.svg#diagram=quillEx&width=100>>
337
338 -- > quillEx = drawTail quill
339 quill :: (Floating n, Ord n) => ArrowHT n
340 quill = arrowtailQuill (2/5 @@ turn)
341
342 -- | <<diagrams/src_Diagrams_TwoD_Arrowheads_blockEx.svg#diagram=blockEx&width=100>>
343
344 -- > blockEx = drawTail block
345 block :: RealFloat n => ArrowHT n
346 block = arrowtailBlock (7/16 @@ turn)