|
1 | 1 | module Arrows |
2 | 2 |
|
3 | | - export Arrow, arrow, add_arrows |
| 3 | +export Arrow, arrow, add_arrows |
4 | 4 |
|
5 | | - using ..PlotsBase.Commons |
| 5 | +using ..PlotsBase.Commons |
6 | 6 |
|
7 | | - # style is :open or :closed (for now) |
8 | | - struct Arrow |
9 | | - style::Symbol |
10 | | - side::Symbol # :head (default), :tail, or :both |
11 | | - headlength::Float64 |
12 | | - headwidth::Float64 |
13 | | - end |
| 7 | +# style is :open or :closed (for now) |
| 8 | +struct Arrow |
| 9 | + style::Symbol |
| 10 | + side::Symbol # :head (default), :tail, or :both |
| 11 | + headlength::Float64 |
| 12 | + headwidth::Float64 |
| 13 | +end |
14 | 14 |
|
15 | | - """ |
16 | | - arrow(args...) |
| 15 | +""" |
| 16 | + arrow(args...) |
17 | 17 |
|
18 | | - Define arrowheads to apply to lines - args are `style` (`:open` or `:closed`), |
19 | | - `side` (`:head`, `:tail` or `:both`), `headlength` and `headwidth` |
20 | | - """ |
21 | | - function arrow(args...) |
22 | | - style, side = :simple, :head |
23 | | - headlength = headwidth = 1 |
24 | | - setlength = false |
25 | | - for arg in args |
26 | | - T = typeof(arg) |
27 | | - if T == Symbol |
28 | | - if arg in (:head, :tail, :both) |
29 | | - side = arg |
30 | | - else |
31 | | - style = arg |
32 | | - end |
33 | | - elseif T <: Number |
34 | | - # first we apply to both, but if there's more, then only change width after the first number |
35 | | - headwidth = Float64(arg) |
36 | | - if !setlength |
37 | | - headlength = headwidth |
38 | | - end |
39 | | - setlength = true |
40 | | - elseif T <: Tuple && length(arg) == 2 |
41 | | - headlength, headwidth = Float64(arg[1]), Float64(arg[2]) |
| 18 | +Define arrowheads to apply to lines - args are `style` (`:open` or `:closed`), |
| 19 | +`side` (`:head`, `:tail` or `:both`), `headlength` and `headwidth` |
| 20 | +""" |
| 21 | +function arrow(args...) |
| 22 | + style, side = :simple, :head |
| 23 | + headlength = headwidth = 1 |
| 24 | + setlength = false |
| 25 | + for arg in args |
| 26 | + T = typeof(arg) |
| 27 | + if T == Symbol |
| 28 | + if arg in (:head, :tail, :both) |
| 29 | + side = arg |
42 | 30 | else |
43 | | - @warn "Skipped arrow arg $arg" |
| 31 | + style = arg |
| 32 | + end |
| 33 | + elseif T <: Number |
| 34 | + # first we apply to both, but if there's more, then only change width after the first number |
| 35 | + headwidth = Float64(arg) |
| 36 | + if !setlength |
| 37 | + headlength = headwidth |
44 | 38 | end |
| 39 | + setlength = true |
| 40 | + elseif T <: Tuple && length(arg) == 2 |
| 41 | + headlength, headwidth = Float64(arg[1]), Float64(arg[2]) |
| 42 | + else |
| 43 | + @warn "Skipped arrow arg $arg" |
45 | 44 | end |
46 | | - return Arrow(style, side, headlength, headwidth) |
47 | 45 | end |
| 46 | + return Arrow(style, side, headlength, headwidth) |
| 47 | +end |
48 | 48 |
|
49 | | - # allow for do-block notation which gets called on every valid start/end pair which |
50 | | - # we need to draw an arrow |
51 | | - function add_arrows(func::Function, x::AVec, y::AVec) |
52 | | - for i in 2:length(x) |
53 | | - xyprev = (x[i - 1], y[i - 1]) |
54 | | - xy = (x[i], y[i]) |
55 | | - if ok(xyprev) && ok(xy) |
56 | | - if i == length(x) || !ok(x[i + 1], y[i + 1]) |
57 | | - # add the arrow from xyprev to xy |
58 | | - func(xyprev, xy) |
59 | | - end |
| 49 | +# allow for do-block notation which gets called on every valid start/end pair which |
| 50 | +# we need to draw an arrow |
| 51 | +function add_arrows(func::Function, x::AVec, y::AVec) |
| 52 | + for i in 2:length(x) |
| 53 | + xyprev = (x[i - 1], y[i - 1]) |
| 54 | + xy = (x[i], y[i]) |
| 55 | + if ok(xyprev) && ok(xy) |
| 56 | + if i == length(x) || !ok(x[i + 1], y[i + 1]) |
| 57 | + # add the arrow from xyprev to xy |
| 58 | + func(xyprev, xy) |
60 | 59 | end |
61 | 60 | end |
62 | | - return |
63 | 61 | end |
| 62 | + return |
| 63 | +end |
64 | 64 |
|
65 | 65 | end # module |
66 | | - |
67 | | -# ----------------------------------------------------------------------------- |
68 | | - |
69 | | -using .Arrows |
0 commit comments