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:
anovazzi1 2023-09-08 11:27:04 -03:00
commit ccb8b0d323

View file

@ -385,3 +385,19 @@ export function customStringify(obj: any): string {
);
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 };
}