Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP.Views/3.3/ProbabilisticTSPVisualizer.cs @ 17335

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

#2521: Refactored pTSP to use compositional pattern

File size: 3.4 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;
23using System.Drawing;
24using HeuristicLab.Data;
25using HeuristicLab.Problems.TravelingSalesman.Views;
26
27namespace HeuristicLab.Problems.PTSP.Views {
28  public class ProbabilisticTSPVisualizer : TSPVisualizer {
29    public PercentArray Probabilities { get; set; }
30
31    public override Bitmap Draw(int width, int height) {
32      var bitmap = new Bitmap(width, height);
33      if ((Coordinates != null) && (Coordinates.Rows > 0) && (Coordinates.Columns == 2) && (Probabilities.Length == Coordinates.Rows)) {
34        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
35        for (int i = 0; i < Coordinates.Rows; i++) {
36          if (xMin > Coordinates[i, 0]) xMin = Coordinates[i, 0];
37          if (yMin > Coordinates[i, 1]) yMin = Coordinates[i, 1];
38          if (xMax < Coordinates[i, 0]) xMax = Coordinates[i, 0];
39          if (yMax < Coordinates[i, 1]) yMax = Coordinates[i, 1];
40        }
41
42        int border = 20;
43        double xStep = xMax != xMin ? (width - 2 * border) / (xMax - xMin) : 1;
44        double yStep = yMax != yMin ? (height - 2 * border) / (yMax - yMin) : 1;
45
46        Point[] points = new Point[Coordinates.Rows];
47        for (int i = 0; i < Coordinates.Rows; i++)
48          points[i] = new Point(border + ((int)((Coordinates[i, 0] - xMin) * xStep)),
49                                bitmap.Height - (border + ((int)((Coordinates[i, 1] - yMin) * yStep))));
50
51        using (Graphics graphics = Graphics.FromImage(bitmap)) {
52          if (Tour != null && Tour.Length > 1) {
53            Point[] tour = new Point[Tour.Length];
54            for (int i = 0; i < Tour.Length; i++) {
55              if (Tour[i] >= 0 && Tour[i] < points.Length)
56                tour[i] = points[Tour[i]];
57            }
58            graphics.DrawPolygon(Pens.Black, tour);
59          }
60          for (int i = 0; i < points.Length; i++)
61            graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, Convert.ToInt32(Probabilities[i] * 20), Convert.ToInt32(Probabilities[i] * 20));
62        }
63      } else {
64        using (Graphics graphics = Graphics.FromImage(bitmap)) {
65          graphics.Clear(Color.White);
66          Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
67          string text = "No coordinates defined or in wrong format.";
68          SizeF strSize = graphics.MeasureString(text, font);
69          graphics.DrawString(text, font, Brushes.Black, (width - strSize.Width) / 2.0f, (height - strSize.Height) / 2.0f);
70        }
71      }
72
73      return bitmap;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.