/// <summary> /// Handles the foundNewBestTour event of the tsp control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="SalesmanEventArgs"/> instance containing the event data.</param> private void salesman_foundNewBestTour(object sender, SalesmanEventArgs e) { if (InvokeRequired) { try { Invoke(new DrawEventHandler(DrawRoute), sender, e); return; } catch (Exception) { // ignored } } DrawRoute(sender, e); }
/// <summary> /// Draws the route. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="SalesmanEventArgs"/> instance containing the event data.</param> private void DrawRoute(object sender, SalesmanEventArgs e) { lastFitnessValue.Text = Math.Round(e.BestTour.Fitness, 2).ToString(CultureInfo.CurrentCulture); lastIterationValue.Text = e.Generation.ToString(CultureInfo.CurrentCulture); if (CityImage == null) { CityImage = new Bitmap(tourDiagram.Width, tourDiagram.Height); CityGraphics = Graphics.FromImage(CityImage); } int lastCity = 0; int nextCity = e.BestTour[0].Connection1; CityGraphics.FillRectangle(Brushes.White, 0, 0, CityImage.Width, CityImage.Height); foreach (City city in e.CityList) { // Draw a circle for the city. CityGraphics.DrawEllipse(Pens.Black, city.Location.X - 2, city.Location.Y - 2, 5, 5); // Draw the line connecting the city. CityGraphics.DrawLine(Pens.Black, CityList[lastCity].Location, CityList[nextCity].Location); // figure out if the next city in the list is [0] or [1] if (lastCity != e.BestTour[nextCity].Connection1) { lastCity = nextCity; nextCity = e.BestTour[nextCity].Connection1; } else { lastCity = nextCity; nextCity = e.BestTour[nextCity].Connection2; } } tourDiagram.Image = CityImage; if (e.Complete) { StartButton.Text = "Begin"; StatusLabel.Text = "Click on the mapto place cities"; StatusLabel.ForeColor = Color.Black; } }