Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/10 06:18:48 (15 years ago)
Author:
swagner
Message:

Finished first version of TSP tour visualization (#924).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TSP.Views/3.3/PathTSPTourView.cs

    r3107 r3139  
    8383          DoubleMatrix coordinates = Content.Coordinates;
    8484          Permutation permutation = Content.Permutation;
     85          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
    8586
    86           double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
    87           for (int i = 0; i < coordinates.Rows; i++) {
    88             if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
    89             if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
    90             if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
    91             if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
     87          if (coordinates != null) {
     88            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
     89            for (int i = 0; i < coordinates.Rows; i++) {
     90              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
     91              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
     92              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
     93              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
     94            }
     95
     96            int border = 20;
     97            double xStep = (pictureBox.Width - 2 * border) / (xMax - xMin);
     98            double yStep = (pictureBox.Height - 2 * border) / (yMax - yMin);
     99
     100            Point[] points = new Point[coordinates.Rows];
     101            if (permutation == null) {
     102              for (int i = 0; i < coordinates.Rows; i++)
     103                points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
     104                                      border + ((int)((coordinates[i, 1] - yMin) * yStep)));
     105            } else {
     106              for (int i = 0; i < coordinates.Rows; i++)
     107                points[i] = new Point(border + ((int)((coordinates[permutation[i], 0] - xMin) * xStep)),
     108                                      border + ((int)((coordinates[permutation[i], 1] - yMin) * yStep)));
     109            }
     110
     111            Graphics graphics = Graphics.FromImage(bitmap);
     112            if (permutation != null) graphics.DrawPolygon(Pens.Black, points);
     113            for (int i = 0; i < points.Length; i++)
     114              graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
     115            graphics.Dispose();
    92116          }
    93 
    94           int border = 20;
    95           double xStep = (pictureBox.Width - 2 * border) / (xMax - xMin);
    96           double yStep = (pictureBox.Height - 2 * border) / (yMax - yMin);
    97 
    98           Point[] points = new Point[coordinates.Rows];
    99           for (int i = 0; i < coordinates.Rows; i++)
    100             points[i] = new Point(border + ((int)((coordinates[permutation[i], 0] - xMin) * xStep)),
    101                                   border + ((int)((coordinates[permutation[i], 1] - yMin) * yStep)));
    102 
    103           Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
    104           Graphics graphics = Graphics.FromImage(bitmap);
    105           graphics.DrawPolygon(Pens.Black, points);
    106           for (int i = 0; i < points.Length; i++)
    107             graphics.FillRectangle(Brushes.Blue, points[i].X - 1, points[i].Y - 1, 4, 4);
    108           graphics.Dispose();
    109117          pictureBox.Image = bitmap;
    110118        }
Note: See TracChangeset for help on using the changeset viewer.