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