You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(My apologies in advance for the long message)
Hey,
In order to formulate my question, I need to give a bit of context of what I am trying to do.
Let's say I have 2 nodes linked with an edge. I am trying to find the best possible edge position that links these 2 nodes based on some criteria that is already implemented in my app:
Criteria N1: The edge in source node can only be left or right with a fixed position on the y-axis (As you can see my example, I am linking the foreign key column of a db table to a destination table) so the source edge must always be next to the column that is foreign key Criteria N2: The edge in destination node can be either on top, left, right or bottom. (It is not important where it is exactly on the top (x-axis) or where it is exactly on the sides (y-axis))
My problem is when I move nodes around, I want the edge to break based on the 'best position' (this is the tricky part).
I think (and this is really open for debates, if you have better thoughts) that the best strategy is to calculate the distance between 2 nodes, and then calculate the length of the edge in all possible combinations (left with top, left with bottom... right with top...Etc) and then choose the edge with the length closest to the distance between the 2 nodes (or the shortest one) .
Here's a DEMO:
Here are some examples of bad rendering using my current logic:
Here is my current logic
/** * Checks the source and target node positions to decide the targets handle to use * @returns Target Handle position */functioncalculateTargetHandle(edge: DataEdge): Position{if(edge){constsrcPos=edge.sourceNode?.position??(getElementById(edge.source)asDataNode)?.position??{x: 0,y: 0};constsrcDim=edge.sourceNode?.dimensions??(getElementById(edge.source)asDataNode)?.dimensions??{height: 0,width: 0};consttrgtPos=edge.targetNode?.position??(getElementById(edge.target)asDataNode)?.position??{x: 0,y: 0};consttrgtDim=edge.targetNode?.dimensions??(getElementById(edge.target)asDataNode)?.dimensions??{height: 0,width: 0};consthandleHorizontalOffset=100;consthandleVerticalOffset=-(1/(edge.sourceNode?.data?.columns?.length??5));lettrgtRightSidePos=trgtPos.x+trgtDim.width+handleHorizontalOffset;letsrcRightSidePos=srcPos.x+srcDim.width+handleHorizontalOffset;consttrgtBottomPos=trgtPos.y+trgtDim.height+trgtDim.height*handleVerticalOffset;constsrcBottomPos=srcPos.y+srcDim.height+srcDim.height*handleVerticalOffset;lettrgtLeftSidePos=trgtPos.x-handleHorizontalOffset;letsrcLeftSidePos=srcPos.x-handleHorizontalOffset;consttrgtTopPos=trgtPos.y-trgtPos.y*handleVerticalOffset;constsrcTopPos=srcPos.y-srcPos.y*handleVerticalOffset;// < is left/on top of ; > is right / under of ;if(srcRightSidePos<trgtLeftSidePos){return"left";}elseif(srcLeftSidePos>trgtRightSidePos){return"right";}elseif(srcBottomPos<trgtTopPos){return"top";}elseif(srcTopPos>trgtBottomPos){return"bottom";}// When no condition is met, check without the offsetssrcRightSidePos-=handleHorizontalOffset;srcLeftSidePos+=handleHorizontalOffset;trgtRightSidePos-=handleHorizontalOffset;trgtLeftSidePos+=handleHorizontalOffset;if(srcRightSidePos<trgtLeftSidePos){return"left";}elseif(srcLeftSidePos>trgtRightSidePos){return"right";}}return"left";}
My question is: is there a function that already handles this? If not, is there a function that gets the distance between 2 nodes and/or a function that gives the length of an edge ?) If you have a better solution, an idea or even a thought, I would be very happy to try it out 🙏
I am well aware that this is a very long and custom question. I appreciate your time and your project a lot. Even an idea can go a long way.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
(My apologies in advance for the long message)
Hey,
In order to formulate my question, I need to give a bit of context of what I am trying to do.
Let's say I have 2 nodes linked with an edge. I am trying to find the best possible edge position that links these 2 nodes based on some criteria that is already implemented in my app:
Criteria N1: The edge in source node can only be left or right with a fixed position on the y-axis (As you can see my example, I am linking the foreign key column of a db table to a destination table) so the source edge must always be next to the column that is foreign key
Criteria N2: The edge in destination node can be either on top, left, right or bottom. (It is not important where it is exactly on the top (x-axis) or where it is exactly on the sides (y-axis))
My problem is when I move nodes around, I want the edge to break based on the 'best position' (this is the tricky part).
I think (and this is really open for debates, if you have better thoughts) that the best strategy is to calculate the distance between 2 nodes, and then calculate the length of the edge in all possible combinations (left with top, left with bottom... right with top...Etc) and then choose the edge with the length closest to the distance between the 2 nodes (or the shortest one) .
Here's a DEMO:
Here are some examples of bad rendering using my current logic:
Here is my current logic
My question is: is there a function that already handles this? If not, is there a function that gets the distance between 2 nodes and/or a function that gives the length of an edge ?)
If you have a better solution, an idea or even a thought, I would be very happy to try it out 🙏
I am well aware that this is a very long and custom question. I appreciate your time and your project a lot. Even an idea can go a long way.
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions