-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
156 lines (129 loc) · 5.58 KB
/
MainWindow.xaml.cs
File metadata and controls
156 lines (129 loc) · 5.58 KB
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
149
150
151
152
153
154
155
156
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MazeSolver.Builder;
using MazeSolver.Common;
using MazeSolver.Solver;
using System;
using System.ComponentModel;
using System.Windows.Threading;
namespace MazeSolver
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private int _height;
private Cell[,] _maze;
private int _width;
private IList<Cell> _solution;
public MainWindow()
{
InitializeComponent();
}
public void ExecuteBackground(Action task, Action taskDone)
{
solveButton.IsEnabled = false;
generateButton.IsEnabled = false;
progressBar.IsIndeterminate = true;
var worker = new BackgroundWorker();
worker.DoWork += (o, e) => task();
worker.RunWorkerCompleted += ((o, e) => Dispatcher.BeginInvoke(new Action(() =>
{
taskDone();
solveButton.IsEnabled = true;
generateButton.IsEnabled = true;
progressBar.IsIndeterminate = false;
}), DispatcherPriority.Render));
worker.RunWorkerAsync();
}
private void GenerateButtonClick(object sender, RoutedEventArgs e)
{
_width = _height = (int) slider.Value;
image.Width = _width*10;
image.Height = _height*10;
IMazeBuilder builder = new DFSMazeBuilder();
ExecuteBackground(() => _maze = builder.Build(_width, _height),
DrawMaze);
}
private void SolveButtonClick(object sender, RoutedEventArgs e)
{
IMazeSolver solver = new BFSMazeSolver();
Cell startingPoint = _maze[0, 0];
Cell endingPoint = _maze[_width - 1, _height - 1];
ExecuteBackground(() => _solution = solver.Solve(_maze, startingPoint, endingPoint),
() => DrawSolution(_solution));
}
private void DrawMaze()
{
mazeDrawing.Geometry = null;
solutionDrawing.Geometry = null;
double xFactor = image.Width/_width;
double yFactor = image.Height/_height;
var g = new StreamGeometry();
using (StreamGeometryContext context = g.Open())
{
for (int i = 0; i < _width; i++)
for (int j = 0; j < _height; j++)
{
if (_maze[i, j].North == null)
{
context.BeginFigure(new Point(i*xFactor, j*yFactor), false, false);
context.LineTo(new Point((i + 1)*xFactor, j*yFactor), true, false);
}
if (_maze[i, j].East == null)
{
context.BeginFigure(new Point((i + 1)*xFactor, j*yFactor), false, false);
context.LineTo(new Point((i + 1)*xFactor, (j + 1)*yFactor), true, false);
}
}
context.BeginFigure(new Point(0, 0), false, false);
context.LineTo(new Point(0, yFactor*_height), true, false);
context.LineTo(new Point(xFactor*_width, yFactor*_height), true, false);
}
mazeDrawing.Geometry = g;
if (saveImagesCheckbox.IsChecked.Value)
{
SaveImage("maze.png");
MessageBox.Show("Maze saved in maze.png");
}
}
private void DrawSolution(IList<Cell> solution)
{
solutionDrawing.Geometry = null;
double xFactor = image.Width/_width;
double yFactor = image.Height/_height;
var g = new StreamGeometry();
using (StreamGeometryContext context = g.Open())
{
context.BeginFigure(new Point((solution[0].X + 0.5)*xFactor, (solution[0].Y + 0.5)*yFactor), false,
false);
for (int i = 1; i < solution.Count; i++)
{
context.LineTo(new Point((solution[i].X + 0.5)*xFactor, (solution[i].Y + 0.5)*yFactor), true, false);
}
}
solutionDrawing.Geometry = g;
if (saveImagesCheckbox.IsChecked.Value)
{
SaveImage("solution.png");
MessageBox.Show("Solution saved in solution.png");
}
}
private void SaveImage(string filename)
{
RenderTargetBitmap bmp = new RenderTargetBitmap(_width * 10, _height * 10, 96, 96, PixelFormats.Default);
image.Measure(new Size(_width * 10, _height * 10));
bmp.Render(image);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
{
encoder.Save(fs);
}
}
}
}