-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
1666 lines (1536 loc) · 86.5 KB
/
Form1.cs
File metadata and controls
1666 lines (1536 loc) · 86.5 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
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
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
namespace GameOfLife
{
public partial class Form1 : Form
{
/// <summary>
/// The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead (or populated and unpopulated, respectively).
/// Every cell interacts with its eight neighbors, which are the cells that horizontally, vertically, or diagonally adjacent. At each step in time, the following transition occur:
///
/// 1) Any live cell with fewer than two live neighbors dies, as if by underpopulation.
/// 2) Any live cell with two or three live neighbors lives on to the next generation.
/// 3) Any live cell with more than three live neighbors dies, as if by overpopulation.
/// 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
///
/// These rules, which compare the behavior of the automaton to real life, can be condensed into the following:
///
/// 1) Any live cell with two or three live neighbors survives.
/// 2) Any dead cell with three live neighbors become a live cell.
/// 3) all other live cells die in the next generation. similarly all other dead cells stay dead.
///
/// The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed, live or dead.
/// Births and deaths occur simultaneously, and the discrete moment at which this happens is sometime called a tick.
/// Each generation is a pure function of the preceding one. The rules continue to be applied repeatedly to create further generations.
/// </summary>
#region Constant
int TOP_PANEL_HEIGHT = 20;
int BOTTOM_PANEL_HEIGHT = 30;
int TIMER_SPEED_INCREASE_DECREASE = 20;
#endregion
#region Fields
HeatmapChartControlDX heatmapChartControl;
SimpleButton startStopGenerationButton;
PanelControl topLeftPanelControl;
PanelControl topRightPanelControl;
PanelControl bottomLeftPanelControl;
PanelControl bottomRightPanelControl;
Label speedTextEdit;
Label cellSizeTextEdit;
Label generationCounter;
Label instructionText;
SimpleButton clearButton;
SimpleButton startFromBeginningButton;
SimpleButton openRulesDialogButton;
SimpleButton openFileButton;
SimpleButton exportToFile;
SimpleButton plusSpeedButton;
SimpleButton minusSpeedButton;
SimpleButton plusCellSizeButton;
SimpleButton minusCellSizeButton;
PanelControl rulesPanel;
Thread gameOfLifeThr;
int[][] startingValuesToPlot;
int[][] currGenValuesToPlot;
int[][] copyPasteStartingValuesToPlot;
int[][] copyPasteCurrGenValuesToPlot;
int nRowHeatmap;
int nColHeatmap;
int generationCount;
int cellSizePixels;
int timerSpeedMilliSecond;
int maximumCellSizePossible;
int minimumCellSizePossible;
int maximumSpeedPossible;
int minimumSpeedPossible;
int xStartIdxCP;
int yStartIdxCP;
int xEndIdxCP;
int yEndIdxCP;
bool keepIteratig;
bool hasMatrixValuesToPlot;
bool isRulesPanelVisualized;
#endregion
#region Constructor
public Form1()
{
// Initialize Components.
InitializeComponent();
// Set the name of the Form.
this.Text = "Game Of Life";
// Loading the Default variable Values.
LoadDefaultValues();
// Create the HeatmapControl and add it to the control list.
InitializeHeatMapAndControls();
// Initialize the dimensions and the number of the Heatmap rows and cols of the Grid based on the dimension of the form.
InitializeDimensionsOfHeatmapGrid();
// Initialize the empty starting heatmap matrix with the values to plot.
InitializeHetamapMatrix();
// Only at first opening of the Game Of Life Form.
LoadDefaultPatternAtFirstStart();
}
#endregion
#region Methods
private void LoadDefaultPatternAtFirstStart()
{
//StreamReader reader = System.IO.File.OpenText(@"InfiniteSpaceshipGeneratorPattern.txt");
StreamReader reader = null;
string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GameOfLifePattern");
// Check if the directory exists.
if (Directory.Exists(folderPath))
{
reader = System.IO.File.OpenText(folderPath + "/" + "InfiniteSpaceshipGeneratorPattern.txt");
}
string line;
List<char[]> parsedFile = new List<char[]>();
if (reader != null)
{
// Loop through all the lines in the file.
while ((line = reader.ReadLine()) != null)
{
// REmove the empty spaces at the end of the line if there are.
line = line.TrimEnd();
line = line.TrimStart();
// Split each character and store it in an array.
List<char> items = new List<char>();
// Loop through all the characters and store them in an array.
foreach (char c in line)
{
items.Add(c);
}
parsedFile.Add(items.ToArray());
}
if (parsedFile.Count == 0 || parsedFile[0].Length == 0)
return;
// Draw and Add this starting pattern in the heatmap matrix.
// First, in order to fo that, check that the uploaded matrix doesn't exceeds the dimension of the heatmap matrix in the control.
while (parsedFile.Count + 10 > this.startingValuesToPlot.Length || parsedFile[0].Length + 10 > this.startingValuesToPlot[0].Length)
{
if (this.cellSizePixels - 1 > this.minimumCellSizePossible)
{
this.cellSizePixels -= 1;
// I can modify the dimension of the heatmap matrix.
DecreaseCellSizeMatrixDimension();
}
}
// Then just add the uploaded matrix in the center of the heatmap control.
// Find the index of the heatmap startingValuesToPlot where to start to copy the uploaded matrix.
int rowStartIdx = (int)((this.startingValuesToPlot.Length - parsedFile.Count) / 2.0);
int colStartIdx = (int)((this.startingValuesToPlot[0].Length - parsedFile[0].Length) / 2.0);
// Copy (while rotating its orientation on the Y axis) the uploaded matrix in the startingValuesToPlot.
for (int row = 0; row < parsedFile.Count; row++)
{
for (int col = 0; col < parsedFile[0].Length; col++)
{
if (parsedFile[row][col] != '.')
this.startingValuesToPlot[this.startingValuesToPlot.Length - 1 - (rowStartIdx + row)][colStartIdx + col] = 1;
}
}
this.hasMatrixValuesToPlot = true;
AssignValuesToPlot(this.startingValuesToPlot);
}
}
/// <summary>
/// Initialize the Default values of the variables.
/// </summary>
private void LoadDefaultValues()
{
// Initialize some variables Default Values.
this.cellSizePixels = 10;
this.timerSpeedMilliSecond = 200;
this.keepIteratig = false;
this.generationCount = 0;
// Initialize the minimum and maximum values for the Cell Size and the Speed of the Refresh.
// Minimum possible cell size is 2 pixels.
this.minimumCellSizePossible = 2;
// Maximum possible cell size is the initial with size of the panel divided by two.
this.maximumCellSizePossible = (int)(this.Size.Width / 2.0);
// Refresh is done every 1 second.
this.minimumSpeedPossible = 1000;
// Refresh is done every 20 milliSeconds.
this.maximumSpeedPossible = 20;
}
/// <summary>
/// Initialize all the controls that will be part of the Form.
/// </summary>
private void InitializeHeatMapAndControls()
{
// There will be two panels on the top and on the bottom of the window form.
//
// Form Name.
// --- TOP PANELS which will contain some instruction and the start bottom.
// LEFT PANEL
this.topLeftPanelControl = new PanelControl();
this.topRightPanelControl = new PanelControl();
this.exportToFile = new SimpleButton();
this.exportToFile.Text = "Export To File";
this.exportToFile.Dock = DockStyle.Left;
this.topLeftPanelControl.Controls.Add(this.exportToFile);
this.openFileButton = new SimpleButton();
this.openFileButton.Text = "Open From File";
this.openFileButton.Dock = DockStyle.Left;
this.topLeftPanelControl.Controls.Add(this.openFileButton);
this.instructionText = new Label();
this.instructionText.Text = "Draw your seed pattern and click start. Or: ";
this.instructionText.Dock = DockStyle.Left;
this.topLeftPanelControl.Controls.Add(this.instructionText);
// RIGHT PANEL
this.startStopGenerationButton = new SimpleButton();
this.startStopGenerationButton.Text = "Start";
this.startStopGenerationButton.Font = new Font("Arial", 10, FontStyle.Bold);
this.startStopGenerationButton.Dock = DockStyle.Fill;
this.topRightPanelControl.Controls.Add(this.startStopGenerationButton);
// Adding the instruction Button which will show an instruction dialog.
this.openRulesDialogButton = new SimpleButton();
this.openRulesDialogButton.Text = "Game Rules";
this.openRulesDialogButton.Font = new Font("Arial", 8, FontStyle.Bold);
this.openRulesDialogButton.Dock = DockStyle.Left;
//this.openRulesDialogButton.ForeColor = Color.LightBlue;
this.topRightPanelControl.Controls.Add(this.openRulesDialogButton);
// -- Add the two panels on top of the form.
this.topLeftPanelControl.Dock = DockStyle.None;
this.topRightPanelControl.Dock = DockStyle.None;
this.Controls.Add(topRightPanelControl);
this.Controls.Add(topLeftPanelControl);
// Initialize the panel which will contain the GameOfLife instructions.
rulesPanel = new PanelControl();
InitializeRulesMessagePanel(); rulesPanel.Dock = DockStyle.Fill;
this.Controls.Add(rulesPanel);
rulesPanel.SendToBack();
// --- HEATMAP CONTROL
// Create and add the HeatMapChartControl.
this.heatmapChartControl = new HeatmapChartControlDX();
this.heatmapChartControl.Dock = DockStyle.None;
//this.heatmapChartControl.DataHasBeenUpdated = true;
this.Controls.Add(heatmapChartControl);
heatmapChartControl.BringToFront();
// --- BOTTOM PANELS which will contain the Generation counter, some spin edit about Velocity and Dimension
// LEFT PANEL
this.bottomLeftPanelControl = new PanelControl();
// Size Controls
this.cellSizeTextEdit = new Label();
this.cellSizeTextEdit.Text = "Cell Size: ";
this.plusCellSizeButton = new SimpleButton();
this.minusCellSizeButton = new SimpleButton();
this.plusCellSizeButton.Text = "+";
this.minusCellSizeButton.Text = "-";
this.plusCellSizeButton.Font = new Font("Arial", 12, FontStyle.Bold);
this.minusCellSizeButton.Font = new Font("Arial", 12, FontStyle.Bold);
this.cellSizeTextEdit.Dock = DockStyle.Left;
this.plusCellSizeButton.Dock = DockStyle.Left;
this.minusCellSizeButton.Dock = DockStyle.Left;
this.bottomLeftPanelControl.Controls.Add(this.plusCellSizeButton);
this.bottomLeftPanelControl.Controls.Add(this.minusCellSizeButton);
this.bottomLeftPanelControl.Controls.Add(this.cellSizeTextEdit);
// Speed Controls
this.speedTextEdit = new Label();
this.speedTextEdit.Text = "Speed: ";
this.plusSpeedButton = new SimpleButton();
this.minusSpeedButton = new SimpleButton();
this.plusSpeedButton.Text = "Speed Up";
this.minusSpeedButton.Text = "Slow Down";
this.plusSpeedButton.Font = new Font("Arial", 9, FontStyle.Bold);
this.minusSpeedButton.Font = new Font("Arial", 9, FontStyle.Bold);
this.speedTextEdit.Dock = DockStyle.Left;
this.plusSpeedButton.Dock = DockStyle.Left;
this.minusSpeedButton.Dock = DockStyle.Left;
this.bottomLeftPanelControl.Controls.Add(this.plusSpeedButton);
this.bottomLeftPanelControl.Controls.Add(this.minusSpeedButton);
this.bottomLeftPanelControl.Controls.Add(this.speedTextEdit);
// RIGHT PANEL
this.bottomRightPanelControl = new PanelControl();
// Start From the Beginning Button
this.startFromBeginningButton = new SimpleButton();
this.startFromBeginningButton.Text = "Reset to Initial Pattern";
this.startFromBeginningButton.Dock = DockStyle.Right;
this.bottomRightPanelControl.Controls.Add(this.startFromBeginningButton);
// Clear Button
this.clearButton = new SimpleButton();
this.clearButton.Text = "Clear";
this.clearButton.Dock = DockStyle.Right;
this.clearButton.Font = new Font("Arial", 10, FontStyle.Bold);
this.bottomRightPanelControl.Controls.Add(this.clearButton);
// Generation Label
this.generationCounter = new Label();
this.generationCounter.Text = "Generation: 0";
this.generationCounter.Dock = DockStyle.Left;
this.bottomRightPanelControl.Controls.Add(this.generationCounter);
// Add the two panels on the bottom of the form (after the HeatMap Controls)
this.bottomLeftPanelControl.Dock = DockStyle.None;
this.bottomRightPanelControl.Dock = DockStyle.None;
this.Controls.Add(this.bottomLeftPanelControl);
this.Controls.Add(this.bottomRightPanelControl);
// Set the Bounds in the correct Position all the Panels and Controls.
SetBoundsToControls();
// Add the subscription to the events.
this.openFileButton.Click -= OpenFileButton_Click;
this.openFileButton.Click += OpenFileButton_Click;
this.exportToFile.Click -= ExportToFile_Click;
this.exportToFile.Click += ExportToFile_Click;
this.startFromBeginningButton.Click -= StartFromBeginningButton_Click;
this.startFromBeginningButton.Click += StartFromBeginningButton_Click;
this.startStopGenerationButton.Click -= StartStopGenerationButton_Click;
this.startStopGenerationButton.Click += StartStopGenerationButton_Click;
this.openRulesDialogButton.Click -= OpenRulesDialogButton_Click;
this.openRulesDialogButton.Click += OpenRulesDialogButton_Click;
this.clearButton.Click -= ClearButton_Click;
this.clearButton.Click += ClearButton_Click;
this.plusSpeedButton.Click += PlusSpeedButton_Click;
this.minusSpeedButton.Click += MinusSpeedButton_Click;
this.plusCellSizeButton.Click += IncreaseCellSizeButton_Click;
this.minusCellSizeButton.Click += DecreaseCellSizeButton_Click;
this.heatmapChartControl.HeatMapCellSelection -= HeatmapChartControl_HeatMapCellSelection;
this.heatmapChartControl.HeatMapCellSelection += HeatmapChartControl_HeatMapCellSelection;
this.heatmapChartControl.HeatMapCopyPasteSelection -= HeatmapChartControl_HeatMapCopyPasteSelection;
this.heatmapChartControl.HeatMapCopyPasteSelection += HeatmapChartControl_HeatMapCopyPasteSelection;
this.heatmapChartControl.HeatMapCopyPasteCtrlC -= HeatmapChartControl_HeatMapCopyPasteCtrlC;
this.heatmapChartControl.HeatMapCopyPasteCtrlC += HeatmapChartControl_HeatMapCopyPasteCtrlC;
this.heatmapChartControl.HeatMapCopyPasteCtrlX -= HeatmapChartControl_HeatMapCopyPasteCtrlX;
this.heatmapChartControl.HeatMapCopyPasteCtrlX += HeatmapChartControl_HeatMapCopyPasteCtrlX;
this.heatmapChartControl.HeatMapCopyPasteCtrlV -= HeatmapChartControl_HeatMapCopyPasteCtrlV;
this.heatmapChartControl.HeatMapCopyPasteCtrlV += HeatmapChartControl_HeatMapCopyPasteCtrlV;
}
/// <summary>
/// Initialize the Text that is displayed in the Rules and Information of the Game Of Life inside the "custom" panel.
/// </summary>
private void InitializeRulesMessagePanel()
{
string gameOfLifeRulesAndInformations =
"The Universe of the Game of Life is a two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, \"live\" or \"dead\".\r\n" +
"Every cell interacts with its eight neighbots, which are the cells horizonally, vertically, or diagonally adjacent. At each step in time, the following transition occur:\r\n" +
"\r\n1. Any live cell with fewer than two live neighbors dies, as if by underpopulation.\r\n" +
"2. Any live cell with two or three live neighbors lives on to the next generation.\r\n" +
"3. Any live cell with more than three live neighbors dies, as if by overpopulation.\r\n" +
"4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction." +
"\r\n\r\n" +
"Command for the usage:\r\n" +
" - Left mouse click --> Select / Make Cell Alive\r\n" +
" - Right mouse click --> Erease / Kill Cell\r\n" +
" - Ctrl + Right mouse click + Drag --> Select Multiple Cells / Make Multiple Cells Alive\r\n" +
" - Ctrl + C or X (After Multiple Cell Selection) --> Copy / Cut Selected Cells\r\n" +
" - Ctrl + V --> Paste Selected Cells" +
"\r\n\r\n\r\n" +
"Starting Pattern: \r\n" +
"https://github.com/AlessandroCaula/ProgrammingForFun/tree/main/GameOfLife/GameOfLifeStartingPatterns";
TextBox rulesText = new TextBox();
rulesText.Font = new Font(rulesText.Font.ToString(), 12);
rulesText.Dock = DockStyle.Fill;
rulesText.Multiline = true;
rulesText.Text = gameOfLifeRulesAndInformations;
rulesPanel.Controls.Add(rulesText);
SimpleButton closeRulesPanelButton = new SimpleButton();
closeRulesPanelButton.Text = "Close";
closeRulesPanelButton.Font = new Font("Arial", 15, FontStyle.Bold);
closeRulesPanelButton.Dock = DockStyle.Bottom;
rulesPanel.Controls.Add(closeRulesPanelButton);
closeRulesPanelButton.BackColor = Color.LightBlue;
// Add the event, which is fired when the Close button is clicked in the Rules and Information panel.
closeRulesPanelButton.Click -= CloseRulesPanelButton_Click;
closeRulesPanelButton.Click += CloseRulesPanelButton_Click;
}
/// <summary>
/// Set the Dock Bounds of the controls added to the Form.
/// </summary>
private void SetBoundsToControls()
{
if (this.topLeftPanelControl == null)
return;
// TOP PANELS position.
int leftPanelWidth = (int)(this.Size.Width / 2);
this.topLeftPanelControl.SetBounds(0, 0, leftPanelWidth, TOP_PANEL_HEIGHT);
this.instructionText.Width = TextRenderer.MeasureText(this.instructionText.Text, this.cellSizeTextEdit.Font).Width + 10;
this.openFileButton.Width = (leftPanelWidth - this.instructionText.Width) / 2;
this.exportToFile.Width = (leftPanelWidth - this.instructionText.Width) / 2;
this.topRightPanelControl.SetBounds(leftPanelWidth, 0, this.Size.Width - leftPanelWidth, TOP_PANEL_HEIGHT);
this.topLeftPanelControl.BringToFront();
this.topRightPanelControl.BringToFront();
// HEATMAP position
int heatmapHeight = this.Size.Height - TOP_PANEL_HEIGHT - BOTTOM_PANEL_HEIGHT - 40;
this.heatmapChartControl.SetBounds(0, TOP_PANEL_HEIGHT, this.Size.Width - 16, heatmapHeight);
// BOTTOM PANELS position
this.bottomLeftPanelControl.SetBounds(0, heatmapHeight + 20, leftPanelWidth, BOTTOM_PANEL_HEIGHT);
this.bottomRightPanelControl.SetBounds(leftPanelWidth, heatmapHeight + 20, this.Size.Width - leftPanelWidth, BOTTOM_PANEL_HEIGHT);
this.bottomLeftPanelControl.BringToFront();
this.bottomRightPanelControl.BringToFront();
// Set the width of the speed and cell labels.
this.speedTextEdit.Width = TextRenderer.MeasureText("Speed: ", this.speedTextEdit.Font).Width + 5;
this.cellSizeTextEdit.Width = TextRenderer.MeasureText("Cell Size: ", this.cellSizeTextEdit.Font).Width + 5;
// Set the length of the TrackBar
int leftPanelSpaceForPlusMinusButtons = (int)((leftPanelWidth - (this.speedTextEdit.Width + this.cellSizeTextEdit.Width) - 5) / 2.0);
int leftPanelSpaceForSingleButton = (int)(leftPanelSpaceForPlusMinusButtons / 2.0);
this.plusCellSizeButton.Width = leftPanelSpaceForSingleButton;
this.minusCellSizeButton.Width = leftPanelSpaceForSingleButton;
this.plusSpeedButton.Width = leftPanelSpaceForSingleButton;
this.minusSpeedButton.Width = leftPanelSpaceForSingleButton;
// Set the dimensions of the text and buttons in the bottom right panel.
int rightPanelControlsWidth = (int)(leftPanelWidth / 3.0);
this.generationCounter.Width = rightPanelControlsWidth;
this.clearButton.Width = rightPanelControlsWidth;
this.startFromBeginningButton.Width = rightPanelControlsWidth;
}
/// <summary>
/// Initialize the first dimensions of the Heatmap Grid.
/// </summary>
private void InitializeDimensionsOfHeatmapGrid()
{
// Retrieve the dimensions of the current form.
double formHighPixels = this.Size.Height - TOP_PANEL_HEIGHT - BOTTOM_PANEL_HEIGHT - 18;
double formWidthPixels = this.Size.Width;
// Compute the number of rows and columns of the heatmap grid.
this.nRowHeatmap = (int)(formHighPixels / this.cellSizePixels);
this.nColHeatmap = (int)(formWidthPixels / this.cellSizePixels);
// Check if the number of rows and/or columns is not an even number. In that case change it, and make it even.
if (this.nRowHeatmap % 2 != 0)
this.nRowHeatmap += 1;
if (this.nColHeatmap % 2 != 0)
this.nColHeatmap += 1;
}
/// <summary>
/// Thread iteration of the GameOfLife.
/// </summary>
private void GameOfLifeIterations()
{
if (this.hasMatrixValuesToPlot)
{
// Thread function
this.gameOfLifeThr = new Thread(GameOfLifeThreadIteration);
gameOfLifeThr.Start();
}
}
/// <summary>
/// Thread of the Game of Life Iteration.
/// </summary>
private void GameOfLifeThreadIteration()
{
int[][] prevGenValues = null;
if (this.currGenValuesToPlot != null)
prevGenValues = this.currGenValuesToPlot;
else
prevGenValues = this.startingValuesToPlot;
// Main itaration of the GameOfLife
while (this.keepIteratig)
{
// Sleep the Thread for 300 ms.
Thread.Sleep(this.timerSpeedMilliSecond);
// Call the method with the instruction for the Game Of Life
this.currGenValuesToPlot = GameOfLifeGenerationComputation(prevGenValues);
// Update the previous generation.
prevGenValues = this.currGenValuesToPlot;
// Increase the generation Count.
this.generationCount++;
this.BeginInvoke((MethodInvoker)(() =>
{
// Plot the current Generation Values
AssignValuesToPlot(this.currGenValuesToPlot);
// Update the Text of the new generation.
this.generationCounter.Text = "Generation: " + this.generationCount.ToString();
}));
}
}
#endregion
#region UtilityMethods
/// <summary>
/// Method for the dimension initialization of the empty Heatmap, that will be the two-dimensional space in which the Game Of Life will be created.
/// </summary>
private void InitializeHetamapMatrix()
{
this.keepIteratig = false;
this.hasMatrixValuesToPlot = false;
// Initialize an empty starting values to plot matrix.
this.startingValuesToPlot = new int[this.nRowHeatmap][];
for (int row = 0; row < this.startingValuesToPlot.Length; row++)
{
this.startingValuesToPlot[row] = new int[this.nColHeatmap];
for (int col = 0; col < this.startingValuesToPlot[row].Length; col++)
{
// Initialize the entire matrix values to plot with Zeros.
this.startingValuesToPlot[row][col] = 0;
}
}
AssignValuesToPlot(this.startingValuesToPlot);
}
/// <summary>
/// Updating or creating the living or death cell in the heatmap.
/// </summary>
/// <param name="xCoords"></param>
/// <param name="yCoords"></param>
/// <param name="isFromRightClick"></param>
private void GenerateOrUpdateInitialHetmapMatrixSingleCoord(int xCoords, int yCoords, bool isFromRightClick)
{
this.hasMatrixValuesToPlot = true;
// You can only update the Chart patter if the Game Of Life iteration is in Pause
if (this.keepIteratig == false)
{
if (this.currGenValuesToPlot != null)
{
// If the event comes from a single mouse click, then check if the current cell was already selected (has already a value of 1). If yes, set the cell to 0 (like a deselection).
if (isFromRightClick)
{
// Check if the current cell was already selected (has already a value of 1). If yes, set the cell to 0 (like a deselection).
if (this.currGenValuesToPlot[yCoords][xCoords] == 1)
this.currGenValuesToPlot[yCoords][xCoords] = 0;
}
// Otherwise, just color the selected cell.
else
this.currGenValuesToPlot[yCoords][xCoords] = 1;
AssignValuesToPlot(this.currGenValuesToPlot);
}
else
{
// If the event comes from a single mouse click, then check if the current cell was already selected (has already a value of 1). If yes, set the cell to 0 (like a deselection).
if (isFromRightClick)
{
// Check if the current cell was already selected (has already a value of 1). If yes, set the cell to 0 (like a deselection).
if (this.startingValuesToPlot[yCoords][xCoords] == 1)
this.startingValuesToPlot[yCoords][xCoords] = 0;
}
// Otherwise, just color the selected cell.
else
this.startingValuesToPlot[yCoords][xCoords] = 1;
AssignValuesToPlot(this.startingValuesToPlot);
}
}
}
/// <summary>
/// Assign the value to the selected cell. The heatmap will then convert the value to color cells.
/// </summary>
/// <param name="valueToPlot"></param>
private void AssignValuesToPlot(int[][] valueToPlot)
{
if (valueToPlot != null)
{
// Generate X and Y Axis arrays.
int[] yAxisSteps = new int[valueToPlot.Length];
int[] xAxisSteps = new int[valueToPlot[0].Length];
// Fill the Y axis steps with values.
for (int rowIdx = 0; rowIdx < valueToPlot.Length; rowIdx++)
{
yAxisSteps[rowIdx] = rowIdx + 1;
}
// Fill the X axis steps with values.
for (int colIdx = 0; colIdx < valueToPlot[0].Length; colIdx++)
{
xAxisSteps[colIdx] = colIdx + 1;
}
// Assign these values to the heatmapControl.
heatmapChartControl.XAxis = xAxisSteps;
heatmapChartControl.YAxis = yAxisSteps;
heatmapChartControl.ValuesToPlot = valueToPlot;
}
}
/// <summary>
/// Main loop which will update the heatmap cells status.
/// </summary>
/// <param name="prevGenValues"></param>
/// <returns></returns>
private int[][] GameOfLifeGenerationComputation(int[][] prevGenValues)
{
// ---- GAME OF LIFE RULES:
// The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead (or populated and unpopulated, respectively).
// Every cell interacts with its eight neighbors, which are the cells that horizontally, vertically, or diagonally adjacent. At each step in time, the following transition occur:
// 1) Any live cell with fewer than two live neighbors dies, as if by underpopulation.
// 2) Any live cell with two or three live neighbors lives on to the next generation.
// 3) Any live cell with more than three live neighbors dies, as if by overpopulation.
// 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
// Initialize the new matrix with the new Current Generation of the values to plot Matrix.
int[][] currGenValues = new int[prevGenValues.Length][];
for (int row = 0; row < currGenValues.Length; row++)
{
currGenValues[row] = new int[prevGenValues[row].Length];
}
// Loop through all the cells of the matrix and check the aforementioned conditions.
for (int currRow = 0; currRow < prevGenValues.Length; currRow++)
{
for (int currCol = 0; currCol < prevGenValues[currRow].Length; currCol++)
{
// 1 => Alive
// 0 => Dead
int currCellStatus = prevGenValues[currRow][currCol];
// Now check the status of the Neighbor Cells.
int livingNeighborCells = 0;
// Loop the neighborhood of the current cell, which is composed by 8 cells.
for (int nearRow = currRow - 1; nearRow <= currRow + 1; nearRow++)
{
for (int nearCol = currCol - 1; nearCol <= currCol + 1; nearCol++)
{
// Check if the index of the near Row or Col (neighbor cells) is going outside the original matrix. If it is, skip the cell.
if (nearRow < 0 || nearCol < 0 || nearRow >= prevGenValues.Length || nearCol >= prevGenValues[currRow].Length)
continue;
// Check as well if we are in the current cell/row/col already.
if (nearRow == currRow && nearCol == currCol)
continue;
// If the cell value is 1 means that is Alive.
if (prevGenValues[nearRow][nearCol] == 1)
livingNeighborCells += 1;
}
}
// Now check the Game Of Life Conditions.
// 1) Any live cell with fewer than two live neighbors dies, as if by underpopulation.
// 2) Any live cell with two or three live neighbors lives on to the next generation.
// 3) Any live cell with more than three live neighbors dies, as if by overpopulation.
if (currCellStatus == 1)
{
// Case 1.
if (livingNeighborCells < 2)
currGenValues[currRow][currCol] = 0;
// Case 2.
else if (livingNeighborCells == 2 || livingNeighborCells == 3)
currGenValues[currRow][currCol] = 1;
// Case 3.
else if (livingNeighborCells > 3)
currGenValues[currRow][currCol] = 0;
}
// 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
else
{
if (livingNeighborCells == 3)
currGenValues[currRow][currCol] = 1;
}
}
}
return currGenValues;
}
/// <summary>
/// Action performed when the increase (+) CellSize dimension button is clicked.
/// </summary>
private void IncreaseCellSizeMatrixDimension()
{
InitializeDimensionsOfHeatmapGrid();
// Compute the difference between the previous number of rows and columns of the existing matrix and the new number of rows and cols computed with the new dimension of the cells.
int prevNumberOfRow = this.startingValuesToPlot.Length;
int prevNumberOfCol = this.startingValuesToPlot[0].Length;
int rowsToRemove = prevNumberOfRow - this.nRowHeatmap;
int colsToRemove = prevNumberOfCol - this.nColHeatmap;
// Initialize the new matrix that will have the new dimensions (nRows, nCols) and that will be substituted to the startingValuesToPlot matrix.
int[][] newIncreasedStartingValuesToPlot = new int[this.nRowHeatmap][];
for (int row = 0; row < newIncreasedStartingValuesToPlot.Length; row++)
newIncreasedStartingValuesToPlot[row] = new int[this.nColHeatmap];
// Now we have to "copy" the previous startValuesToPlot in this new increased-in-dimension matrix.
// First compute the number of the cells that will be added from both the top/bottom and left/right margins. Therefore the indexes from and to start copying the old matrix.
int rowIdxWhereToStartCopying = (int)Math.Floor(rowsToRemove / 2.0);
int colIdxWhereToStartCopying = (int)Math.Floor(colsToRemove / 2.0);
//int rowIdxWhereToStopCopying = this.startingValuesToPlot.Length - 1 - ((int)Math.Floor(rowsToRemove / 2.0));
//int colIdxWhereToStopCopying = this.startingValuesToPlot[0].Length - 1 - ((int)Math.Floor(colsToRemove / 2.0));
int rowIdxWhereToStopCopying = this.startingValuesToPlot.Length - 1 - (rowsToRemove - (int)Math.Floor(rowsToRemove / 2.0));
int colIdxWhereToStopCopying = this.startingValuesToPlot[0].Length - 1 - (colsToRemove - (int)Math.Floor(colsToRemove / 2.0));
// Loop through the old matrix, and start copying the values in the new matrix when are included between the "zoomed" new indexes.
int newRow = 0;
for (int oldRow = 0; oldRow < this.startingValuesToPlot.Length; oldRow++)
{
int newCol = 0;
if (oldRow >= rowIdxWhereToStartCopying && oldRow <= rowIdxWhereToStopCopying)
{
for (int oldCol = 0; oldCol < this.startingValuesToPlot[oldRow].Length; oldCol++)
{
if (oldCol >= colIdxWhereToStartCopying && oldCol <= colIdxWhereToStopCopying)
{
newIncreasedStartingValuesToPlot[newRow][newCol] = this.startingValuesToPlot[oldRow][oldCol];
newCol++;
}
}
newRow++;
}
}
// Assign the new increased-in-dimension matrix to the original one.
this.startingValuesToPlot = newIncreasedStartingValuesToPlot;
// If the currGenValuesToPlot is not null, I have to change its dimensions as well.
if (this.currGenValuesToPlot != null)
{
// Initialize the dimension of the newDecreasedCurrGenValuesToPlot matrix.
int[][] newIncreasedCurrGenValuesToPlot = new int[this.nRowHeatmap][];
for (int row = 0; row < newIncreasedCurrGenValuesToPlot.Length; row++)
newIncreasedCurrGenValuesToPlot[row] = new int[this.nColHeatmap];
int newRow1 = 0;
for (int oldRow = 0; oldRow < this.currGenValuesToPlot.Length; oldRow++)
{
int newCol1 = 0;
if (oldRow >= rowIdxWhereToStartCopying && oldRow <= rowIdxWhereToStopCopying)
{
for (int oldCol = 0; oldCol < this.currGenValuesToPlot[oldRow].Length; oldCol++)
{
if (oldCol >= colIdxWhereToStartCopying && oldCol <= colIdxWhereToStopCopying)
{
newIncreasedCurrGenValuesToPlot[newRow1][newCol1] = this.currGenValuesToPlot[oldRow][oldCol];
newCol1++;
}
}
newRow1++;
}
}
this.currGenValuesToPlot = newIncreasedCurrGenValuesToPlot;
}
}
/// <summary>
/// Action performed when the Decrease (-) CellSize dimension button is clicked.
/// </summary>
private void DecreaseCellSizeMatrixDimension()
{
InitializeDimensionsOfHeatmapGrid();
if (this.nColHeatmap < 0 || this.nRowHeatmap < 0)
return;
// Compute the difference between the previous number of rows and columns of the existing matrix and the new number of rows and cols computed with the new dimension of the cells.
int prevNumberOfRow = this.startingValuesToPlot.Length;
int prevNumberOfCol = this.startingValuesToPlot[0].Length;
int rowsToAdd = this.nRowHeatmap - prevNumberOfRow;
int colsToAdd = this.nColHeatmap - prevNumberOfCol;
// Initialize the new matrix that will have the new dimensions (nRows, nCols) and that will be substituted to the startingValuesToPlot matrix.
int[][] newDecreasedStartingValuesToPlot = new int[this.nRowHeatmap][];
for (int row = 0; row < newDecreasedStartingValuesToPlot.Length; row++)
newDecreasedStartingValuesToPlot[row] = new int[this.nColHeatmap];
// Now we have to "copy" the previous startValuesToPlot in this new increased-in-dimension matrix.
// First compute the number of the cells that will be added from both the top/bottom and left/right margins. Therefore the indexes from and to start copying the old matrix.
int rowIdxWhereToStartCopying = (int)Math.Floor(rowsToAdd / 2.0);
int colIdxWhereToStartCopying = (int)Math.Floor(colsToAdd / 2.0);
//int rowIdxWhereToStopCopying = newDecreasedStartingValuesToPlot.Length - 1 - ((int)Math.Floor(rowsToAdd / 2.0));
//int colIdxWhereToStopCopying = newDecreasedStartingValuesToPlot[0].Length - 1 - ((int)Math.Floor(colsToAdd / 2.0));
int rowIdxWhereToStopCopying = newDecreasedStartingValuesToPlot.Length - 1 - (rowsToAdd - (int)Math.Floor(rowsToAdd / 2.0));
int colIdxWhereToStopCopying = newDecreasedStartingValuesToPlot[0].Length - 1 - (colsToAdd - (int)Math.Floor(colsToAdd / 2.0));
// Loop through the new matrix, and start copying the values from the old matrix when correct.
for (int newRow = 0; newRow < newDecreasedStartingValuesToPlot.Length; newRow++)
{
for (int newCol = 0; newCol < newDecreasedStartingValuesToPlot[newRow].Length; newCol++)
{
// Check if the index of the new increased matrix correspond to the old matrix, so that we can copy the old matrix in the new increased one.
if ((newRow >= rowIdxWhereToStartCopying && newRow <= rowIdxWhereToStopCopying) && (newCol >= colIdxWhereToStartCopying && newCol <= colIdxWhereToStopCopying))
{
newDecreasedStartingValuesToPlot[newRow][newCol] = this.startingValuesToPlot[newRow - rowIdxWhereToStartCopying][newCol - colIdxWhereToStartCopying];
}
}
}
// Assign the new increased-in-dimension matrix to the original one.
this.startingValuesToPlot = newDecreasedStartingValuesToPlot;
// For this, I need to change both the startingValuesToPlot and the currGenValuesToPlot, if it is not null.
if (this.currGenValuesToPlot != null)
{
int[][] newDecreasedCurrGenValuesToPlot = new int[this.nRowHeatmap][];
for (int row = 0; row < newDecreasedStartingValuesToPlot.Length; row++)
newDecreasedCurrGenValuesToPlot[row] = new int[this.nColHeatmap];
// Now we have to copy the previous CurrentGeneration ValuesToPlot in this new increased-in-dimension matrix.
// Loop through the new matrix, and start copying the values from the old matrix when correct.
for (int newRow = 0; newRow < newDecreasedCurrGenValuesToPlot.Length; newRow++)
{
for (int newCol = 0; newCol < newDecreasedCurrGenValuesToPlot[newRow].Length; newCol++)
{
// Check if the index of the new increased matrix correspond to the old matrix, so that we can copy the old matrix values in the new increased one.
if ((newRow >= rowIdxWhereToStartCopying && newRow < rowIdxWhereToStopCopying) && (newCol >= colIdxWhereToStartCopying && newCol <= colIdxWhereToStopCopying))
{
newDecreasedCurrGenValuesToPlot[newRow][newCol] = this.currGenValuesToPlot[newRow - rowIdxWhereToStartCopying][newCol - colIdxWhereToStartCopying];
}
}
}
// Assign the new increased-in-dimension matrix to the original one.
this.currGenValuesToPlot = newDecreasedCurrGenValuesToPlot;
}
}
/// <summary>
/// Method used for extracting the matrix selected cells with the Ctrl action used for the Copy/Paste operation.
/// </summary>
private void CopyPasteSelectionHeatmapCellCtrlC(int xStartIdx, int yStartIdx, int xEndIdx, int yEndIdx)
{
// Compute the number of row and number of columns of the new copyPasteCurrGenValuesToPlot.
int newNRow = 1;
if (yStartIdx > yEndIdx)
newNRow = yStartIdx - yEndIdx + 1;
else if (yEndIdx > yStartIdx)
newNRow = yEndIdx - yStartIdx + 1;
int newNCol = 1;
if (xStartIdx > xEndIdx)
newNCol = xStartIdx - xEndIdx + 1;
else if (xEndIdx > xStartIdx)
newNCol = xEndIdx - xStartIdx + 1;
// Check if the currentValuesToPlot is null. If not, select the cells.
if (this.currGenValuesToPlot != null)
{
// Initialize the Copy Paste Rows matrix, that will contain the selected cells.
this.copyPasteCurrGenValuesToPlot = new int[newNRow][];
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newRowIdx = 0;
// Loop through all the selected cells.
// In order to make them to look like selected, change the values of the cells, in which the value is equal to 0, to a value of 2. That will be then colored by the red.
// If the cell has already a value of 1, then don't change anything.
for (int row = 0; row < this.currGenValuesToPlot.Length; row++)
{
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newColIdx = 0;
// Check if the current row is in between the xStart and xEnd Idx coordinates of the copy paste selection.
if ((row <= yStartIdx && row >= yEndIdx) || (row >= yStartIdx && row <= yEndIdx))
{
// Initialize the Copy Paste Cols matrix, that will contain the selected cells.
this.copyPasteCurrGenValuesToPlot[newRowIdx] = new int[newNCol];
// Loop through all the columns of the matrix.
for (int col = 0; col < this.currGenValuesToPlot[row].Length; col++)
{
// Check if the current col is included in between yStart and yEnd coordinates of the copy paste selection.
// Inverted < and > for the col comparison with the Start and End indexes. (probably because the Y axis is upside-down).
if ((col >= xStartIdx && col <= xEndIdx) || (col <= xStartIdx && col >= xEndIdx))
{
// Copy the values of the selected rectangle.
this.copyPasteCurrGenValuesToPlot[newRowIdx][newColIdx] = this.currGenValuesToPlot[row][col];
newColIdx++;
//// If the current cell has a value of 0. Then substitute it with a value of 2.
//if (this.currGenValuesToPlot[row][col] == 0)
}
}
newRowIdx++;
}
}
//AssignValuesToPlot(this.copyPasteCurrGenValuesToPlot);
}
// Otherwise change the values of the startingValuesToPlot
else
{
// Initialize the Copy Paste Rows matrix, that will contain the selected cells.
this.copyPasteStartingValuesToPlot = new int[newNRow][];
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newRowIdx = 0;
// Loop through all the selected cells.
// In order to make them to look like selected, change the values of the cells, in which the value is equal to 0, to a value of 2. That will be then colored by the red.
// If the cell has already a value of 1, then don't change anything.
for (int row = 0; row < this.startingValuesToPlot.Length; row++)
{
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newColIdx = 0;
// Check if the current row is in between the xStart and xEnd Idx coordinates of the copy paste selection.
if ((row <= yStartIdx && row >= yEndIdx) || (row >= yStartIdx && row <= yEndIdx))
{
// Initialize the Copy Paste Cols matrix, that will contain the selected cells.
this.copyPasteStartingValuesToPlot[newRowIdx] = new int[newNCol];
// Loop through all the columns of the matrix.
for (int col = 0; col < this.startingValuesToPlot[row].Length; col++)
{
// Check if the current col is included in between yStart and yEnd coordinates of the copy paste selection.
// Inverted < and > for the col comparison with the Start and End indexes. (probably because the Y axis is upside-down).
if ((col >= xStartIdx && col <= xEndIdx) || (col <= xStartIdx && col >= xEndIdx))
{
// Copy the values of the selected rectangle.
this.copyPasteStartingValuesToPlot[newRowIdx][newColIdx] = this.startingValuesToPlot[row][col];
newColIdx++;
}
}
newRowIdx++;
}
}
//AssignValuesToPlot(this.copyPasteStartingValuesToPlot);
}
}
/// <summary>
/// Method used for the extraction of the matrix selected cells with the Ctrl action used for the Cut/Paste operation.
/// </summary>
/// <param name="xStartIdx"></param>
/// <param name="yStartIdx"></param>
/// <param name="xEndIdx"></param>
/// <param name="yEndIdx"></param>
private void CopyPasteSelectionHeatmapCellCtrlX(int xStartIdx, int yStartIdx, int xEndIdx, int yEndIdx)
{
// Compute the number of row and number of columns of the new copyPasteCurrGenValuesToPlot.
int newNRow = 1;
if (yStartIdx > yEndIdx)
newNRow = yStartIdx - yEndIdx + 1;
else if (yEndIdx > yStartIdx)
newNRow = yEndIdx - yStartIdx + 1;
int newNCol = 1;
if (xStartIdx > xEndIdx)
newNCol = xStartIdx - xEndIdx + 1;
else if (xEndIdx > xStartIdx)
newNCol = xEndIdx - xStartIdx + 1;
// Check if the currentValuesToPlot is null. If not, select the cells.
if (this.currGenValuesToPlot != null)
{
// Initialize the Copy Paste Rows matrix, that will contain the selected cells.
this.copyPasteCurrGenValuesToPlot = new int[newNRow][];
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newRowIdx = 0;
// Loop through all the selected cells.
// In order to make them to look like selected, change the values of the cells, in which the value is equal to 0, to a value of 2. That will be then colored by the red.
// If the cell has already a value of 1, then don't change anything.
for (int row = 0; row < this.currGenValuesToPlot.Length; row++)
{
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newColIdx = 0;
// Check if the current row is in between the xStart and xEnd Idx coordinates of the copy paste selection.
if ((row <= yStartIdx && row >= yEndIdx) || (row >= yStartIdx && row <= yEndIdx))
{
// Initialize the Copy Paste Cols matrix, that will contain the selected cells.
this.copyPasteCurrGenValuesToPlot[newRowIdx] = new int[newNCol];
// Loop through all the columns of the matrix.
for (int col = 0; col < this.currGenValuesToPlot[row].Length; col++)
{
// Check if the current col is included in between yStart and yEnd coordinates of the copy paste selection.
// Inverted < and > for the col comparison with the Start and End indexes. (probably because the Y axis is upside-down).
if ((col >= xStartIdx && col <= xEndIdx) || (col <= xStartIdx && col >= xEndIdx))
{
// Copy the values of the selected rectangle.
this.copyPasteCurrGenValuesToPlot[newRowIdx][newColIdx] = this.currGenValuesToPlot[row][col];
// Remove the elements selected from the currGenValuesToPlot.
this.currGenValuesToPlot[row][col] = 0;
newColIdx++;
}
}
newRowIdx++;
}
}
AssignValuesToPlot(this.currGenValuesToPlot);
}
// Otherwise change the values of the startingValuesToPlot
else
{
// Initialize the Copy Paste Rows matrix, that will contain the selected cells.
this.copyPasteStartingValuesToPlot = new int[newNRow][];
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newRowIdx = 0;
// Loop through all the selected cells.
// In order to make them to look like selected, change the values of the cells, in which the value is equal to 0, to a value of 2. That will be then colored by the red.
// If the cell has already a value of 1, then don't change anything.
for (int row = 0; row < this.startingValuesToPlot.Length; row++)
{
// Counter for the new copyPasteCurrGenValuesToPlot rows.
int newColIdx = 0;
// Check if the current row is in between the xStart and xEnd Idx coordinates of the copy paste selection.
if ((row <= yStartIdx && row >= yEndIdx) || (row >= yStartIdx && row <= yEndIdx))
{
// Initialize the Copy Paste Cols matrix, that will contain the selected cells.
this.copyPasteStartingValuesToPlot[newRowIdx] = new int[newNCol];
// Loop through all the columns of the matrix.
for (int col = 0; col < this.startingValuesToPlot[row].Length; col++)
{
// Check if the current col is included in between yStart and yEnd coordinates of the copy paste selection.