-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
148 lines (116 loc) · 4.83 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Salesman.Algorithms;
namespace Salesman
{
public partial class MainWindow : Window
{
private readonly SolidColorBrush MarkerFillBrush = new SolidColorBrush();
private readonly SolidColorBrush LineFillBrush = new SolidColorBrush();
private readonly double MARKER_DIAMETER = 10;
private readonly double MARKER_RADIUS;
private readonly Salesman Logic = new Salesman();
private readonly Random rnd = new Random();
private readonly MenuedAlgorithm[] Algorithms = new MenuedAlgorithm[] { new MenuedAlgorithm("Brute Force", new BruteForceThreaded()), new MenuedAlgorithm("Greedy", new GreedyAll()) };
private ComboBoxItem CurrentAlgorithm;
public MainWindow()
{
InitializeComponent();
MarkerFillBrush.Color = Colors.Black;
LineFillBrush.Color = Colors.LightBlue;
this.MARKER_RADIUS = MARKER_DIAMETER / 2;
Logic.Algorithm = Algorithms[0];
}
protected void Marker_Click(object sender, MouseEventArgs e)
{
Point clickCoords = new Point(e.GetPosition(this.Markers));
Ellipse marker = new Ellipse { Width = MARKER_DIAMETER, Height = MARKER_DIAMETER, Fill = MarkerFillBrush };
double x = clickCoords.X - MARKER_RADIUS;
double y = clickCoords.Y - MARKER_RADIUS;
Thickness margin = new Thickness(x, y, 0, 0);
marker.Margin = margin;
_ = this.Markers.Children.Add(marker);
Logic.Points.AddPoint(new Point(x, y));
}
private Point RandomCoords()
{
return new Point(rnd.Next(73500) / 100 + 15, rnd.Next(33000) / 100 + 15);
}
private void Randomise_Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < Logic.Points.Length; i++)
{
Point point = Logic.Points.GetPoint(i);
Point coords = RandomCoords();
point.X = coords.X;
point.Y = coords.Y;
Shape marker = (Shape)this.Markers.Children[i];
marker.Margin = new Thickness(point.X, point.Y, 0, 0);
Lines.Children.Clear();
}
}
private void Clear_Button_Click(object sender, RoutedEventArgs e)
{
this.Markers.Children.Clear();
this.Lines.Children.Clear();
this.Logic.Points.Clear();
}
private void Add_Point_Button_Click(object sender, RoutedEventArgs e)
{
Ellipse marker = new Ellipse { Width = MARKER_DIAMETER, Height = MARKER_DIAMETER, Fill = MarkerFillBrush };
Point coords = RandomCoords();
Thickness margin = new Thickness(coords.X, coords.Y, 0, 0);
marker.Margin = margin;
_ = this.Markers.Children.Add(marker);
Logic.Points.AddPoint(new Point(coords.X, coords.Y));
}
private void Calculate_Button_Click(object sender, RoutedEventArgs e)
{
Button calculateButton = (Button)sender;
calculateButton.IsEnabled = false;
if (Logic.Points.Length > 0)
{
Logic.Solution = null;
Logic.RunAlgorithm();
Lines.Children.Clear();
RenderLine(0);
MessageBox.Show(Math.Round(SolutionMaths.DistanceTotal(Logic.Points.GetAllPoints(), Logic.Solution, false), 1).ToString(), "Distance");
}
calculateButton.IsEnabled = true;
}
private void RenderLine(int prev)
{
if (Lines.Children.Count == Logic.Points.Length || Logic.Solution[prev] == -1) return;
Point prevPoint = Logic.Points.GetPoint(prev);
int current = Logic.Solution[prev];
Point currentPoint = Logic.Points.GetPoint(current);
Line newLine = new Line
{
Stroke = LineFillBrush,
StrokeThickness = 3,
X1 = prevPoint.X + MARKER_RADIUS,
Y1 = prevPoint.Y + MARKER_RADIUS,
X2 = currentPoint.X + MARKER_RADIUS,
Y2 = currentPoint.Y + MARKER_RADIUS
};
_ = Lines.Children.Add(newLine);
RenderLine(current);
}
private void AlgrithmMenu_Click(object sender, RoutedEventArgs e)
{
ComboBoxItem menuItem = (ComboBoxItem)sender;
foreach (MenuedAlgorithm option in this.Algorithms)
{
if (option.DisplayName.Equals(menuItem.Content))
{
Logic.Algorithm = option;
break;
}
}
}
}
}