# How alternate colors in dashed arrows?

How might I alternate two colors, say Red and Blue, for the dashes on a pair of oppositely pointing arrows, such as in the following, so that the gaps between the blue dashes are filled with red dashes and vice versa?

Note that, for reasons beyond the scope of this question, I do not want to use a single arrow having two arrowheads, that is, I do not want to use something like Arrowheads[{-0.03,0.03}] that puts arrowheads on both ends of a single arrow.

{p,q}=RandomPoint[Disk[],2];
Graphics[{PointSize@Large,
Red,Point@p,Blue,Point@q,
Dashing[0.03],
Arrow[{p,q}],Arrow[{q,p}]
},
PlotRange->1.1]


• Dashing[{}], Arrow[{p, q}], Red, Dashing[0.03], Arrow[{q, p}] comes pretty close to what you're describing. It relies on the fact that the red dashes are drawn in front of the solid blue arrow. The downside is that the red dashes are also drawn over the blue arrowhead, which looks awkward. Commented Feb 22, 2022 at 21:59
• @MichaelSeifert, hack it with an offset: {Dashing[{}], Arrow[{p, q}], Red, Dashing[0.03], Arrow[{q + 0.06 (p - q), p}]} Commented Feb 22, 2022 at 22:08

Use the second argument of Dashing as offset and overlay two dashed lines.

SeedRandom[1];
{p, q} = RandomPoint[Disk[], 2];

alternatingArrow[p_, q_, color1_, color2_, r_] := {
color1, Dashing[r], Line[{p, q}],
color2, Dashing[r, r], Line[{p, q}],
color1, Arrow[{p + 0.999 (q - p), q}],
color2, Arrow[{q + 0.999 (p - q), p}]
}

Graphics[{Thickness[0.005], alternatingArrow[p, q, Blue, Red, 0.03],
PointSize@Large, Red, Point@p, Blue, Point@q}]


Alternatively, as proposed by @MichaelSeifert in his comment, you can plot one solid line, overlaid with a dashed one. However, note that in this case the red dashes are a bit awkward at their edges because of the antialising with the underlying blue line.

alternatingArrow2[p_, q_, color1_, color2_, r_] := {
color1, Arrow[{p, q}],
color2, Dashing[r, r], Arrow[{q + r (p - q), p}]
}

Graphics[{Thickness[0.005], alternatingArrow2[p, q, Blue, Red, 0.03],
PointSize@Large, Red, Point@p, Blue, Point@q}]


• Note that the lines have to be drawn in the same direction for this to work (i.e., both are in the order {p,q}.) I was trying to make it work with one reversed, a tactic I do not recommend. Commented Feb 22, 2022 at 22:08
• @Domen: Just as I said I need two arrows, one in each direction, so if lines are going to be used, then I need two lines, one in each direction. (In the particular application I have in mind, faking things with two lines going in the same direction would distort and confuse the intent!) Commented Feb 22, 2022 at 22:11
• @murray Composing primitives into more complex expressions is the core of programming. I think it's exactly opposite to "distorting and confusing the intent" to create a special-purpose function with a semantically suggestive name like alternatingArrow. You have two arrows pointing in opposite directions. They each have their own arrowhead, which you were adamant about. There is even a clear path to evolving this solution to having different dashing patterns or even more colors. Maybe rather than being dismissive of the suggestion, you can be clearer about how it doesn't satisfy your need. Commented Feb 22, 2022 at 22:26
• @lericr: In my application, I use a certain (mathematical) function to get from one point to a 2nd point, then another such function to get from the 2nd point to a 3rd point, and I'm trying to show graphically that the 3rd point is the same as the first. I may be able to adapt the proposed answer to handle that. Commented Feb 22, 2022 at 22:49
• Maybe you can use Arrows that aren’t lines. They could look like a loop and they would be more distinguishable as two separate things instead of looking like a single striped thing. Commented Feb 22, 2022 at 22:57