void DrawUpdatedPoints(GAAlgorithm alg) { pictureBox1.Image = GetImage(_maxX, _maxY, _scale, _points, alg); // print calculated distance label1.Text = string.Format("There are {0} G0 points, the {1}th generation with {2} times of mutation. Best value: {3}", _points.Count, alg.CurrentGeneration, alg.MutationTimes, alg.BestValue); }
// method that is called from the async GAAlgorithm long running task void ReportProgress(GAAlgorithm alg) { // only update if 50 ms has passed var timeNow = DateTime.Now; if ((DateTime.Now - previousTime).Milliseconds <= 50) { return; } DrawUpdatedPoints(alg); previousTime = timeNow; }
public MainForm(frmPlotter plotter, List <IPoint> points, float maxX, float maxY) { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent(); _points = points; this._maxX = maxX; this._maxY = maxY; this._plotter = plotter; _alg = new GAAlgorithm(_points); _scale = CalculateOptimalScale(maxX, maxY); DrawInitialPoints(); }
static Bitmap GetImage(float maxX, float maxY, float scale, IList <IPoint> points, GAAlgorithm alg = null) { // constants const int radius = 4; const int MARGIN = 10; var bitmap = new Bitmap((int)((maxX + MARGIN) * scale), (int)((maxY + MARGIN) * scale)); Graphics gfx = Graphics.FromImage(bitmap); // set background gfx.Clear(Color.White); // define pens var straightPen = new Pen(Color.Red, 1.0f); straightPen.StartCap = LineCap.NoAnchor; straightPen.EndCap = LineCap.ArrowAnchor; var lastStraightPen = new Pen(Color.HotPink, 1.0f); lastStraightPen.StartCap = LineCap.NoAnchor; lastStraightPen.EndCap = LineCap.ArrowAnchor; var lastBrush = new SolidBrush(lastStraightPen.Color); var arcPen = new Pen(Color.Black, 0.5f); var arcBrush = new SolidBrush(Color.Black); IPoint firstLocation = null; IPoint previousLocation = null; IPoint currentLocation = null; int TotalCount = 0; if (alg != null) { TotalCount = alg.BestPath.Count; } else { TotalCount = points.Count; } for (int i = 0; i < TotalCount; i++) { if (alg != null) { currentLocation = (IPoint)points[alg.BestPath[i]]; } else { currentLocation = (IPoint)points[i]; } if (previousLocation != null) { // draw gfx.FillEllipse(arcBrush, currentLocation.X * scale - radius, (maxY - currentLocation.Y) * scale - radius, radius * 2, radius * 2); gfx.DrawLine(straightPen, previousLocation.X * scale, (maxY - previousLocation.Y) * scale, currentLocation.X * scale, (maxY - currentLocation.Y) * scale); } previousLocation = currentLocation; } // last line and circle if (alg != null) { firstLocation = points[alg.BestPath[0]]; } else { firstLocation = points[0]; } // draw last circle back to first location gfx.FillEllipse(lastBrush, firstLocation.X * scale - radius, (maxY - firstLocation.Y) * scale - radius, radius * 2, radius * 2); // draw last line gfx.DrawLine(lastStraightPen, previousLocation.X * scale, (maxY - previousLocation.Y) * scale, firstLocation.X * scale, (maxY - firstLocation.Y) * scale); gfx.Flush(); gfx.Dispose(); // dispose pens straightPen.Dispose(); lastStraightPen.Dispose(); lastBrush.Dispose(); arcPen.Dispose(); arcBrush.Dispose(); return(bitmap); }