3
$\begingroup$

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,
Thickness[0.005],Arrowheads[0.03],
Dashing[0.03],
Arrow[{p,q}],Arrow[{q,p}]
},
PlotRange->1.1]

How alternate colors of dashes and gaps?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Feb 22, 2022 at 21:59
  • $\begingroup$ @MichaelSeifert, hack it with an offset: {Dashing[{}], Arrow[{p, q}], Red, Dashing[0.03], Arrow[{q + 0.06 (p - q), p}]} $\endgroup$
    – Domen
    Feb 22, 2022 at 22:08

1 Answer 1

8
$\begingroup$

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}]

Mathematica graphics

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}]

Mathematica graphics

$\endgroup$
8
  • 1
    $\begingroup$ 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. $\endgroup$ Feb 22, 2022 at 22:08
  • $\begingroup$ @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!) $\endgroup$
    – murray
    Feb 22, 2022 at 22:11
  • 2
    $\begingroup$ @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. $\endgroup$
    – lericr
    Feb 22, 2022 at 22:26
  • $\begingroup$ @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. $\endgroup$
    – murray
    Feb 22, 2022 at 22:49
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Feb 22, 2022 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.