public static RouteFindingKnowledgeBase Parse(string data) { var kb = new RouteFindingKnowledgeBase { Actions = new List<AbstractAction>(), States = new List<AbstractState>() }; var split = data.Split('\n'); split = split.Select(s => s.Trim()).ToArray(); foreach (var entry in split) { // skip blank lines if (String.IsNullOrEmpty(entry)) continue; var entrySplit = entry.Split(' '); entrySplit = entrySplit.Select(s => s.Trim()).ToArray(); var sx = int.Parse(entrySplit[0]); var sy = int.Parse(entrySplit[1]); var ex = int.Parse(entrySplit[3]); var ey = int.Parse(entrySplit[4]); var actionName = entrySplit[2]; var startState = kb.GetOrCreateState(sx, sy); var endState = kb.GetOrCreateState(ex, ey); var action = new RouteFindingAction(actionName, startState, endState); kb.Actions.Add(action); } return kb; }
private void PaintRoute(String name, String filepath, int x1, int y1, int x2, int y2) { // Setup window this.Size = new Size(500, 500); this.Text = name; this.Paint += this.OnPaint; this.RouteFindingKb = RouteFindingKnowledgeBase.Parse(File.ReadAllText(filepath)); this.RouteSearchResult = AStar.Search(new RouteFindingNode(new RouteFindingState(x1, y1), new RouteFindingState(x2, y2)), this.RouteFindingKb); }
public static void DrawKnowledgeBase(Graphics gfx, RouteFindingNode traceNode, RouteFindingKnowledgeBase kb, RouteFindingState startState, RouteFindingState endState) { var offset = 10; var scale = Math.Max(kb.MaxX, kb.MaxY); scale = 400 / scale; // Draw map of routes var pen = new Pen(Color.FromArgb(100, 1, 1, 0)) { Width = 5 }; pen.EndCap = LineCap.ArrowAnchor; pen.StartCap = LineCap.RoundAnchor; foreach (var action in kb.Actions.Select(a => (RouteFindingAction)a)) gfx.DrawLine(pen, action.StartState.X * scale + offset, action.StartState.Y * scale + offset, action.EndState.X * scale + offset, action.EndState.Y * scale + offset); // Draw calculated route pen = new Pen(Color.FromArgb(255, 0, 255, 0)) { Width = 2.4f }; var last = endState; while (traceNode != null) { gfx.DrawLine(pen, last.X * scale + offset, last.Y * scale + offset, traceNode.State.X * scale + offset, traceNode.State.Y * scale + offset); last = traceNode.State; traceNode = traceNode.Parent; } // Draw end marker pen = new Pen(Color.FromArgb(255, 255, 0, 0)) { Width = 3 }; var esize = 12; gfx.DrawEllipse(pen, (int)(endState.X * scale - esize * .5) + offset, (int)(endState.Y * scale - esize * .5) + offset, esize, esize); // Draw start marker pen = new Pen(Color.FromArgb(255, 0, 0, 255)) { Width = 3 }; esize = 12; gfx.DrawEllipse(pen, (int)(startState.X * scale - esize * .5) + offset, (int)(startState.Y * scale - esize * .5) + offset, esize, esize); }