WIP
This commit is contained in:
@@ -1,56 +1,7 @@
|
||||
import type { Vector } from "./Vector.ts";
|
||||
|
||||
export type Node<T> = T;
|
||||
export type Edge<T> = [Node<T>, Node<T>];
|
||||
export type Graph<T> = Map<Node<T>, Array<Node<T>>>;
|
||||
|
||||
export type PositionPartial = {
|
||||
position: Vector;
|
||||
};
|
||||
|
||||
export function drawNode(
|
||||
node: Node<PositionPartial>,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
color: string = "#fff",
|
||||
): void {
|
||||
ctx.lineWidth = 2;
|
||||
ctx.strokeStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.arc(node.position.x, node.position.y, 8, 0, 2 * Math.PI);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
export function getNodeByPosition(
|
||||
nodes: Array<Node<PositionPartial>>,
|
||||
position: Vector,
|
||||
): Node<PositionPartial> | null {
|
||||
for (const node of nodes) {
|
||||
if (
|
||||
node.position.x <= position.x + 16 &&
|
||||
node.position.x >= position.x - 16 &&
|
||||
node.position.y <= position.y + 16 &&
|
||||
node.position.y >= position.y - 16
|
||||
) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function drawEdge(
|
||||
edge: Edge<PositionPartial>,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
color: string = "#fff",
|
||||
): void {
|
||||
ctx.lineWidth = 2;
|
||||
ctx.strokeStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(edge[0].position.x, edge[0].position.y);
|
||||
ctx.lineTo(edge[1].position.x, edge[1].position.y);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
export function createGraph<T>(
|
||||
nodes: Array<Node<T>>,
|
||||
edges: Array<Edge<T>>,
|
||||
|
||||
Reference in New Issue
Block a user