Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/MatrixTSPDataView.cs @ 17251

Last change on this file since 17251 was 17251, checked in by abeham, 5 years ago

#2521: finished refactoring TSP

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Drawing;
23using System.Windows.Forms;
24using HeuristicLab.Core.Views;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.TravelingSalesman.Views {
30  [View("Matrix TSP Data View")]
31  [Content(typeof(MatrixTSPData), IsDefaultView = true)]
32  public partial class MatrixTSPDataView : NamedItemView {
33
34    public new MatrixTSPData Content {
35      get { return (MatrixTSPData)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public MatrixTSPDataView() {
40      InitializeComponent();
41    }
42
43    protected override void OnContentChanged() {
44      base.OnContentChanged();
45      if (Content == null) {
46        distanceMatrixView.Content = null;
47        coordinatesMatrixView.Content = null;
48        coordinatesPictureBox.Image = null;
49      } else {
50        distanceMatrixView.Content = Content.Matrix;
51        coordinatesMatrixView.Content = Content.DisplayCoordinates;
52        GenerateImage();
53      }
54    }
55
56    protected override void SetEnabledStateOfControls() {
57      base.SetEnabledStateOfControls();
58      distanceMatrixView.Enabled = !ReadOnly && !Locked && Content != null;
59      coordinatesMatrixView.Enabled = !ReadOnly && !Locked && Content != null;
60    }
61
62    private void GenerateImage() {
63      if (coordinatesPictureBox.Width > 0 && coordinatesPictureBox.Height > 0) {
64        DoubleMatrix coordinates = Content?.DisplayCoordinates;
65        var bitmap = new Bitmap(coordinatesPictureBox.Width, coordinatesPictureBox.Height);
66
67        if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
68          double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
69          for (int i = 0; i < coordinates.Rows; i++) {
70            if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
71            if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
72            if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
73            if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
74          }
75
76          int border = 20;
77          double xStep = xMax != xMin ? (coordinatesPictureBox.Width - 2 * border) / (xMax - xMin) : 1;
78          double yStep = yMax != yMin ? (coordinatesPictureBox.Height - 2 * border) / (yMax - yMin) : 1;
79
80          Point[] points = new Point[coordinates.Rows];
81          for (int i = 0; i < coordinates.Rows; i++)
82            points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
83                                  bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
84
85          using (Graphics graphics = Graphics.FromImage(bitmap)) {
86            for (int i = 0; i < points.Length; i++)
87              graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
88          }
89        } else {
90          using (Graphics graphics = Graphics.FromImage(bitmap)) {
91            graphics.Clear(Color.White);
92            Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
93            string text = "No coordinates defined or in wrong format.";
94            SizeF strSize = graphics.MeasureString(text, font);
95            graphics.DrawString(text, font, Brushes.Black, (float)(coordinatesPictureBox.Width - strSize.Width) / 2.0f, (float)(coordinatesPictureBox.Height - strSize.Height) / 2.0f);
96          }
97        }
98        coordinatesPictureBox.Image = bitmap;
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.