summaryrefslogtreecommitdiffstats
path: root/src/CS340.Plotter/TourPlot.cs
blob: 11f23ba8c3758075c8dc93ddeb3346a47a49c4a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using TSP;

namespace Plotter
{
    public class TourPlot
    {
        public Tour Tour { get; set; }
        public PictureBox Canvas { get; set; }

        public TourPlot(PictureBox canvas)
        {
            Canvas = canvas;
        }

        public void Render()
        {
            const int Scaler = 125;
            using Bitmap bmp = new(Canvas.Width, Canvas.Height);
            using Graphics gfx = Graphics.FromImage(bmp);
            using Pen pen = new(Color.Black);
            using Pen gridPen = new(Color.LightGray);
            using Font font = new("Arial", 16);
            using Font gridFont = new("Arial", 10);
            using SolidBrush brush = new(Color.Black);

            gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            gfx.Clear(Color.White);

            for (int i = 0; i <= 100; i += 10)
            {
                Point left = ScaleLocation(new Coordinate(0, i));
                Point right = ScaleLocation(new Coordinate(100, i));
                gfx.DrawLine(gridPen, left, right);
                gfx.DrawString(i.ToString(), gridFont, brush, left);

                Point bottom = ScaleLocation(new Coordinate(i, 0));
                Point top = ScaleLocation(new Coordinate(i, 100));
                gfx.DrawLine(gridPen, bottom, top);
                gfx.DrawString(i.ToString(), gridFont, brush, bottom);
            }

            foreach (City city in Tour.Cities.Where(city => city.Parent != -1))
            {
                City parent = Tour[city.Parent];

                Point cityPoint = ScaleLocation(city.Location);
                Point parentPoint = ScaleLocation(parent.Location);

                gfx.DrawString(city.Id.ToString(), font, brush, cityPoint);
                gfx.DrawLine(pen, cityPoint, parentPoint);
            }

            // copy the bitmap to the picturebox (double buffered)
            Canvas.Image?.Dispose();
            Canvas.Image = (Bitmap)bmp.Clone();

            Point ScaleLocation(Coordinate coordinate)
            {
                int x = Canvas.Width * (coordinate.X + 10) / Scaler;
                int y = Canvas.Height * ((coordinate.Y - 100) * -1 + 15) / Scaler;

                return new Point(x, y);
            }
        }

        public void WriteDebug()
        {
            Debug.WriteLine(Tour);

            foreach (City city in Tour.Cities)
                Debug.WriteLine(city.Location);
        }

    }
}