Lusien_Lashans

ээээ

Jul 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Windows.Forms;
  6.  
  7. namespace WindowsFormsApp1
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.         }
  15.  
  16.         private Point GetButtonCenter(Button button)
  17.         {
  18.             return new Point(
  19.                     button.Location.X + button.Size.Width / 2,
  20.                     button.Location.Y + button.Size.Height / 2
  21.                 );
  22.         }
  23.  
  24.         private void DrawLinePoints(object sender, PaintEventArgs e)
  25.         {
  26.             if (currentPath == null || currentPath.Count < 2)
  27.                 return;
  28.  
  29.             Pen blackPen = new Pen(Color.DarkRed, 3);
  30.  
  31.             for (int i = 1; i < currentPath.Count; i++)
  32.             {
  33.                 var point1 = GetButtonCenter(buttonsMap[currentPath[i - 1]]);
  34.                 var point2 = GetButtonCenter(buttonsMap[currentPath[i]]);
  35.  
  36.                 e.Graphics.DrawLine(blackPen, point1, point2);
  37.             }
  38.         }
  39.  
  40.         private List<int> GetPath(int start, int end)
  41.         {
  42.             var arrayLenght = buttonsMap.Length;
  43.             var weights = new int[arrayLenght, arrayLenght];
  44.             var input = File.ReadAllText("nodes.txt").Split(' ', '\n');
  45.  
  46.             var currentStr = 0;
  47.             Graph graph = new Graph(arrayLenght);
  48.  
  49.             for (var i = 0; i < arrayLenght; i++)
  50.             {
  51.                 for (var j = 0; j < arrayLenght; j++, currentStr++)
  52.                 {
  53.                     weights[i, j] = int.Parse(input[currentStr]);
  54.  
  55.                     if (weights[i, j] != 0)
  56.                         graph.AddEdge(i, j, weights[i, j]);
  57.                 }
  58.             }
  59.  
  60.             return Program.Dijkstra(graph, start, end);
  61.         }
  62.  
  63.         private List<int> currentPath = new List<int>();
  64.         private List<int> buttonsQueue = new List<int>();
  65.  
  66.         private void ButtonMap_Click(object sender, EventArgs e)
  67.         {
  68.             var button = sender as Button;
  69.             var buttonIndex = (int)button.Tag;
  70.  
  71.             buttonsQueue.Add(buttonIndex);
  72.             button.BackColor = Color.Gray;
  73.  
  74.             if (buttonsQueue.Count == 2)
  75.             {
  76.                 currentPath = GetPath(buttonsQueue[0], buttonsQueue[1]);
  77.  
  78.                 foreach (var index in currentPath)
  79.                     buttonsMap[index].BackColor = Color.Gray;
  80.  
  81.                 Refresh();
  82.             }
  83.  
  84.             if (buttonsQueue.Count == 3)
  85.             {
  86.                 foreach (var index in currentPath)
  87.                     buttonsMap[index].BackColor = Color.FromKnownColor(KnownColor.ControlLight);
  88.  
  89.                 buttonsQueue = new List<int>() { buttonIndex };
  90.                 currentPath = null;
  91.  
  92.                 Refresh();
  93.             }
  94.         }
  95.     }
  96. }
Add Comment
Please, Sign In to add comment