8

The following code generates a graph.

LayeredGraphPlot[
    {{"i" -> "m", 1}, {"i" -> "...", 1}, {"i" -> "n", 1}, {"m" -> "j", 2}, {"..." -> "j", 2}, {"n" -> "j", 2}}, 
    DirectedEdges -> True, 
    VertexLabeling -> True, 
    PlotStyle -> {FontSize -> 14}, 
    EdgeRenderingFunction -> (Switch[#3, 1, {Dashed, Arrow[#1, .1]}, 2, {Arrow[#1, .1]}] &)]

enter image description here

My question is that is it possible to draw curves between "i" and each of "m", "...", "n" instead of straight lines, like the red curve below? Thank you. enter image description here

Tony
  • 1,020
  • 6
  • 17

1 Answers1

14

You can construct a BezierCurve by adding two control points to the list of vertex coordinates for the two vertices (p[[1]] and p[[2]]) incident to an edge.

In the following function bC with three arguments ({t1,t2,t3}), the two additional points are obtained by taking two points on the line joining the two vertex coordinates, t1 p[[1]] + (1-t1) p[[2]] and t2 p[[1]] + (1-t2) p[[2]] where 0<= t1, t2 <=1, and adding to these points {0,t3} or {t3,0} to ensure that the resulting four points are not collinear. The default values for the three arguments are {t1,t2,t3}= {1/3,2/3,1/3}.

ClearAll[bC]
bC[t1_:(1/3),t2_:(2/3), t3_:(1/3)]:= With[{p=#}, 
    BezierCurve[{p[[1]],
                 t1 p[[1]]+(1-t1)p[[2]]+If[p[[1,1]]==p[[2,1]],{t3,0},{0,t3}],
                 t2 p[[1]]+(1-t2)p[[2]]-If[p[[1,1]]==p[[2,1]],{t3,0},{0,t3}],
                 p[[2]]}]]&;

LayeredGraphPlot[ {{"i" -> "m", 1}, {"i" -> "...", 1}, {"i" -> "n", 1}, 
                   {"m" -> "j", 2}, {"..." -> "j", 2}, {"n" -> "j", 2}}, 
    DirectedEdges -> True, VertexLabeling -> True, 
    PlotStyle -> {FontSize -> 14}, 
    EdgeRenderingFunction -> (Switch[#3, 1, {Dashed,Arrow[bC[][#],.1]}, 2, {Arrow[#1, .1]}] &)]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Why did you reopen this question, please? – Mr.Wizard Apr 08 '15 at 23:00
  • @Mr.Wizard, I was puzzling over why this q/a was reopened and appeared with a "modified by kguler" tag. I must have inadvertently clicked the reopen button. Sorry. – kglr Apr 08 '15 at 23:16
  • That's okay. It just surprised me as I didn't see any precipitating comments or recent edits. Why don't you close it now? I think this was the previous duplicate if I am reading the timeline correctly. – Mr.Wizard Apr 09 '15 at 00:57
  • Mr.Wizard, how come my accidental click made the previous close votes and links disappear? – kglr Apr 09 '15 at 01:01
  • 1
    You have the Gold tag badge for plotting (and list-manipulation) which gives you moderator-like single vote power to close (and apparently reopen) duplicates in this tag. Use it wisely. :-) (Possible duplicate: comments are automatically deleted when the close occurs.) – Mr.Wizard Apr 09 '15 at 01:22
  • Thank you @Mr.Wizard. I wasn't aware of the badges/power relation. – kglr Apr 09 '15 at 01:33
  • Full details here: (231212) – Mr.Wizard Apr 09 '15 at 01:34