From 941474041b913b9c04c692f39f59ee4f5dec3710 Mon Sep 17 00:00:00 2001 From: kolaflash Date: Sun, 4 Jan 2026 23:22:32 +0100 Subject: [PATCH] WIP WIP --- dev/GraphBrowser.tsx | 52 ++++++++++++++++++++++++++++++++++++++++---- dev/main.tsx | 9 +++++++- lib/Vector.ts | 7 ++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/dev/GraphBrowser.tsx b/dev/GraphBrowser.tsx index deb617b..c1e4b82 100644 --- a/dev/GraphBrowser.tsx +++ b/dev/GraphBrowser.tsx @@ -3,11 +3,12 @@ import { type Component, createSignal, onMount } from "solid-js"; import { drawEdge, drawNode, + type Edge, type Graph, type Node, type PositionPartial, } from "#/lib/Graph.ts"; -import { createVector, type Vector } from "#/lib/Vector.ts"; +import { createVector, scale, type Vector } from "#/lib/Vector.ts"; function drawGraph( graph: Graph, @@ -21,10 +22,52 @@ function drawGraph( } } +function applyRepulsion( + nodes: Array<{ position: Vector; velocity: Vector }>, +): void { + const strength = 16; + + nodes.forEach((node, index) => { + node.velocity = createVector(0, 0); + for (let i = 0; i < nodes.length; i++) { + if (index === i) { + continue; + } + const a = node.position; + const b = nodes[i].position; + + const dx = a.x - b.x; + const dy = a.y - b.y; + + const direction = createVector(dx, dy); + + const distanceSquare = dx * dx + dy * dy; + + const magnitude = strength / distanceSquare; + + scale(direction, magnitude); + + node.velocity.x += direction.x; + node.velocity.y += direction.y; + } + }); +} + +function doTheHookeThing([ + a, + b, +]: Edge): void { + const edgeLength = 32; + const stiffness = 0.8; + const displacement = 0; + const f = stiffness * displacement; +} + function placeNodes(graph: Graph): void { const visited: Array> = []; const currentPosition = createVector(); const radius = 24; + const dist = radius * 1.5; for (const [node, neighbours] of graph.entries()) { if (visited.includes(node)) { @@ -38,12 +81,13 @@ function placeNodes(graph: Graph): void { neighbours.forEach((neighbour, index) => { const fraction = (index + 1) / neighbours.length; const rad = Math.PI * 2 * fraction; - neighbour.position.x = currentPosition.x + radius * Math.cos(rad); - neighbour.position.y = currentPosition.y + radius * Math.sin(rad); + + neighbour.position.x = currentPosition.x + dist * Math.cos(rad); + neighbour.position.y = currentPosition.y + dist * Math.sin(rad); visited.push(neighbour); }); - currentPosition.y += radius * 2; + currentPosition.y += dist; } } diff --git a/dev/main.tsx b/dev/main.tsx index 65d636d..8309f22 100644 --- a/dev/main.tsx +++ b/dev/main.tsx @@ -42,13 +42,20 @@ const nodes: Array = [ { position: createVector(), label: "hai" }, { position: createVector(), label: "sea" }, { position: createVector(), label: "vot" }, + { position: createVector(), label: "zoe" }, + { position: createVector(), label: "asm" }, ]; const edges: Array> = [ [nodes[0], nodes[1]], [nodes[0], nodes[2]], - [nodes[0], nodes[3]], [nodes[4], nodes[5]], + [nodes[5], nodes[3]], + [nodes[1], nodes[2]], + [nodes[0], nodes[3]], + [nodes[2], nodes[6]], + [nodes[5], nodes[7]], + [nodes[5], nodes[6]], ]; const graph = createGraph(nodes, edges); diff --git a/lib/Vector.ts b/lib/Vector.ts index c24323a..39fe6b8 100644 --- a/lib/Vector.ts +++ b/lib/Vector.ts @@ -29,6 +29,13 @@ export function getDistance(a: Vector, b: Vector): number { return Math.sqrt(dx * dx + dy * dy); } +export function getDistanceSquared(a: Vector, b: Vector): number { + const dx = a.x - b.x; + const dy = a.y - b.y; + + return dx * dx + dy * dy; +} + export function getSquared(vector: Vector): number { return vector.x * vector.x + vector.y * vector.y; }