@@ -60,6 +60,44 @@ export default {
60
60
}
61
61
}
62
62
63
+ /**
64
+ * compute node positions during rotation
65
+ * given x-y coordinates of the root and the child, compute the
66
+ * intermediate position after step 1 or step 2
67
+ * coords represented as record with fields rX, rY, cX, cY (root X&Y,
68
+ * child X&Y)
69
+ * We keep the edge the same length and rotate it around the point 40%
70
+ * along the edge from the root. If initially the y coordinate of the
71
+ * root is 1 and the child is zero, after step 1 the root is at 2/3
72
+ * and the child is at 1/2 and after step 2 the root is at 1/3 and the
73
+ * child is at 1. The x coordinates are computed so as to keep the length
74
+ * of the edge same (magic numbers derived from maths).
75
+ * returns record with new coordinates
76
+ */
77
+ function rotateStep ( pos0 , step ) {
78
+
79
+ let { rX, rY, cX, cY} = pos0 ;
80
+ let deltaX = rX - cX ;
81
+ let deltaY = rY - cY ;
82
+ // edge length = sqrt(deltaX**2 + deltaY**2)
83
+ let deltaX1 ; // deltaX for new position
84
+ let pos1 = { rX :0 , rY :0 , cX :0 , cY :0 } ;
85
+ if ( step == 1 ) {
86
+ pos1 . rY = ( 2 * rY + cY ) / 3 ;
87
+ pos1 . cY = ( rY + cY ) / 2 ;
88
+ deltaX1 = Math . sqrt ( ( 35 / 36 ) * deltaY ** 2 + deltaX ** 2 ) ;
89
+ } else { // assume step == 2
90
+ pos1 . rY = ( rY + 2 * cY ) / 3 ;
91
+ pos1 . cY = rY ;
92
+ deltaX1 = Math . sqrt ( ( 5 / 9 ) * deltaY ** 2 + deltaX ** 2 ) ;
93
+ }
94
+ if ( rX > cX ) // reverse direction of deltaX
95
+ deltaX1 = - deltaX1 ;
96
+ pos1 . rX = ( 0.6 * rX + 0.4 * cX ) - 0.4 * deltaX1 ;
97
+ pos1 . cX = ( 0.6 * rX + 0.4 * cX ) + 0.6 * deltaX1 ;
98
+ return pos1 ;
99
+ }
100
+
63
101
// We apply the following char notations in our rotation code.
64
102
// It helps code reusability and readability.
65
103
// tNum / charNotation
@@ -116,17 +154,11 @@ export default {
116
154
let rootNode = vis . graph . findNode ( tt6 ) ;
117
155
let leafNode = vis . graph . findNode ( tt2 ) ;
118
156
// store the previous height of the root node
119
- vis . graph . storePrevHeight ( rootNode . y ) ;
120
- // calculate the new position of the nodes
121
- let newY = ( rootNode . y + leafNode . y ) / 2 ;
122
- let moveX = 0 ;
123
- // avoid overlapping
124
- if ( ( rootNode . x - leafNode . x ) < nodeSize ) {
125
- moveX = nodeSize - ( rootNode . x - leafNode . x ) ;
126
- }
127
- // set the new position of the nodes
128
- vis . graph . setNodePosition ( tt6 , rootNode . x + moveX , newY ) ;
129
- vis . graph . setNodePosition ( tt2 , leafNode . x - moveX , newY ) ;
157
+ let pos0 = { rX :rootNode . x , rY :rootNode . y , cX :leafNode . x , cY :leafNode . y } ;
158
+ vis . graph . setRotPos ( pos0 ) ;
159
+ let pos = rotateStep ( pos0 , 1 ) ; // compute new position
160
+ vis . graph . setNodePosition ( tt6 , pos . rX , pos . rY ) ;
161
+ vis . graph . setNodePosition ( tt2 , pos . cX , pos . cY ) ;
130
162
} ,
131
163
[ A . key , R . key ] ,
132
164
depth
@@ -144,14 +176,14 @@ export default {
144
176
}
145
177
146
178
// -- following code is for visualising the rotation step by step --
147
- let rootNode = vis . graph . findNode ( tt6 ) ;
179
+ let rootNode = vis . graph . findNode ( tt2 ) ;
148
180
let leafNode = vis . graph . findNode ( tt2 ) ;
149
181
// freeze the layout to avoid the nodes moving automatically
150
182
vis . graph . setPauseLayout ( true ) ;
151
- let move = ( rootNode . x - leafNode . x ) / 5 ;
152
- // set the new position of the nodes
153
- vis . graph . setNodePosition ( tt2 , leafNode . x + move , vis . graph . getPrevHeight ( ) ) ;
154
- vis . graph . setNodePosition ( tt6 , rootNode . x - move , rootNode . y + ( rootNode . y - leafNode . y ) * 0.5 ) ;
183
+ let pos0 = vis . graph . getRotPos ( ) ; // original position
184
+ let pos = rotateStep ( pos0 , 2 ) ; // compute new position
185
+ vis . graph . setNodePosition ( tt6 , pos . rX , pos . rY ) ;
186
+ vis . graph . setNodePosition ( tt2 , pos . cX , pos . cY ) ;
155
187
} ,
156
188
[ R . key , A . key , D ? D . key : false ] ,
157
189
depth
@@ -274,19 +306,14 @@ export default {
274
306
275
307
// -- following code is for visualising the rotation step by step --
276
308
// find the position of the nodes in the graph
277
- let rootNode = vis . graph . findNode ( tt6 ) ;
278
- let leafNode = vis . graph . findNode ( tt2 ) ;
309
+ let rootNode = vis . graph . findNode ( tt2 ) ;
310
+ let leafNode = vis . graph . findNode ( tt6 ) ;
279
311
// store the previous height of the root node
280
- vis . graph . storePrevHeight ( leafNode . y ) ;
281
- let newY = ( rootNode . y + leafNode . y ) / 2 ;
282
- let moveX = 0 ;
283
- // avoid overlapping
284
- if ( ( rootNode . x - leafNode . x ) < nodeSize ) {
285
- moveX = nodeSize - ( rootNode . x - leafNode . x ) ;
286
- }
287
- // set the new position of the nodes
288
- vis . graph . setNodePosition ( tt6 , rootNode . x + moveX , newY ) ;
289
- vis . graph . setNodePosition ( tt2 , leafNode . x - moveX , newY ) ;
312
+ let pos0 = { rX :rootNode . x , rY :rootNode . y , cX :leafNode . x , cY :leafNode . y } ;
313
+ vis . graph . setRotPos ( pos0 ) ;
314
+ let pos = rotateStep ( pos0 , 1 ) ; // compute new position
315
+ vis . graph . setNodePosition ( tt2 , pos . rX , pos . rY ) ;
316
+ vis . graph . setNodePosition ( tt6 , pos . cX , pos . cY ) ;
290
317
} ,
291
318
[ A . key , R . key ] ,
292
319
depth
@@ -304,15 +331,13 @@ export default {
304
331
}
305
332
306
333
// -- following code is for visualising the rotation step by step --
307
- // find the position of the nodes in the graph
308
- let rootNode = vis . graph . findNode ( tt6 ) ;
309
- let leafNode = vis . graph . findNode ( tt2 ) ;
310
334
// freeze the layout to avoid the nodes moving automatically
311
335
vis . graph . setPauseLayout ( true ) ;
312
- let move = ( leafNode . x - rootNode . x ) / 5 ;
313
336
// set the new position of the nodes
314
- vis . graph . setNodePosition ( tt6 , rootNode . x + move , vis . graph . getPrevHeight ( ) ) ;
315
- vis . graph . setNodePosition ( tt2 , leafNode . x - move , leafNode . y - ( rootNode . y - leafNode . y ) * 0.5 ) ;
337
+ let pos0 = vis . graph . getRotPos ( ) ; // original position
338
+ let pos = rotateStep ( pos0 , 2 ) ; // compute new position
339
+ vis . graph . setNodePosition ( tt2 , pos . rX , pos . rY ) ;
340
+ vis . graph . setNodePosition ( tt6 , pos . cX , pos . cY ) ;
316
341
} ,
317
342
[ R . key , A . key , D ? D . key : false ] ,
318
343
depth
0 commit comments