Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Routing.TSP/TSPTourView.cs @ 2

Last change on this file since 2 was 2, checked in by swagner, 16 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Permutation;
32using HeuristicLab.Charting;
33
34namespace HeuristicLab.Routing.TSP {
35  public partial class TSPTourView : ViewBase {
36    public TSPTour TSPTour {
37      get { return (TSPTour)base.Item; }
38      set { base.Item = value; }
39    }
40
41
42    public TSPTourView() {
43      InitializeComponent();
44      Caption = "TSP Tour View";
45    }
46    public TSPTourView(TSPTour tspTour)
47      : this() {
48      TSPTour = tspTour;
49    }
50
51
52    protected override void RemoveItemEvents() {
53      TSPTour.CoordinatesChanged -= new EventHandler(TSPTour_CoordinatesChanged);
54      TSPTour.TourChanged -= new EventHandler(TSPTour_TourChanged);
55      base.RemoveItemEvents();
56    }
57    protected override void AddItemEvents() {
58      base.AddItemEvents();
59      TSPTour.CoordinatesChanged += new EventHandler(TSPTour_CoordinatesChanged);
60      TSPTour.TourChanged += new EventHandler(TSPTour_TourChanged);
61    }
62
63    protected override void UpdateControls() {
64      base.UpdateControls();
65      Chart chart = new Chart(0, 0, 10, 10);
66      chartControl.ScaleOnResize = false;
67
68      if ((TSPTour.Coordinates != null) && (TSPTour.Tour != null)) {
69        chart.UpdateEnabled = false;
70
71        double[,] coords = TSPTour.Coordinates.Data;
72        int[] tour = TSPTour.Tour.Data;
73
74        for (int i = 0; i < tour.Length - 1; i++)
75          chart.Group.Add(new Line(chart, coords[tour[i], 0], coords[tour[i], 1],
76                                          coords[tour[i + 1], 0], coords[tour[i + 1], 1], Pens.Black));
77        chart.Group.Add(new Line(chart, coords[tour[tour.Length - 1], 0], coords[tour[tour.Length - 1], 1],
78                                        coords[tour[0], 0], coords[tour[0], 1], Pens.Black));
79
80        for (int i = 0; i < coords.GetLength(0); i++) {
81          FixedSizeCircle circle = new FixedSizeCircle(chart, coords[i, 0], coords[i, 1], 8, Pens.Black, Brushes.Blue);
82          circle.ToolTipText = "(" + coords[i, 0].ToString() + " ; " + coords[i, 1].ToString() + ")";
83          chart.Group.Add(circle);
84        }
85
86        if (coords.GetLength(0) > 0) {
87          PointD min = new PointD(coords[0, 0], coords[0, 1]);
88          PointD max = new PointD(coords[0, 0], coords[0, 1]);
89          for (int i = 0; i < coords.GetLength(0); i++) {
90            if (coords[i, 0] < min.X) min.X = coords[i, 0];
91            if (coords[i, 1] < min.Y) min.Y = coords[i, 1];
92            if (coords[i, 0] > max.X) max.X = coords[i, 0];
93            if (coords[i, 1] > max.Y) max.Y = coords[i, 1];
94          }
95          Offset offset = new Offset((max.X - min.X) / 20, (max.Y - min.Y) / 20);
96          min = min - offset;
97          max = max + offset;
98          chart.SetPosition(min, max);
99        }
100        chart.UpdateEnabled = true;
101      }
102      chartControl.Chart = chart;
103    }
104
105    #region TSPTour Events
106    private void TSPTour_TourChanged(object sender, EventArgs e) {
107      Refresh();
108    }
109    private void TSPTour_CoordinatesChanged(object sender, EventArgs e) {
110      Refresh();
111    }
112    #endregion
113  }
114}
Note: See TracBrowser for help on using the repository browser.