diff options
Diffstat (limited to 'src/CS340.Plotter/TourPlot.cs')
-rw-r--r-- | src/CS340.Plotter/TourPlot.cs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/CS340.Plotter/TourPlot.cs b/src/CS340.Plotter/TourPlot.cs new file mode 100644 index 0000000..11f23ba --- /dev/null +++ b/src/CS340.Plotter/TourPlot.cs @@ -0,0 +1,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); + } + + } +} |