Add path finding

This commit is contained in:
2026-07-13 01:15:01 +02:00
parent 791ee7b804
commit aece4e202e
8 changed files with 223 additions and 49 deletions
+35 -4
View File
@@ -1,10 +1,12 @@
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PamhagenSysCtrl.Helpers.Pipeline {
public class Graph {
protected HashSet<INode> Nodes = [];
protected HashSet<IEdge> Edges = [];
public HashSet<INode> Nodes = [];
public HashSet<IEdge> Edges = [];
public Graph() {
@@ -20,7 +22,32 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
return p;
}
public Path? GetPath(INode start, INode end, Func<INode, bool>? valid = null) {
if (!Nodes.Contains(start) || !Nodes.Contains(end)) throw new ArgumentException("Start/end node not contained in graph");
valid ??= a => true;
List<(IEdge, INode)> hops = [];
Path? path = null;
foreach (var o in start.Outputs) {
if (!valid(o.End)) {
continue;
} else if (o.End == end) {
return new(start, [(o, o.End)]);
}
var p = GetPath(o.End, end, valid);
if (p != null && (path == null || p.Value.Hops.Count() + 1 < path.Value.Hops.Count())) {
path = new(start, [(o, o.End), ..p.Value.Hops]);
}
}
return path != null ? new(start, path.Value.Hops) : null;
}
public void Draw(Canvas canvas) {
canvas.Children.Add(new Rectangle() {
Fill = Brushes.White,
Width = 5000,
Height = 5000,
});
foreach (var e in Edges) {
e.Draw(canvas);
}
@@ -29,13 +56,17 @@ namespace PamhagenSysCtrl.Helpers.Pipeline {
}
}
public void Update(double x, double y) {
public ISet<INode> Update(double x, double y) {
HashSet<INode> hover = new();
foreach (var e in Edges) {
e.Update(x, y);
}
foreach (var n in Nodes) {
n.Update(x, y);
if (n.Update(x, y)) {
hover.Add(n);
}
}
return hover;
}
}
}