diff options
author | Toby Vincent <tobyv13@gmail.com> | 2021-04-23 20:46:50 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2021-04-23 20:46:50 -0500 |
commit | 771a298977d37cd2bca618b3f4ad5b6aaba89405 (patch) | |
tree | 4b1ffa15808c4efc02b529eb50b168b35d6f6229 /src/CS340.Plotter/TourPlot.cs | |
parent | 64587fb4b32a74cfbb53216ad85e1b796035bc7f (diff) |
added dialog for plot file location
Diffstat (limited to 'src/CS340.Plotter/TourPlot.cs')
-rw-r--r-- | src/CS340.Plotter/TourPlot.cs | 44 |
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); } } |