Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting.Views/3.4/SingleDepotVRPView.cs @ 7858

Last change on this file since 7858 was 7858, checked in by svonolfe, 12 years ago

Added color palette (#1177)

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using HeuristicLab.MainForm;
9using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
10using HeuristicLab.Data;
11
12namespace HeuristicLab.Problems.VehicleRouting.Views {
13  [View("SingleDepotVRPProblemInstance View")]
14  [Content(typeof(SingleDepotVRPProblemInstance), true)]
15  public partial class SingleDepotVRPView : VRPProblemInstanceView {
16    public new SingleDepotVRPProblemInstance Content {
17      get { return (SingleDepotVRPProblemInstance)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public SingleDepotVRPView() {
22      InitializeComponent();
23    }
24
25    protected override void DrawVisualization(Bitmap bitmap) {
26      DoubleMatrix coordinates = Content.Coordinates;
27      DoubleMatrix distanceMatrix = Content.DistanceMatrix;
28      BoolValue useDistanceMatrix = Content.UseDistanceMatrix;
29
30      if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
31        double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
32        for (int i = 0; i < coordinates.Rows; i++) {
33          if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
34          if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
35          if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
36          if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
37        }
38
39        int border = 20;
40        double xStep = xMax != xMin ? (bitmap.Width - 2 * border) / (xMax - xMin) : 1;
41        double yStep = yMax != yMin ? (bitmap.Height - 2 * border) / (yMax - yMin) : 1;
42
43        using (Graphics graphics = Graphics.FromImage(bitmap)) {
44          if (Solution != null) {
45             List<Tour> tours = Solution.GetTours();
46             List<Pen> pens = GetColors(tours.Count);
47
48            int currentTour = 0;
49            foreach (Tour tour in tours) {
50              Point[] tourPoints = new Point[tour.Stops.Count + 2];
51              Brush[] customerBrushes = new Brush[tour.Stops.Count];
52              int lastCustomer = 0;
53
54              for (int i = -1; i <= tour.Stops.Count; i++) {
55                int location = 0;
56
57                if (i == -1 || i == tour.Stops.Count)
58                  location = 0; //depot
59                else
60                  location = tour.Stops[i];
61
62                Point locationPoint = new Point(border + ((int)((coordinates[location, 0] - xMin) * xStep)),
63                                bitmap.Height - (border + ((int)((coordinates[location, 1] - yMin) * yStep))));
64                tourPoints[i + 1] = locationPoint;
65
66                if (i != -1 && i != tour.Stops.Count) {
67                  Brush customerBrush = Brushes.Black;
68                  customerBrushes[i] = customerBrush;
69                }
70                lastCustomer = location;
71              }
72
73              graphics.DrawPolygon(pens[currentTour], tourPoints);
74
75              for (int i = 0; i < tour.Stops.Count; i++) {
76                graphics.FillRectangle(customerBrushes[i], tourPoints[i + 1].X - 3, tourPoints[i + 1].Y - 3, 6, 6);
77              }
78
79              graphics.FillEllipse(Brushes.Blue, tourPoints[0].X - 5, tourPoints[0].Y - 5, 10, 10);
80
81              currentTour++;
82            }
83
84            for (int i = 0; i < pens.Count; i++)
85               pens[i].Dispose();
86          } else {
87            Point locationPoint;
88            //just draw customers
89            for (int i = 1; i < coordinates.Rows; i++) {
90              locationPoint = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
91                              bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
92
93              graphics.FillRectangle(Brushes.Black, locationPoint.X - 3, locationPoint.Y - 3, 6, 6);
94            }
95
96            locationPoint = new Point(border + ((int)((coordinates[0, 0] - xMin) * xStep)),
97                              bitmap.Height - (border + ((int)((coordinates[0, 1] - yMin) * yStep))));
98            graphics.FillEllipse(Brushes.Blue, locationPoint.X - 5, locationPoint.Y - 5, 10, 10);
99          }
100        }
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.