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