feat(reactflowUtils.ts): add getMiddlePoint function to calculate the average position of multiple nodes
The getMiddlePoint function calculates the average position of multiple nodes by summing up the x and y coordinates of each node and dividing them by the total number of nodes. This allows us to find the middle point or center of a group of nodes. This function can be used to determine the position of a new node based on the average position of existing nodes.
This commit is contained in:
parent
7d9a9a7def
commit
ccb8b0d323
1 changed files with 16 additions and 0 deletions
|
|
@ -385,3 +385,19 @@ export function customStringify(obj: any): string {
|
||||||
);
|
);
|
||||||
return `{${keyValuePairs.join(",")}}`;
|
return `{${keyValuePairs.join(",")}}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getMiddlePoint(nodes: Node[]) {
|
||||||
|
let middlePointX = 0;
|
||||||
|
let middlePointY = 0;
|
||||||
|
|
||||||
|
nodes.forEach((node) => {
|
||||||
|
middlePointX += node.position.x;
|
||||||
|
middlePointY += node.position.y;
|
||||||
|
});
|
||||||
|
|
||||||
|
const totalNodes = nodes.length;
|
||||||
|
const averageX = middlePointX / totalNodes;
|
||||||
|
const averageY = middlePointY / totalNodes;
|
||||||
|
|
||||||
|
return { x: averageX, y: averageY };
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue