Skip to content
Snippets Groups Projects
Commit ff909d7f authored by Matthias Konitzny's avatar Matthias Konitzny :fire:
Browse files

Improved typing

parent ce8279a9
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,6 @@ import { Node } from "../common/graph/node";
import { Link } from "../common/graph/link";
export interface VisualGraphNode extends Node {
color: string;
__threeObj: THREE.Group;
}
......
import { Link } from "../common/graph/link";
import { NodeType } from "../common/graph/nodetype";
import { Node, NodeData } from "../common/graph/node";
import { Node, NodeData, SimNodeData } from "../common/graph/node";
import * as Common from "../common/graph/graph";
import { History } from "../common/history";
import { GraphContent, SimGraphData } from "../common/graph/graph";
......@@ -60,12 +60,22 @@ export class DynamicGraph extends Common.Graph {
return objectGroup;
}
public createNode(data?: NodeData): Node {
public createNode(
data?: NodeData | SimNodeData,
x?: number,
y?: number,
vx?: number,
vy?: number
): Node {
if (data == undefined) {
data = {
id: 0,
name: "Undefined",
type: this.objectGroups[0].name, // TODO: Change to id
x: x,
y: y,
vx: vx,
vy: vy,
};
}
return super.createNode(data);
......@@ -120,30 +130,26 @@ export class DynamicGraph extends Common.Graph {
}
/**
* Goes over all nodes and finds the closest node based on distance, that is not the given reference node.
* @param referenceNode Reference node to get closest other node to.
* Goes over all nodes and finds the closest node based on distance.
* @returns Closest node and distance. Undefined, if no closest node can be found.
*/
public getClosestNeighbor(referenceNode: Node): {
public getClosestNode(
x: number,
y: number,
exclude?: Node
): {
node: Node;
distance: number;
} {
if (referenceNode == undefined || this.nodes.length < 2) {
return undefined;
}
// Iterate over all nodes, keep the one with the shortest distance
let closestDistance = Number.MAX_VALUE;
let closestNode: Node = undefined;
this.nodes.forEach((node) => {
if (node.equals(referenceNode)) {
if (node.equals(exclude)) {
return; // Don't compare to itself
}
const currentDistance = Math.hypot(
referenceNode.x - node.x,
referenceNode.y - node.y
);
const currentDistance = Math.hypot(x - node.x, y - node.y);
if (closestDistance > currentDistance) {
closestDistance = currentDistance;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment