public static void SaveGraph(WOGraph graph, string filePath) { using (System.IO.StreamWriter output = new System.IO.StreamWriter(filePath)) { output.WriteLine(graph.VerticesNumber.ToString()); for (int i = 0; i < graph.VerticesNumber; i++) { WOGraphVertice vertice = graph.GetVerticeAt(i); output.WriteLine("{0} {1} {2}", vertice.Label, vertice.Coordinates.X, vertice.Coordinates.Y); } for (int i = 0; i < graph.VerticesNumber; i++) { string buffer = ""; for (int j = 0; j < graph.VerticesNumber; j++) { buffer += graph.GetEdgeWeightAt(i, j).ToString(); if (j != graph.VerticesNumber - 1) { buffer += " "; } } output.WriteLine(buffer); } output.Close(); } }
static public void DrawWOGraph(WOGraph graph, PictureBox canvas) { Bitmap bitmap = new Bitmap(canvas.Image); Graphics graphics = Graphics.FromImage(bitmap); for (int i = 0; i < graph.VerticesNumber; i++) { for (int j = 0; j < graph.VerticesNumber; j++) { graphics.DrawLine(Pens.Black, graph.GetVerticeAt(i).Coordinates, graph.GetVerticeAt(j).Coordinates); } } for (int i = 0; i < graph.VerticesNumber; i++) { graphics.FillEllipse(Brushes.Red, graph.GetVerticeAt(i).Coordinates.X - 5, graph.GetVerticeAt(i).Coordinates.Y - 5, 10, 10); } canvas.Image = bitmap; }
private void pictureBoxCanvas_MouseClick(object sender, MouseEventArgs e) { for (int i = 0; graph != null && i < graph.VerticesNumber; i++) { WOGraphVertice vertice = graph.GetVerticeAt(i); if ((e.X < vertice.Coordinates.X + 5 && e.X > vertice.Coordinates.X - 5) && (e.Y < vertice.Coordinates.Y + 5 && e.Y > vertice.Coordinates.Y - 5)) { dragVertice = true; verticeToDrag = i; break; } } if (dragVertice != true) { Painter.DrawPoint(new Point(e.X, e.Y), pictureBoxCanvas); if (graph == null) { graph = new WOGraph(0); } graph.AddVertice(e.X, e.Y); textBoxVerticesNumber.Text = graph.VerticesNumber.ToString(); } else { if (e.Button == MouseButtons.Right) { dragVertice = false; } } }
static public void DrawTour(Tour tour, WOGraph graph, PictureBox canvas, bool drawLabels = false) { Bitmap bitmap = new Bitmap(canvas.Image); Graphics graphics = Graphics.FromImage(bitmap); float arrowCapSize = 4; AdjustableArrowCap arrowCap = new AdjustableArrowCap(arrowCapSize, arrowCapSize * 2); Pen pen = new Pen(Brushes.Red); pen.CustomEndCap = arrowCap; pen.Width = 2; for (int i = 0; i < tour.VerticesNumber; i++) { graphics.FillEllipse(Brushes.Blue, graph.GetVerticeAt(i).Coordinates.X - 5, graph.GetVerticeAt(i).Coordinates.Y - 5, 10, 10); } for (int i = 0; i < tour.VerticesNumber; i++) { graphics.DrawLine(pen, graph.GetVerticeAt(i).Coordinates, graph.GetVerticeAt(tour.IndexAt(i)).Coordinates); } if (drawLabels == true) { Font font = new Font("Arial", 9); for (int i = 0; i < tour.VerticesNumber; i++) { graphics.DrawString(graph.GetVerticeAt(i).Label, font, Brushes.Black, graph.GetVerticeAt(i).Coordinates.X + 10, graph.GetVerticeAt(i).Coordinates.Y - 10); } } canvas.Image = bitmap; }