From 205b8e74be79f364d8dd1aab4bf41d5988f2717a Mon Sep 17 00:00:00 2001 From: limina1 Date: Wed, 23 Jul 2025 16:03:02 -0400 Subject: [PATCH] move force to top-level --- .../EventNetwork/utils/starForceSimulation.ts | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/src/lib/navigator/EventNetwork/utils/starForceSimulation.ts b/src/lib/navigator/EventNetwork/utils/starForceSimulation.ts index fb77f18..c22ac1d 100644 --- a/src/lib/navigator/EventNetwork/utils/starForceSimulation.ts +++ b/src/lib/navigator/EventNetwork/utils/starForceSimulation.ts @@ -111,13 +111,52 @@ export function createStarSimulation( return simulation; } +/** + * Applies the radial force to keep content nodes in orbit around their star center + * @param nodes - The array of network nodes + * @param nodeToCenter - Map of content node IDs to their star center node + * @param targetDistance - The desired distance from center to content node + * @param alpha - The current simulation alpha + */ +function applyRadialForce( + nodes: NetworkNode[], + nodeToCenter: Map, + targetDistance: number, + alpha: number +): void { + nodes.forEach(node => { + if (node.kind === 30041) { + const center = nodeToCenter.get(node.id); + if ( + center && + center.x != null && + center.y != null && + node.x != null && + node.y != null + ) { + // Calculate desired position + const dx = node.x - center.x; + const dy = node.y - center.y; + const distance = Math.sqrt(dx * dx + dy * dy); + + if (distance > 0) { + // Normalize and apply force + const force = (distance - targetDistance) * alpha * 0.3; // Reduced force + node.vx = (node.vx || 0) - (dx / distance) * force; + node.vy = (node.vy || 0) - (dy / distance) * force; + } + } + } + }); +} + /** * Creates a custom radial force that keeps content nodes in orbit around their star center */ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any { // Build a map of content nodes to their star centers const nodeToCenter = new Map(); - + links.forEach(link => { const source = link.source as NetworkNode; const target = link.target as NetworkNode; @@ -126,27 +165,8 @@ function createRadialForce(nodes: NetworkNode[], links: NetworkLink[]): any { } }); - // Custom force function function force(alpha: number) { - nodes.forEach(node => { - if (node.kind === 30041) { - const center = nodeToCenter.get(node.id); - if (center && center.x != null && center.y != null && node.x != null && node.y != null) { - // Calculate desired position - const dx = node.x - center.x; - const dy = node.y - center.y; - const distance = Math.sqrt(dx * dx + dy * dy); - - if (distance > 0) { - // Normalize and apply force - const targetDistance = STAR_LINK_DISTANCE; - const force = (distance - targetDistance) * alpha * 0.3; // Reduced force - node.vx = (node.vx || 0) - (dx / distance) * force; - node.vy = (node.vy || 0) - (dy / distance) * force; - } - } - } - }); + applyRadialForce(nodes, nodeToCenter, STAR_LINK_DISTANCE, alpha); } force.initialize = function(_: NetworkNode[]) {