private void examToolStripMenuItem_Click(object sender, EventArgs e) { if (!this.examToolStripMenuItem.Enabled) return; __enable_all_menus(false); this.__kill_threads(); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart((thread) => { if (this.optimalPath == null || this.optimalPath.Count == 0) { this.examToolStripMenuItem.GetCurrentParent().Invoke(new Action(() => { __enable_all_menus(true); this.examToolStripMenuItem.Enabled = false; })); return; } var gh = new GridHelper(this.g); var rand = new Random(); while (this.g.AgentPoint != this.g.GoalPoint) { var l = (List<KeyValuePair<float, GridHelper.Directions>>)this.optimalPath[this.g.AgentPoint]; var s = l[rand.Next(0, l.Count - 1)]; var m = gh.Move(this.g.AgentPoint, s.Value); MarkStartGoalPoints(m.NewPoint, g.GoalPoint); this.DrawDirection(m.OldPoint, s.Value); Application.DoEvents(); System.Threading.Thread.Sleep(500); } MessageBox.Show("Has reached the goal...", "Horray!!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); this.examToolStripMenuItem.GetCurrentParent().Invoke(new Action(() => { __enable_all_menus(true); })); lock (this.ThreadsPool) this.ThreadsPool.Remove(thread as System.Threading.Thread); })); t.Start(t); lock (this.ThreadsPool) this.ThreadsPool.Add(t); }
private void DrawDirection(Point point, GridHelper.Directions direction) { using (var gfx = this.grid.CreateGraphics()) { var p = g.abs2grid(point); var pen = new Pen(Color.Black, 4); var margin = 10; switch (direction) { case GridHelper.Directions.NORTH: gfx.DrawLine(pen, new Point(p.X, p.Y - margin), new Point(p.X, p.Y + margin)); gfx.FillPolygon(new SolidBrush(pen.Color), new Point[] { new Point(p.X - margin + 5, p.Y - margin + 5), new Point(p.X + margin - 5, p.Y - margin + 5), new Point(p.X, p.Y - 2 * margin) }); break; case GridHelper.Directions.EAST: gfx.DrawLine(pen, new Point(p.X - margin - 5, p.Y), new Point(p.X + margin - 5, p.Y)); gfx.FillPolygon(new SolidBrush(pen.Color), new Point[] { new Point(p.X + margin - 5, p.Y - margin + 5), new Point(p.X + margin - 5, p.Y + margin - 5), new Point(p.X + 2 * (margin), p.Y) }); break; case GridHelper.Directions.SOUTH: gfx.DrawLine(pen, new Point(p.X, p.Y - margin - 5), new Point(p.X, p.Y + margin)); gfx.FillPolygon(new SolidBrush(Color.Black), new Point[] { new Point(p.X - margin + 5, p.Y + margin - 5), new Point(p.X + margin - 5, p.Y + margin - 5), new Point(p.X, p.Y + 2 * margin) }); break; case GridHelper.Directions.WEST: gfx.DrawLine(pen, new Point(p.X - margin + 5, p.Y), new Point(p.X + margin + 5, p.Y)); gfx.FillPolygon(new SolidBrush(pen.Color), new Point[] { new Point(p.X - margin + 5, p.Y - margin + 5), new Point(p.X - margin + 5, p.Y + margin - 5), new Point(p.X - 2 * margin, p.Y) }); break; case GridHelper.Directions.HOLD: gfx.FillEllipse(new SolidBrush(Color.Black), p.X - 5, p.Y - 5, 10, 10); break; } } }