@@ -58,9 +58,21 @@ func determineStartAndEndDir(e *edge) (direction, direction, direction, directio
5858 }
5959 d := determineDirection (genericCoord (* e .from .gridCoord ), genericCoord (* e .to .gridCoord ))
6060 var preferredDir , preferredOppositeDir , alternativeDir , alternativeOppositeDir direction
61+
62+ // Check if this is a backwards flowing edge
63+ isBackwards := false
64+ if graphDirection == "LR" {
65+ // In LR mode, backwards flow is when edge goes from right to left (Left direction)
66+ isBackwards = (d == Left || d == UpperLeft || d == LowerLeft )
67+ } else { // TD mode
68+ // In TD mode, backwards flow is when edge goes from bottom to top (Up direction)
69+ isBackwards = (d == Up || d == UpperLeft || d == UpperRight )
70+ }
71+
6172 // LR: prefer vertical over horizontal
6273 // TD: prefer horizontal over vertical
6374 // TODO: This causes some squirmy lines if the corner spot is already occupied.
75+ // For backwards edges, use special start positions: Down in LR mode, Right in TD mode
6476 switch d {
6577 case LowerRight :
6678 if graphDirection == "LR" {
@@ -88,8 +100,9 @@ func determineStartAndEndDir(e *edge) (direction, direction, direction, directio
88100 }
89101 case LowerLeft :
90102 if graphDirection == "LR" {
103+ // Backwards flow in LR mode - start from Down, arrive at Down
91104 preferredDir = Down
92- preferredOppositeDir = Right
105+ preferredOppositeDir = Down // Edge goes to bottom of destination
93106 alternativeDir = Left
94107 alternativeOppositeDir = Up
95108 } else {
@@ -100,22 +113,46 @@ func determineStartAndEndDir(e *edge) (direction, direction, direction, directio
100113 }
101114 case UpperLeft :
102115 if graphDirection == "LR" {
103- preferredDir = Up
104- preferredOppositeDir = Right
116+ // Backwards flow in LR mode - start from Down, arrive at Down
117+ preferredDir = Down
118+ preferredOppositeDir = Down // Edge goes to bottom of destination
105119 alternativeDir = Left
106120 alternativeOppositeDir = Down
107121 } else {
108- preferredDir = Left
109- preferredOppositeDir = Down
122+ // Backwards flow in TD mode - start from Right, arrive at Right
123+ preferredDir = Right
124+ preferredOppositeDir = Right // Edge goes to right of destination
110125 alternativeDir = Up
111126 alternativeOppositeDir = Right
112127 }
113128 default :
114- preferredDir = d
115- preferredOppositeDir = preferredDir .getOpposite ()
116- // TODO: just return null and don't calculate alternative path
117- alternativeDir = d
118- alternativeOppositeDir = preferredOppositeDir
129+ // Handle direct backwards flow cases
130+ if isBackwards {
131+ if graphDirection == "LR" && d == Left {
132+ // Direct left flow in LR mode - start from Down, arrive at Down
133+ preferredDir = Down
134+ preferredOppositeDir = Down // Edge goes to bottom of destination
135+ alternativeDir = Left
136+ alternativeOppositeDir = Right
137+ } else if graphDirection == "TD" && d == Up {
138+ // Direct up flow in TD mode - start from Right, arrive at Right
139+ preferredDir = Right
140+ preferredOppositeDir = Right // Edge goes to right of destination
141+ alternativeDir = Up
142+ alternativeOppositeDir = Down
143+ } else {
144+ preferredDir = d
145+ preferredOppositeDir = preferredDir .getOpposite ()
146+ alternativeDir = d
147+ alternativeOppositeDir = preferredOppositeDir
148+ }
149+ } else {
150+ preferredDir = d
151+ preferredOppositeDir = preferredDir .getOpposite ()
152+ // TODO: just return null and don't calculate alternative path
153+ alternativeDir = d
154+ alternativeOppositeDir = preferredOppositeDir
155+ }
119156 }
120157 return preferredDir , preferredOppositeDir , alternativeDir , alternativeOppositeDir
121158}
0 commit comments