feat: extract graph-interface into seperate package

This commit is contained in:
2024-04-10 15:40:01 +02:00
parent 404fcbfe39
commit 2ed1501747
93 changed files with 2193 additions and 1788 deletions

View File

@ -0,0 +1,42 @@
import type { Graph } from "@nodes/types";
export function grid(width: number, height: number) {
const graph: Graph = {
id: Math.floor(Math.random() * 100000),
edges: [],
nodes: [],
};
const amount = width * height;
for (let i = 0; i < amount; i++) {
const x = i % width;
const y = Math.floor(i / height);
graph.nodes.push({
id: i,
tmp: {
visible: false,
},
position: [x * 30, y * 40],
props: i == 0 ? { value: 0 } : { op_type: 2, a: 2, b: 2 },
type: i == 0 ? "max/plantarium/float" : "max/plantarium/math",
});
graph.edges.push([i, 0, i + 1, i === amount - 1 ? "input" : "a",]);
}
graph.nodes.push({
id: amount,
tmp: {
visible: false,
},
position: [width * 30, (height - 1) * 40],
type: "max/plantarium/output",
props: {},
});
return graph;
}

View File

@ -0,0 +1,2 @@
export { grid } from "./grid";
export { tree } from "./tree";

View File

@ -0,0 +1,56 @@
import type { Graph, Node } from "@nodes/types";
export function tree(depth: number): Graph {
const nodes: Node[] = [
{
id: 0,
type: "max/plantarium/output",
position: [0, 0]
},
{
id: 1,
type: "max/plantarium/math",
position: [-40, -10]
}
]
const edges: [number, number, number, string][] = [
[1, 0, 0, "input"]
];
for (let d = 0; d < depth; d++) {
const amount = Math.pow(2, d);
for (let i = 0; i < amount; i++) {
const id0 = amount * 2 + i * 2;
const id1 = amount * 2 + i * 2 + 1;
const parent = Math.floor(id0 / 2);
const x = -(d + 1) * 50 - 40;
const y = i * 80 - amount * 35;
nodes.push({
id: id0,
type: "max/plantarium/math",
position: [x, y],
});
edges.push([id0, 0, parent, "a"]);
nodes.push({
id: id1,
type: "max/plantarium/math",
position: [x, y + 35],
});
edges.push([id1, 0, parent, "b"]);
}
}
return {
id: Math.floor(Math.random() * 100000),
nodes,
edges
};
}