@@ -52,32 +52,44 @@ export default function initializeNodePosition(data: NodeData) {
5252 const depth = node . depth ;
5353 const radius = NODE_RADIUS ( depth ) ;
5454
55- if ( depth === 2 ) {
56- const angle = ( 360 / length ) * index ;
57- const radianAngle = ( Math . PI / 180 ) * angle ;
58- xPos += radius * Math . cos ( radianAngle ) * 8 ;
59- yPos += radius * Math . sin ( radianAngle ) * 8 ;
60- } else if ( depth === 3 ) {
61- const divideAngle = 180 / ( length + 1 ) ;
62- const adjustedAngle = divideAngle * ( index + 1 ) - 90 ;
63- const { x, y } = calculateVector ( rootLocation , { x : xPos , y : yPos } , adjustedAngle , 360 ) ;
64- xPos += x ;
65- yPos += y ;
66- } else if ( depth === 4 ) {
67- const divideAngle = 180 / ( length + 3 ) ;
68- const adjustedAngle = divideAngle * ( index + 2 ) - 90 ;
69- const { x, y } = calculateVector ( rootLocation , { x : xPos , y : yPos } , adjustedAngle , 360 ) ;
70- xPos += x ;
71- yPos += y ;
72- } else if ( depth === 5 ) {
73- const ancestor = findAncestor ( data , nodeId , 2 ) ;
74- const divideAngle = 180 / ( length + 1 ) ;
75- const adjustedAngle = divideAngle * ( index + 1 ) - 90 ;
76- const calculatedDistance = Math . sqrt ( ( 2 * radius * radius ) / ( 1 - Math . cos ( ( Math . PI / 180 ) * divideAngle ) ) ) * 1.8 ;
77- const maxDistance = Math . max ( calculatedDistance , 200 ) ;
78- const { x, y } = calculateVector ( ancestor . location , { x : xPos , y : yPos } , adjustedAngle , maxDistance ) ;
55+ const applyVectorAdjustment = ( origin : { x : number ; y : number } , angle : number , distance : number ) => {
56+ const { x, y } = calculateVector ( origin , { x : xPos , y : yPos } , angle , distance ) ;
7957 xPos += x ;
8058 yPos += y ;
59+ } ;
60+
61+ switch ( depth ) {
62+ case 2 : {
63+ const angle = ( 360 / length ) * index ;
64+ const radianAngle = ( Math . PI / 180 ) * angle ;
65+ xPos += radius * Math . cos ( radianAngle ) * 8 ;
66+ yPos += radius * Math . sin ( radianAngle ) * 8 ;
67+ break ;
68+ }
69+ case 3 : {
70+ const divideAngle = 180 / ( length + 1 ) ;
71+ const adjustedAngle = divideAngle * ( index + 1 ) - 90 ;
72+ applyVectorAdjustment ( rootLocation , adjustedAngle , 360 ) ;
73+ break ;
74+ }
75+ case 4 : {
76+ const divideAngle = 180 / ( length + 3 ) ;
77+ const adjustedAngle = divideAngle * ( index + 2 ) - 90 ;
78+ applyVectorAdjustment ( rootLocation , adjustedAngle , 360 ) ;
79+ break ;
80+ }
81+ case 5 : {
82+ const ancestor = findAncestor ( data , nodeId , 2 ) ;
83+ const divideAngle = 180 / ( length + 1 ) ;
84+ const adjustedAngle = divideAngle * ( index + 1 ) - 90 ;
85+ const calculatedDistance =
86+ Math . sqrt ( ( 2 * radius * radius ) / ( 1 - Math . cos ( ( Math . PI / 180 ) * divideAngle ) ) ) * 1.8 ;
87+ const maxDistance = Math . max ( calculatedDistance , 200 ) ;
88+ applyVectorAdjustment ( ancestor . location , adjustedAngle , maxDistance ) ;
89+ break ;
90+ }
91+ default :
92+ break ;
8193 }
8294 node . location . x = xPos ;
8395 node . location . y = yPos ;
0 commit comments