Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Routing.TSP/3.3/TSPTourView.cs @ 4021

Last change on this file since 4021 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 5.4 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  /// <summary>
36  /// Class for the visual representation of a <see cref="TSPTour"/>.
37  /// </summary>
38  public partial class TSPTourView : ViewBase {
39    /// <summary>
40    /// Gets or sets the <see cref="TSPTour"/> to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public TSPTour TSPTour {
45      get { return (TSPTour)base.Item; }
46      set { base.Item = value; }
47    }
48
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="TSPTourView"/> with caption "TSP Tour View".
52    /// </summary>
53    public TSPTourView() {
54      InitializeComponent();
55      Caption = "TSP Tour View";
56    }
57    /// <summary>
58    /// Initializes a new instance of <see cref="TSPTourView"/> with the given <paramref name="tspTour"/>.
59    /// </summary>
60    /// <param name="tspTour">The tour to display.</param>
61    public TSPTourView(TSPTour tspTour)
62      : this() {
63      TSPTour = tspTour;
64    }
65
66
67    /// <summary>
68    /// Removes all event handlers from the underlying <see cref="TSPTour"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void RemoveItemEvents() {
72      TSPTour.CoordinatesChanged -= new EventHandler(TSPTour_CoordinatesChanged);
73      TSPTour.TourChanged -= new EventHandler(TSPTour_TourChanged);
74      base.RemoveItemEvents();
75    }
76    /// <summary>
77    /// Adds event handlers to the underlying <see cref="TSPTour"/>.
78    /// </summary>
79    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
80    protected override void AddItemEvents() {
81      base.AddItemEvents();
82      TSPTour.CoordinatesChanged += new EventHandler(TSPTour_CoordinatesChanged);
83      TSPTour.TourChanged += new EventHandler(TSPTour_TourChanged);
84    }
85
86    /// <summary>
87    /// Updates all controls with the latest data of the model.
88    /// </summary>
89    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
90    protected override void UpdateControls() {
91      base.UpdateControls();
92      Chart chart = new Chart(0, 0, 10, 10);
93      chartControl.ScaleOnResize = false;
94
95      if ((TSPTour.Coordinates != null) && (TSPTour.Tour != null)) {
96        chart.UpdateEnabled = false;
97
98        double[,] coords = TSPTour.Coordinates.Data;
99        int[] tour = TSPTour.Tour.Data;
100
101        for (int i = 0; i < tour.Length - 1; i++)
102          chart.Group.Add(new Line(chart, coords[tour[i], 0], coords[tour[i], 1],
103                                          coords[tour[i + 1], 0], coords[tour[i + 1], 1], Pens.Black));
104        chart.Group.Add(new Line(chart, coords[tour[tour.Length - 1], 0], coords[tour[tour.Length - 1], 1],
105                                        coords[tour[0], 0], coords[tour[0], 1], Pens.Black));
106
107        for (int i = 0; i < coords.GetLength(0); i++) {
108          FixedSizeCircle circle = new FixedSizeCircle(chart, coords[i, 0], coords[i, 1], 8, Pens.Black, Brushes.Blue);
109          circle.ToolTipText = "(" + coords[i, 0].ToString() + " ; " + coords[i, 1].ToString() + ")";
110          chart.Group.Add(circle);
111        }
112
113        if (coords.GetLength(0) > 0) {
114          PointD min = new PointD(coords[0, 0], coords[0, 1]);
115          PointD max = new PointD(coords[0, 0], coords[0, 1]);
116          for (int i = 0; i < coords.GetLength(0); i++) {
117            if (coords[i, 0] < min.X) min.X = coords[i, 0];
118            if (coords[i, 1] < min.Y) min.Y = coords[i, 1];
119            if (coords[i, 0] > max.X) max.X = coords[i, 0];
120            if (coords[i, 1] > max.Y) max.Y = coords[i, 1];
121          }
122          Offset offset = new Offset((max.X - min.X) / 20, (max.Y - min.Y) / 20);
123          min = min - offset;
124          max = max + offset;
125          chart.SetPosition(min, max);
126        }
127        chart.UpdateEnabled = true;
128      }
129      chartControl.Chart = chart;
130    }
131
132    #region TSPTour Events
133    private void TSPTour_TourChanged(object sender, EventArgs e) {
134      Refresh();
135    }
136    private void TSPTour_CoordinatesChanged(object sender, EventArgs e) {
137      Refresh();
138    }
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.