summaryrefslogtreecommitdiffstats
path: root/src/CS340.Plotter/TourPlot.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/CS340.Plotter/TourPlot.cs')
-rw-r--r--src/CS340.Plotter/TourPlot.cs44
1 files changed, 28 insertions, 16 deletions
diff --git a/src/CS340.Plotter/TourPlot.cs b/src/CS340.Plotter/TourPlot.cs
index 30d0f2a..09cd514 100644
--- a/src/CS340.Plotter/TourPlot.cs
+++ b/src/CS340.Plotter/TourPlot.cs
@@ -1,5 +1,4 @@
using System;
-using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
@@ -9,25 +8,48 @@ namespace Plotter
{
public class TourPlot
{
+ public string GraphName { get; set; }
public Tour Tour { get; set; }
public PictureBox Canvas { get; set; }
public Label WeightLabel { get; set; }
public Label RuntimeLabel { get; set; }
public TimeSpan RunTime { get; set; }
- public TourPlot(PictureBox canvas, Label weightLabel, Label runtimeLabel)
+ public TourPlot(string graphName, PictureBox canvas, Label weightLabel, Label runtimeLabel)
{
+ GraphName = graphName;
Canvas = canvas;
WeightLabel = weightLabel;
RuntimeLabel = runtimeLabel;
}
+ public void Save(string filename) =>
+ Save(filename, Canvas.Width, Canvas.Height);
+
+ public void Save(string filename, int width, int height) =>
+ Draw(300, 300).Save(filename);
+
public void Render()
{
+ Draw(Canvas.Width, Canvas.Height);
+
+ // copy the bitmap to the picturebox (double buffered)
+ Canvas.Image?.Dispose();
+ Canvas.Image = Draw(Canvas.Width, Canvas.Height);
+
+ // set runtime and weight labels
+ RuntimeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
+ RunTime.Hours, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds / 10);
+
+ WeightLabel.Text = Tour.Weight.ToString("F3");
+ }
+
+ public Bitmap Draw(int width, int height)
+ {
const int Scaler = 25;
const int PlotSize = 100;
- using Bitmap bmp = new(Canvas.Width, Canvas.Height);
+ using Bitmap bmp = new(width, height);
using Graphics gfx = Graphics.FromImage(bmp);
using Pen pen = new(Color.Black);
using Pen gridPen = new(Color.LightGray);
@@ -68,27 +90,17 @@ namespace Plotter
gfx.DrawLine(pen, cityPoint, parentPoint);
}
- // copy the bitmap to the picturebox (double buffered)
- Canvas.Image?.Dispose();
- Canvas.Image = (Bitmap)bmp.Clone();
-
- // set runtime and weight labels
- RuntimeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- RunTime.Hours, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds / 10);
-
- WeightLabel.Text = Tour.Weight.ToString("F3");
+ return (Bitmap)bmp.Clone();
// helper function to unify the scaling of images
Point ScaleLocation(Coordinate coordinate)
{
- int x = Canvas.Width * (coordinate.X + Scaler / 2) / (Scaler + PlotSize);
- int y = Canvas.Height * ((coordinate.Y - PlotSize) * -1 + Scaler / 2) / (Scaler + PlotSize);
+ int x = width * (coordinate.X + Scaler / 2) / (Scaler + PlotSize);
+ int y = height * ((coordinate.Y - PlotSize) * -1 + Scaler / 2) / (Scaler + PlotSize);
return new Point(x, y);
}
}
- public void Save(string filename) =>
- Canvas.Image.Save(filename);
}
}