Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private Point GetButtonCenter(Button button)
- {
- return new Point(
- button.Location.X + button.Size.Width / 2,
- button.Location.Y + button.Size.Height / 2
- );
- }
- private void DrawLinePoints(object sender, PaintEventArgs e)
- {
- if (currentPath == null || currentPath.Count < 2)
- return;
- Pen blackPen = new Pen(Color.DarkRed, 3);
- for (int i = 1; i < currentPath.Count; i++)
- {
- var point1 = GetButtonCenter(buttonsMap[currentPath[i - 1]]);
- var point2 = GetButtonCenter(buttonsMap[currentPath[i]]);
- e.Graphics.DrawLine(blackPen, point1, point2);
- }
- }
- private List<int> GetPath(int start, int end)
- {
- var arrayLenght = buttonsMap.Length;
- var weights = new int[arrayLenght, arrayLenght];
- var input = File.ReadAllText("nodes.txt").Split(' ', '\n');
- var currentStr = 0;
- Graph graph = new Graph(arrayLenght);
- for (var i = 0; i < arrayLenght; i++)
- {
- for (var j = 0; j < arrayLenght; j++, currentStr++)
- {
- weights[i, j] = int.Parse(input[currentStr]);
- if (weights[i, j] != 0)
- graph.AddEdge(i, j, weights[i, j]);
- }
- }
- return Program.Dijkstra(graph, start, end);
- }
- private List<int> currentPath = new List<int>();
- private List<int> buttonsQueue = new List<int>();
- private void ButtonMap_Click(object sender, EventArgs e)
- {
- var button = sender as Button;
- var buttonIndex = (int)button.Tag;
- buttonsQueue.Add(buttonIndex);
- button.BackColor = Color.Gray;
- if (buttonsQueue.Count == 2)
- {
- currentPath = GetPath(buttonsQueue[0], buttonsQueue[1]);
- foreach (var index in currentPath)
- buttonsMap[index].BackColor = Color.Gray;
- Refresh();
- }
- if (buttonsQueue.Count == 3)
- {
- foreach (var index in currentPath)
- buttonsMap[index].BackColor = Color.FromKnownColor(KnownColor.ControlLight);
- buttonsQueue = new List<int>() { buttonIndex };
- currentPath = null;
- Refresh();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment