-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeControl.cs
1101 lines (964 loc) · 44.8 KB
/
MazeControl.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 Garrett Blankenberg
*
* Copyright 2020 Harjit Singh
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace MouseControls
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
/// <summary>
/// Represents a control used to maintain and display maze state.
/// </summary>
public partial class MazeControl : UserControl
{
/// <summary>
/// The private storage for the path.
/// </summary>
private Point[] path;
/// <summary>
/// The private storage for the solver coordinates.
/// </summary>
private Point[] coord;
/// <summary>
/// The private storage for the ExpandPath coordinates.
/// </summary>
private int[] coordCopy = new int[100*100];
/// <summary>
/// The private storage for the cost.
/// </summary>
private int[] cost = new int[1024];
/// <summary>
/// Private storage for the number of rows.
/// </summary>
private int numberOfRows;
/// <summary>
/// Private storage for the number of columns.
/// </summary>
private int numberOfColumns;
/// <summary>
/// Private storage for the mouse coordinate.
/// </summary>
private Point mouseCoord;
/// <summary>
/// Private storage for the ExpandLine coordinates.
/// </summary>
private Point[] ExpandLine = new Point[2];
/// <summary>
/// Private storage for the EndPoint coordinates.
/// </summary>
private Point EndPoint;
/// <summary>
/// Private storage for the ExpandPoint coordinates.
/// </summary>
private List<Point> ExpandPoint = new List<Point>();
/// <summary>
/// Initializes a new instance of the MazeControl class.
/// </summary>
public MazeControl()
{
this.NumberOfRows = 16;
this.NumberOfColumns = 16;
this.BackColor = Color.Black;
this.WallColor = Color.Red;
this.WallFilled = true;
this.NoWallColor = this.BackColor;
this.NoWallFilled = false;
this.CorrectedWallColor = Color.Green;
this.CorrectedWallFilled = true;
this.CorrectedNoWallColor = Color.Green;
this.CorrectedNoWallFilled = false;
this.NotMappedColor = Color.Red;
this.NotMappedFilled = false;
this.NotMappedNoWallColor = Color.Red;
this.NotMappedNoWallFilled = false;
this.NotMappedWallColor = Color.Red;
this.NotMappedWallFilled = true;
this.PegColor = Color.DarkRed;
this.PathColor = Color.Yellow;
this.PointEndColor = Color.Green;
this.PointExpandColor = Color.Yellow;
this.FontColor = Color.White;
this.MouseColor = Color.AliceBlue;
this.Path = new Point[0];
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.InitializeComponent();
}
/// <summary>
/// Gets a two dimensional array for all the interior horizontal walls of the maze, with the origin being the bottom left corner of the maze.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public WallState[,] HorizontalWalls
{
get;
private set;
}
/// <summary>
/// Gets a two dimensional array for all the interior vertical walls of the maze, with the origin being the bottom left corner of the maze.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public WallState[,] VerticalWalls
{
get;
private set;
}
/// <summary>
/// Gets a two dimensional array for all the cells that have been visited in the maze, with the origin being the bottom left corner of the maze.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Boolean[,] VisitedCells
{
get;
private set;
}
/// <summary>
/// Gets or sets the path for the maze as an array of points with the origin of the maze in the lower left corner.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Point[] Path
{
get
{
return this.path;
}
set
{
this.path = value;
this.Refresh();
}
}
/// <summary>
/// Gets or sets the number of rows for this maze.
/// </summary>
[Category("Design")]
[Description("The number of rows in the maze control.")]
public int NumberOfRows
{
get
{
return this.numberOfRows;
}
set
{
this.numberOfRows = value;
if (this.NumberOfRows > 0 && this.NumberOfColumns > 0)
{
this.HorizontalWalls = new WallState[this.NumberOfRows + 1, this.NumberOfColumns];
this.VerticalWalls = new WallState[this.NumberOfRows, this.NumberOfColumns + 1];
this.VisitedCells = new Boolean[this.NumberOfRows, this.NumberOfColumns];
this.DrawBorderWalls();
}
}
}
/// <summary>
/// Gets or sets the number of columns for this maze.
/// </summary>
[Category("Design")]
[Description("The number of columns in the maze control.")]
public int NumberOfColumns
{
get
{
return this.numberOfColumns;
}
set
{
this.numberOfColumns = value;
if (this.NumberOfRows > 0 && this.NumberOfColumns > 0)
{
this.HorizontalWalls = new WallState[this.NumberOfRows + 1, this.NumberOfColumns];
this.VerticalWalls = new WallState[this.NumberOfRows, this.NumberOfColumns + 1];
this.VisitedCells = new Boolean[this.NumberOfRows, this.NumberOfColumns];
this.DrawBorderWalls();
}
}
}
/// <summary>
/// Gets or sets the color for the pegs.
/// </summary>
[Category("Appearance")]
[Description("The color of the pegs.")]
public Color PegColor
{
get;
set;
}
/// <summary>
/// Gets or sets the color for the mouse.
/// </summary>
[Category("Appearance")]
[Description("The color of the mouse.")]
public Color MouseColor
{
get;
set;
}
/// <summary>
/// Gets or sets the wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the detected walls.")]
public Color WallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the wall is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the wall is drawn as a filled rectangle.")]
public bool WallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the no-wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the detected no-walls.")]
public Color NoWallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the no-wall rectangle is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the no-wall is drawn as a filled rectangle.")]
public bool NoWallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the corrected wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the corrected walls.")]
public Color CorrectedWallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the corrected wall is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the corrected wall is drawn as a filled rectangle.")]
public bool CorrectedWallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the corrected no-wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the corrected no-walls.")]
public Color CorrectedNoWallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the corrected no-wall rectangle is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the corrected no-wall is drawn as a filled rectangle.")]
public bool CorrectedNoWallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the not mapped color.
/// </summary>
[Category("Appearance")]
[Description("The color of the not-mapped walls.")]
public Color NotMappedColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the not-mapped rectangle is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the not-mapped edge is drawn as a filled rectangle.")]
public bool NotMappedFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the not mapped no wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the not-mapped no walls.")]
public Color NotMappedNoWallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the not-mapped no wall rectangle is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the not-mapped no wall is drawn as a filled rectangle.")]
public bool NotMappedNoWallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the not mapped wall color.
/// </summary>
[Category("Appearance")]
[Description("The color of the not-mapped wall.")]
public Color NotMappedWallColor
{
get;
set;
}
/// <summary>
/// Gets or sets a value indicating whether the not-mapped wall rectangle is drawn as a filled rectangle.
/// </summary>
[Category("Appearance")]
[Description("A value indicating whether the not-mapped wall is drawn as a filled rectangle.")]
public bool NotMappedWallFilled
{
get;
set;
}
/// <summary>
/// Gets or sets the path color.
/// </summary>
[Category("Appearance")]
[Description("The color of the paths.")]
public Color PathColor
{
get;
set;
}
/// <summary>
/// Gets or sets the solver expansion path end point color.
/// </summary>
[Category("Appearance")]
[Description("The color of solver expansion path end point.")]
public Color PointEndColor
{
get;
set;
}
/// <summary>
/// Gets or sets the solver expansion point color.
/// </summary>
[Category("Appearance")]
[Description("The color of solver expansion point.")]
public Color PointExpandColor
{
get;
set;
}
/// <summary>
/// Gets or sets the font color.
/// </summary>
[Category("Appearance")]
[Description("The color of the fonts.")]
public Color FontColor
{
get;
set;
}
/// <summary>
/// Adds cell to display and indicate that it has been visited. This is used when we load in a maze and then run it.
/// </summary>
/// <param name="cellCoordinate">The cell coordinate nibbles, in 0xYX format.</param>
public void vAddCellVisited(byte cellCoordinate)
{
Point coordinate = new Point(cellCoordinate & 0x0F, (cellCoordinate >> 4) & 0x0F);
this.VisitedCells[coordinate.Y, coordinate.X] = true;
// change the cell's walls to indicate it has been mapped
WallFlags walls = WallFlags.MappedNorth |
WallFlags.MappedEast |
WallFlags.MappedSouth |
WallFlags.MappedWest;
UpdateWallsWithRefresh(cellCoordinate, walls);
}
/// <summary>
/// Draws the mouse in the specified cell.
/// </summary>
/// <param name="cellCoordinate">The cell coordinate nibbles, in 0xYX format.</param>
public void DrawMouse(byte cellCoordinate)
{
this.mouseCoord = new Point(cellCoordinate & 0x0F, (cellCoordinate >> 4) & 0x0F);
this.Refresh();
}
/// <summary>
/// Draws a line between the two points we are expanding.
/// </summary>
/// <param name="FromX, FromY">The X and Y coordinates of the cell we are expanding from.</param>
/// <param name="ToX, ToY">The X and Y coordinates of the cell we are expanding to.</param>
public void DrawExpandLine(int FromX, int FromY, int ToX, int ToY)
{
this.ExpandLine[0].X = FromX;
this.ExpandLine[0].Y = FromY;
this.ExpandLine[1].X = ToX;
this.ExpandLine[1].Y = ToY;
this.Refresh();
}
/// <summary>
/// Draws the last cell in the path.
/// </summary>
/// <param name="X, Y">Even integers are the X coordinate and Odd integers are the corresponding Y coordinate. The coordinates include the cell and the pegs.</param>
public void DrawEndPoint(int X, int Y)
{
this.EndPoint.X = X;
this.EndPoint.Y = Y;
this.Refresh();
}
/// <summary>
/// Draws the solver path from an array of integers.
/// The origin of the maze in the lower left corner.
/// </summary>
/// <param name="coordArray">The path coordinate integers. Even integers are the X coordinate and Odd integers are the corresponding Y coordinate. The coordinates include the cell and the pegs.</param>
/// <param name="coordQty">The number of coordinate pairs.</param>
public void DrawExpandPath(IntPtr coordArray, int coordQty)
{
Marshal.Copy(coordArray, this.coordCopy, 0, coordQty * 2);
Point[] newCoord = new Point[coordQty];
for (int i = 0, j = 0; j < coordQty; i += 2, j++)
{
newCoord[j].X = coordCopy[i];
newCoord[j].Y = coordCopy[i + 1];
}
this.coord = newCoord;
// update coordinate path on the display
this.Refresh();
}
/// <summary>
/// Draws the cell being expanded.
/// </summary>
/// <param name="X, Y">Even integers are the X coordinate and Odd integers are the corresponding Y coordinate. The coordinates include the cell and the pegs.</param>
public void DrawExpandPoint(int X, int Y)
{
this.ExpandPoint.Add(new Point(X, Y));
this.Refresh();
}
/// <summary>
/// Remove a cell that has been expanded.
/// </summary>
/// <param name=""></param>
public void RemoveExpandPoint()
{
if (this.ExpandPoint.Count != 0)
{
this.ExpandPoint.RemoveAt(0);
this.Refresh();
}
}
/// <summary>
/// Sets the path from an array of coordinate nibbles in 0xYX format, with the origin of the maze in the lower left corner.
/// </summary>
/// <param name="pathCoordinates">The path coordinates nibbles, in 0xYX format.</param>
/// <param name="pathLength">The path length.</param>
public void SetPathFromCoordinateNibble(byte[] pathCoordinates, byte pathLength)
{
Point[] newPath = new Point[pathLength];
for (int i = 0; i < pathLength; i++)
{
newPath[i] = new Point(pathCoordinates[i] & 0x0F, (pathCoordinates[i] >> 4) & 0x0F);
}
this.Path = newPath;
}
/// <summary>
/// Displays the cost on the maze
/// </summary>
/// <param name="cost">The cost for each cell.</param>
/// <param name="costQty">The number of cost elements.</param>
public void SetCost(IntPtr costArray, int costQty)
{
Marshal.Copy(costArray, this.cost, 0, costQty);
// update cost info. on maze display
this.Refresh();
}
/// <summary>
/// Reset the wall states to all unmapped but don't refresh the display.
/// </summary>
public void ResetWallsNoRefresh()
{
for (int row = 0; row < this.NumberOfRows; row++)
{
for (int column = 0; column < this.NumberOfColumns; column++)
{
if (row != 0)
{
this.HorizontalWalls[row, column] = WallState.NotMapped;
}
if (column != 0)
{
this.VerticalWalls[row, column] = WallState.NotMapped;
}
this.VisitedCells[row, column] = false;
}
}
return;
}
/// <summary>
/// Reset the wall states to all unmapped.
/// </summary>
public void ResetWalls()
{
// update maze to empty state
ResetWallsNoRefresh();
// update the display
this.Refresh();
}
/// <summary>
/// Updates the wall states from a coordinate nibble and wall state but doesn't refresh the display
/// I'm not sure what we use this routine for
/// </summary>
/// <param name="coordinateNibble">The coordinate nibble, in 0xYX format.</param>
/// <param name="wallFlags">The wall flags.</param>
public void UpdateNotMappedWallsNoRefresh(byte coordinateNibble, WallFlags wallFlags)
{
Point coordinate = new Point(coordinateNibble & 0x0F, (coordinateNibble >> 4) & 0x0F);
// South wall
this.HorizontalWalls[coordinate.Y, coordinate.X] = GetNotMappedWallState(
this.HorizontalWalls[coordinate.Y, coordinate.X],
(wallFlags & WallFlags.MappedSouth) != 0,
(wallFlags & WallFlags.SouthWall) != 0);
// North wall
this.HorizontalWalls[coordinate.Y + 1, coordinate.X] = GetNotMappedWallState(
this.HorizontalWalls[coordinate.Y + 1, coordinate.X],
(wallFlags & WallFlags.MappedNorth) != 0,
(wallFlags & WallFlags.NorthWall) != 0);
// West wall
this.VerticalWalls[coordinate.Y, coordinate.X] = GetNotMappedWallState(
this.VerticalWalls[coordinate.Y, coordinate.X],
(wallFlags & WallFlags.MappedWest) != 0,
(wallFlags & WallFlags.WestWall) != 0);
// East wall
this.VerticalWalls[coordinate.Y, coordinate.X + 1] = GetNotMappedWallState(
this.VerticalWalls[coordinate.Y, coordinate.X + 1],
(wallFlags & WallFlags.MappedEast) != 0,
(wallFlags & WallFlags.EastWall) != 0);
}
/// <summary>
/// Updates the wall states from a coordinate nibble and wall state and refreshes the display
/// </summary>
/// <param name="coordinateNibble">The coordinate nibble, in 0xYX format.</param>
/// <param name="wallFlags">The wall flags.</param>
public void UpdateWallsNoRefresh(byte coordinateNibble, WallFlags wallFlags)
{
Point coordinate = new Point(coordinateNibble & 0x0F, (coordinateNibble >> 4) & 0x0F);
// South wall
this.HorizontalWalls[coordinate.Y, coordinate.X] = GetNewWallState(
this.HorizontalWalls[coordinate.Y, coordinate.X],
(wallFlags & WallFlags.MappedSouth) != 0,
(wallFlags & WallFlags.SouthWall) != 0);
// North wall
this.HorizontalWalls[coordinate.Y + 1, coordinate.X] = GetNewWallState(
this.HorizontalWalls[coordinate.Y + 1, coordinate.X],
(wallFlags & WallFlags.MappedNorth) != 0,
(wallFlags & WallFlags.NorthWall) != 0);
// West wall
this.VerticalWalls[coordinate.Y, coordinate.X] = GetNewWallState(
this.VerticalWalls[coordinate.Y, coordinate.X],
(wallFlags & WallFlags.MappedWest) != 0,
(wallFlags & WallFlags.WestWall) != 0);
// East wall
this.VerticalWalls[coordinate.Y, coordinate.X + 1] = GetNewWallState(
this.VerticalWalls[coordinate.Y, coordinate.X + 1],
(wallFlags & WallFlags.MappedEast) != 0,
(wallFlags & WallFlags.EastWall) != 0);
return;
}
/// <summary>
/// Updates the wall states from a coordinate nibble and wall state and refreshes the display
/// </summary>
/// <param name="coordinateNibble">The coordinate nibble, in 0xYX format.</param>
/// <param name="wallFlags">The wall flags.</param>
public void UpdateWallsWithRefresh(byte coordinateNibble, WallFlags wallFlags)
{
UpdateWallsNoRefresh(coordinateNibble, wallFlags);
// make sure walls are displayed after they are added
this.Refresh();
return;
}
/// <summary>
/// Draws the control surface.
/// </summary>
/// <param name="e">The OnPaint arguments.</param>
protected override void OnPaint(PaintEventArgs e)
{
const float PegToWallRatio = 0.2F;
int numberOfPegsWide = this.NumberOfRows + 1;
int numberOfPegsTall = this.NumberOfColumns + 1;
float horizontalBorderInPixels = (int)(this.Width * 0.04);
float verticalBorderInPixels = (int)(this.Height * 0.04);
float mazeWidth = this.Width - (horizontalBorderInPixels * 2);
float mazeHeight = this.Height - (verticalBorderInPixels * 2);
float pegWidth = (mazeWidth * PegToWallRatio) / numberOfPegsWide;
float pegHeight = (mazeHeight * PegToWallRatio) / numberOfPegsTall;
float horizontalWallWidth = (mazeWidth * (1 - PegToWallRatio)) / this.NumberOfRows;
float verticalWallHeight = (mazeHeight * (1 - PegToWallRatio)) / this.NumberOfColumns;
List<Rectangle> pegRectangles = new List<Rectangle>(numberOfPegsWide * numberOfPegsTall);
List<Rectangle> wallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> notMappedRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> notMappedNoWallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> notMappedWallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> noWallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> correctedWallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
List<Rectangle> correctedNoWallRectangles = new List<Rectangle>(this.NumberOfRows * this.NumberOfColumns);
SizeF pegSize = new SizeF(pegWidth, pegHeight);
SizeF horizontalRectangleSize = new SizeF(horizontalWallWidth, pegSize.Height);
SizeF verticalRectangleSize = new SizeF(pegSize.Width, verticalWallHeight);
PointF mazeBottomLeft = new PointF(horizontalBorderInPixels, this.Height - verticalBorderInPixels);
PointF pegBottomLeft = mazeBottomLeft - new SizeF(0, pegSize.Height);
PointF rowBottomLeft = pegBottomLeft;
for (int row = 0; row < this.NumberOfRows + 1; row++)
{
for (int column = 0; column < this.NumberOfColumns + 1; column++)
{
// Add the top left peg
pegRectangles.Add(new Rectangle(new Point((int)pegBottomLeft.X, (int)pegBottomLeft.Y), new Size((int)pegWidth, (int)pegHeight)));
if (column < this.NumberOfColumns)
{
Rectangle rectangle = new Rectangle((int)(pegBottomLeft.X + pegSize.Width), (int)pegBottomLeft.Y, (int)horizontalRectangleSize.Width, (int)horizontalRectangleSize.Height);
WallState wallstate = this.HorizontalWalls[row, column];
switch (wallstate)
{
case WallState.Wall:
wallRectangles.Add(rectangle);
break;
case WallState.NoWall:
noWallRectangles.Add(rectangle);
break;
case WallState.CorrectedWall:
correctedWallRectangles.Add(rectangle);
break;
case WallState.CorrectedNoWall:
correctedNoWallRectangles.Add(rectangle);
break;
case WallState.NotMapped:
notMappedRectangles.Add(rectangle);
break;
case WallState.NotMappedNoWall:
notMappedNoWallRectangles.Add(rectangle);
break;
case WallState.NotMappedWall:
notMappedWallRectangles.Add(rectangle);
break;
default:
throw new InvalidOperationException(string.Format("Unrecognzied value for wall state '{0}'.", wallstate));
}
}
if (row < this.NumberOfRows)
{
Rectangle rectangle = new Rectangle((int)pegBottomLeft.X, (int)(pegBottomLeft.Y - verticalRectangleSize.Height), (int)verticalRectangleSize.Width, (int)verticalRectangleSize.Height);
WallState wallstate = this.VerticalWalls[row, column];
switch (wallstate)
{
case WallState.Wall:
wallRectangles.Add(rectangle);
break;
case WallState.NoWall:
noWallRectangles.Add(rectangle);
break;
case WallState.CorrectedWall:
correctedWallRectangles.Add(rectangle);
break;
case WallState.CorrectedNoWall:
correctedNoWallRectangles.Add(rectangle);
break;
case WallState.NotMapped:
notMappedRectangles.Add(rectangle);
break;
case WallState.NotMappedNoWall:
notMappedNoWallRectangles.Add(rectangle);
break;
case WallState.NotMappedWall:
notMappedWallRectangles.Add(rectangle);
break;
default:
throw new InvalidOperationException(string.Format("Unrecognzied value for wall state '{0}'.", wallstate));
}
}
pegBottomLeft += new SizeF(pegSize.Width + horizontalRectangleSize.Width, 0);
}
pegBottomLeft = rowBottomLeft = rowBottomLeft - new SizeF(0, pegSize.Height + verticalRectangleSize.Height);
}
e.Graphics.Clear(this.BackColor);
this.DrawRectangles(e.Graphics, this.WallFilled, this.WallColor, wallRectangles);
this.DrawRectangles(e.Graphics, this.NoWallFilled, this.NoWallColor, noWallRectangles);
this.DrawRectangles(e.Graphics, this.CorrectedWallFilled, this.CorrectedWallColor, correctedWallRectangles);
this.DrawRectangles(e.Graphics, this.CorrectedNoWallFilled, this.CorrectedNoWallColor, correctedNoWallRectangles);
this.DrawRectangles(e.Graphics, this.NotMappedFilled, this.NotMappedColor, notMappedRectangles);
this.DrawRectangles(e.Graphics, this.NotMappedNoWallFilled, this.NotMappedNoWallColor, notMappedNoWallRectangles);
this.DrawRectangles(e.Graphics, this.NotMappedWallFilled, this.NotMappedWallColor, notMappedWallRectangles);
this.DrawRectangles(e.Graphics, true, this.PegColor, pegRectangles);
if (this.Path != null)
{
List<PointF> pathLines = new List<PointF>(this.Path.Length);
for (int i = 0; i < this.Path.Length; i++)
{
pathLines.Add(
new PointF(
(((float)this.Path[i].X) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + pegSize.Width + (horizontalRectangleSize.Width / 2),
(((float)(this.NumberOfRows - this.Path[i].Y - 1)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + pegSize.Height + (verticalRectangleSize.Height / 2)));
}
// must have two points to draw a line!
if (pathLines.Count >= 2)
{
e.Graphics.DrawLines(new Pen(new SolidBrush(this.PathColor), 4), pathLines.ToArray());
}
Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular, GraphicsUnit.Pixel);
for (int i = 0; i < pathLines.Count; i++)
{
e.Graphics.DrawString(i.ToString(), font, new SolidBrush(this.FontColor), pathLines[i] + new SizeF(2, 2));
}
}
if ((this.coord != null) && (this.coord.Length >= 2))
{
List<PointF> coordLines = new List<PointF>(this.coord.Length);
for (int i = 0; i < this.coord.Length; i++)
{
coordLines.Add(
new PointF(
(((((float)(this.coord[i].X - 1)) / 2) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + pegSize.Width + (horizontalRectangleSize.Width / 2)),
((((float)(this.NumberOfRows * 2 - this.coord[i].Y - 1)) / 2) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + pegSize.Height + (verticalRectangleSize.Height / 2)));
}
// e.Graphics.DrawLines(new Pen(new SolidBrush(this.PathColor), 4), coordLines.ToArray());
e.Graphics.DrawCurve(new Pen(new SolidBrush(this.PathColor), 4), coordLines.ToArray(), 0.25F);
// e.Graphics.DrawBeziers(new Pen(new SolidBrush(this.PathColor), 4), coordLines.ToArray());
#if DEBUG_DISPLAY_PATH_COORD
Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular, GraphicsUnit.Pixel);
for (int i = 0; i < coordLines.Count; i++)
{
e.Graphics.DrawString(i.ToString(), font, new SolidBrush(this.FontColor), coordLines[i] + new SizeF(2, 2));
}
#endif
}
int mouseX, mouseY;
mouseX = (int)((((float)mouseCoord.X) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + (pegSize.Width + horizontalRectangleSize.Width) / 2);
mouseY = (int)((((float)(this.NumberOfRows - mouseCoord.Y - 1)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + (pegSize.Height + verticalRectangleSize.Height) / 2);
Rectangle mouseRectangle = new Rectangle(mouseX - (int)(pegWidth / 2), mouseY - (int)(pegHeight / 2), (int)(pegWidth * 2), (int)(pegHeight * 2));
e.Graphics.FillRectangle(new SolidBrush(this.MouseColor), mouseRectangle);
// draw end point and expansion points
int pointX, pointY;
pointX = (int)((((float)EndPoint.X - 1) / 2 * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + (pegSize.Width + horizontalRectangleSize.Width) / 2);
pointY = (int)((((float)(this.NumberOfRows * 2 - EndPoint.Y - 1)) / 2 * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + (pegSize.Height + verticalRectangleSize.Height) / 2);
Rectangle pointRectangle = new Rectangle(pointX, pointY, (int)pegWidth, (int)pegHeight);
e.Graphics.FillRectangle(new SolidBrush(this.PointEndColor), pointRectangle);
pointRectangle.Width = (int)(pegWidth / 2);
pointRectangle.Height = (int)(pegHeight / 2);
if (ExpandPoint.Count != 0)
{
foreach (Point point in ExpandPoint)
{
// if this is a cell, then put it at the bottom of the cell; for a wall, leave it where it is
PointF Offset = new Point( 0, 0 );
if ((point.X & 0x01) == 0)
{
Offset.X = pegWidth / 2;
}
else
{
// Offset.X = horizontalWallWidth / 2;
}
if ((point.Y & 0x01) == 1)
{
Offset.Y = 0; // verticalWallHeight / 4;
}
pointRectangle.X = (int)(((float)point.X / 2 * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + Offset.X);
pointRectangle.Y = (int)((((float)((NumberOfRows * 2) - point.Y - 1)) / 2 * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + ((pegSize.Height + verticalRectangleSize.Height) / 2) + Offset.Y);
e.Graphics.FillRectangle(new SolidBrush(this.PointExpandColor), pointRectangle);
}
}
int LineFromX, LineFromY, LineToX, LineToY;
LineFromX = (int)((((float)ExpandLine[0].X) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + (pegSize.Width + horizontalRectangleSize.Width) / 2);
LineFromY = (int)((((float)(this.NumberOfRows - ExpandLine[0].Y - 1)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + (pegSize.Height + verticalRectangleSize.Height) / 2);
LineToX = (int)((((float)ExpandLine[1].X) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + (pegSize.Width + horizontalRectangleSize.Width) / 2);
LineToY = (int)((((float)(this.NumberOfRows - ExpandLine[1].Y - 1)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + (pegSize.Height + verticalRectangleSize.Height) / 2);
Point[] Line =
{
new Point(LineFromX, LineFromY),
new Point(LineToX, LineToY)
};
e.Graphics.DrawLines(new Pen(new SolidBrush(this.PathColor), 2), Line);
// code to show cost on maze
if (cost != null)
{
int costQty = (this.NumberOfColumns * this.NumberOfRows);
PointF costLocation = new PointF(0, 0);
Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular, GraphicsUnit.Pixel);
SolidBrush brush = new SolidBrush(this.FontColor);
for (int cellX = 0; cellX < this.NumberOfRows; cellX++)
{
for (int cellY = 0; cellY < this.NumberOfColumns; cellY++)
{
// BUGBUG: Do loop above using the eqn. below
costLocation.X = ((((float)cellX) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels + (pegSize.Width + horizontalRectangleSize.Width) / 2);
costLocation.Y = ((((float)(this.NumberOfRows - cellY - 1)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels + (pegSize.Height + verticalRectangleSize.Height) / 2);
costLocation.X -= 1.5F * pegSize.Width;
costLocation.Y += pegSize.Height;
// The solver code currently organizes the data in column major format
int i = cellY + cellX * this.NumberOfColumns;
e.Graphics.DrawString(Convert.ToString(cost[i], 16), font, brush, costLocation);
}
}
}
// code to show cell coordinates on left and bottom of maze
// only show if the maze is a square
if (this.numberOfColumns == this.numberOfRows)
{
int nodeQty = this.NumberOfColumns;
PointF nodeHorizLoc = new PointF(0, 0);
PointF nodeVertLoc = new PointF(0, 0);
Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular, GraphicsUnit.Pixel);
SolidBrush brush = new SolidBrush(this.FontColor);
for (int node = 0; node < nodeQty; node++)
{
nodeHorizLoc.X = (((float)node) * (horizontalRectangleSize.Width + pegSize.Width)) + horizontalBorderInPixels - pegSize.Width / 2;
nodeHorizLoc.Y = ((((float)(this.NumberOfRows)) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels) + pegSize.Height; // + verticalRectangleSize.Height);
// show horizontal peg label
e.Graphics.DrawString(Convert.ToString(2 * node), font, brush, nodeHorizLoc);
// show horizontal wall label
nodeHorizLoc.X += pegSize.Width / 2 + horizontalRectangleSize.Width / 2;
e.Graphics.DrawString(Convert.ToString(2 * node + 1), font, brush, nodeHorizLoc);
nodeVertLoc.X = (float) horizontalBorderInPixels / 3;
nodeVertLoc.Y = (((float)this.NumberOfRows - node) * (verticalRectangleSize.Height + pegSize.Height)) + verticalBorderInPixels - pegSize.Height; // / 2;
// show vertical peg label
e.Graphics.DrawString(Convert.ToString(2 * node), font, brush, nodeVertLoc);
// show vertical wall label
nodeVertLoc.Y -= pegSize.Width / 2 + horizontalRectangleSize.Width / 2;
e.Graphics.DrawString(Convert.ToString(2 * node + 1), font, brush, nodeVertLoc);
}
}
}
/// <summary>
/// Gets the new not mapped wall state.
/// </summary>