From ccb8b0d323376e28272ed9c9a65adf03bb082f20 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 8 Sep 2023 11:27:04 -0300 Subject: [PATCH] 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. --- src/frontend/src/utils/reactflowUtils.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index 8105e759c..897baa602 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -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 }; +}