-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cpp
813 lines (813 loc) · 40.5 KB
/
Algorithms.cpp
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
# ifndef ALGORITHMS_OMID_SOJOODI
# include "Algorithms.hpp"
/**
* @brief Algorithms Constructor
* @details This Constructor Initializes the Algorithm with the Given Parameters.
*
* @param algorithm The Algorithm to Use
* @param environment The Environment to Use
* @param dataMethod The Data Method to Use
* @param processingMethod The Processing Method to Use
*
* @see ENUM_SUPPORTED_ALGORITHMS (\ref Algorithms.hpp "Algorithm" Supported by the Class)
* @see ENUM_ALGORITHM_ENVIRONMENT (\ref Graphics.hpp "Graphics" Environment)
* @see ENUM_ALGORITHMS_DATA_METHOD (\ref Algorithms.hpp "Algorithm" Data Generation Methods)
* @see ENUM_ALGORIHTMS_PROCESSING_METHODS (\ref Algorithms.hpp "Algorithm" Processing Methods)
*/
Algorithms::Algorithms(
ENUM_SUPPORTED_ALGORITHMS algorithm,
ENUM_ALGORITHM_ENVIRONMENT environment,
ENUM_ALGORITHMS_DATA_METHOD dataMethod,
ENUM_ALGORIHTMS_PROCESSING_METHODS processingMethod
) {
std::cout << MODULE ALGORITHMS_LABEL "Initialized" << std::endl;
//-- Initialize Algorithm
switch (algorithm) {
//-- Bubble Sort
case ALGORITHM_SORT_BUBBLE: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateRandomData(
MAX_RANDOM_DATA,
int(WINDOW_WIDTH * 0.1),
int(WINDOW_WIDTH * 0.9),
environment,
RANDOM_DATA_SHAPE_DELTOID,
CALCULATE_DATA_THETA_MIDDLE
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Sort with Visualization
std::vector<env::Point2D> sorted_array;
sorted_array = sorts.bubble.getSorted2D(
graphics.points2D,
BUBBLE_ASCENDING,
true,
graphics,
BUBBLE_SORT_THETA
);
//-- Starting Time
begin_time = std::chrono::high_resolution_clock::now();
//-- Sort
sorts.bubble.getSorted2D(
graphics.points2D,
BUBBLE_ASCENDING,
false,
graphics
);
//-- Ending Time
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - begin_time);
std::cout << MODULE "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
//-- Show Sorted Array
int red, green, blue;
for (int i = 0; i < sorted_array.size() - 1; i++) {
if (i % SHOW_FRAME_THRESHOLD == 0) {
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
}
//-- Handle Line Color
green = 254 * (i / double(sorted_array.size())) / 3 + 20;
blue = 254 * (i / double(sorted_array.size())) / 2 + 20;
red = 254 * (i / double(sorted_array.size())) / 5 + 20;
if (blue > 254) blue = 254; else if (blue < 0) blue = 50;
if (green > 254) green = 254; else if (green < 0) green = 50;
if (red > 254) red = 254; else if (red < 0) red = 50;
cv::line(
graphics.windows.main.matrix,
cv::Point(sorted_array[i].x, sorted_array[i].y),
cv::Point(sorted_array[i + 1].x, sorted_array[i + 1].y),
cv::Scalar(blue, green, red),
1,
cv::LINE_AA
);
}
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
break;
}
//-- Selection Sort
case ALGORITHM_SORT_SELECTION: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateRandomData(
MAX_RANDOM_DATA,
int(WINDOW_WIDTH * 0.1),
int(WINDOW_WIDTH * 0.9),
environment,
RANDOM_DATA_SHAPE_SPIRAL,
CALCULATE_DATA_THETA_MIDDLE
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Sort with Visualization
std::vector<env::Point2D> sorted_array;
sorted_array = sorts.selection.getSorted2D(
graphics.points2D,
SELECTION_ASCENDING,
true,
graphics,
SELECTION_SORT_THETA
);
//-- Starting Time
begin_time = std::chrono::high_resolution_clock::now();
//-- Sort
sorts.selection.getSorted2D(
graphics.points2D,
SELECTION_ASCENDING,
false,
graphics
);
//-- Ending Time
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - begin_time);
std::cout << MODULE "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
//-- Show Sorted Array
int red, green, blue;
for (int i = 0; i < sorted_array.size() - 1; i++) {
if (i % SHOW_FRAME_THRESHOLD == 0) {
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
}
//-- Handle Line Color
green = 254 * (i / double(sorted_array.size())) / 3 + 20;
blue = 254 * (i / double(sorted_array.size())) / 2 + 20;
red = 254 * (i / double(sorted_array.size())) / 5 + 20;
if (blue > 254) blue = 254; else if (blue < 0) blue = 50;
if (green > 254) green = 254; else if (green < 0) green = 50;
if (red > 254) red = 254; else if (red < 0) red = 50;
cv::line(
graphics.windows.main.matrix,
cv::Point(sorted_array[i].x, sorted_array[i].y),
cv::Point(sorted_array[i + 1].x, sorted_array[i + 1].y),
cv::Scalar(blue, green, red),
1,
cv::LINE_AA
);
}
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(0);
break;
}
//-- Insertion Sort
case ALGORITHM_SORT_INSERTION: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateRandomData(
MAX_RANDOM_DATA,
int(WINDOW_WIDTH * 0.1),
int(WINDOW_WIDTH * 0.9),
environment,
RANDOM_DATA_SHAPE_SPIRAL_ROAD,
CALCULATE_DATA_THETA_MIDDLE
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Sort with Visualization
std::vector<env::Point2D> sorted_array;
sorted_array = sorts.insertion.getSorted2D(
graphics.points2D,
INSERTION_ASCENDING,
true,
graphics,
INSERTION_SORT_BOTTOM_TO_TOP
);
//-- Starting Time
begin_time = std::chrono::high_resolution_clock::now();
//-- Sort
sorts.insertion.getSorted2D(
graphics.points2D,
INSERTION_ASCENDING,
false,
graphics
);
//-- Ending Time
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - begin_time);
std::cout << MODULE "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
//-- Show Sorted Array
int red, green, blue;
for (int i = 0; i < sorted_array.size() - 1; i++) {
if (i % SHOW_FRAME_THRESHOLD == 0) {
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
}
//-- Handle Line Color
green = 254 * (i / double(sorted_array.size())) / 3 + 20;
blue = 254 * (i / double(sorted_array.size())) / 2 + 20;
red = 254 * (i / double(sorted_array.size())) / 5 + 20;
if (blue > 254) blue = 254; else if (blue < 0) blue = 50;
if (green > 254) green = 254; else if (green < 0) green = 50;
if (red > 254) red = 254; else if (red < 0) red = 50;
cv::line(
graphics.windows.main.matrix,
cv::Point(sorted_array[i].x, sorted_array[i].y),
cv::Point(sorted_array[i + 1].x, sorted_array[i + 1].y),
cv::Scalar(blue, green, red),
1,
cv::LINE_AA
);
}
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(0);
break;
}
//-- Merge Sort
case ALGORITHM_SORT_MERGE: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateRandomData(
MAX_RANDOM_DATA,
int(WINDOW_WIDTH * 0.1),
int(WINDOW_WIDTH * 0.9),
environment,
RANDOM_DATA_SHAPE_SPIRAL,
CALCULATE_DATA_THETA_MIDDLE
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Sort with Visualization
std::vector<env::Point2D> sorted_array;
sorted_array = sorts.merge.getSorted2D(
graphics.points2D,
MERGE_DESCENDING,
true,
graphics,
MERGE_SORT_THETA
);
//-- Starting Time
begin_time = std::chrono::high_resolution_clock::now();
//-- Sort
sorts.merge.getSorted2D(
graphics.points2D,
MERGE_ASCENDING,
false,
graphics
);
//-- Ending Time
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - begin_time);
std::cout << MODULE "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
//-- Show Sorted Array
int red, green, blue;
for (int i = 0; i < sorted_array.size() - 1; i++) {
if (i % SHOW_FRAME_THRESHOLD == 0) {
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
}
//-- Handle Line Color
green = 254 * (i / double(sorted_array.size())) / 3 + 20;
blue = 254 * (i / double(sorted_array.size())) / 2 + 20;
red = 254 * (i / double(sorted_array.size())) / 5 + 20;
if (blue > 254) blue = 254; else if (blue < 0) blue = 50;
if (green > 254) green = 254; else if (green < 0) green = 50;
if (red > 254) red = 254; else if (red < 0) red = 50;
cv::line(
graphics.windows.main.matrix,
cv::Point(sorted_array[i].x, sorted_array[i].y),
cv::Point(sorted_array[i + 1].x, sorted_array[i + 1].y),
cv::Scalar(blue, green, red),
1,
cv::LINE_AA
);
}
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(0);
break;
}
//-- Quick Sort
case ALGORITHM_SORT_QUICK: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateRandomData(
MAX_RANDOM_DATA,
int(WINDOW_WIDTH * 0.1),
int(WINDOW_WIDTH * 0.9),
environment,
RANDOM_DATA_SHAPE_SPIRAL,
CALCULATE_DATA_THETA_MIDDLE
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Sort with Visualization
std::vector<env::Point2D> sorted_array;
sorted_array = sorts.quick.getSorted2D(
graphics.points2D,
QUICK_ASCENDING,
true,
graphics,
QUICK_SORT_LEFT_TO_RIGHT
);
//-- Starting Time
begin_time = std::chrono::high_resolution_clock::now();
//-- Sort
sorts.quick.getSorted2D(
graphics.points2D,
QUICK_ASCENDING,
false,
graphics,
QUICK_SORT_THETA
);
//-- Ending Time
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - begin_time);
std::cout << MODULE "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
//-- Show Sorted Array
int red, green, blue;
for (int i = 0; i < sorted_array.size() - 1; i++) {
if (i % SHOW_FRAME_THRESHOLD == 0) {
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(1);
}
//-- Handle Line Color
green = 254 * (i / double(sorted_array.size())) / 3 + 20;
blue = 254 * (i / double(sorted_array.size())) / 2 + 20;
red = 254 * (i / double(sorted_array.size())) / 5 + 20;
if (blue > 254) blue = 254; else if (blue < 0) blue = 50;
if (green > 254) green = 254; else if (green < 0) green = 50;
if (red > 254) red = 254; else if (red < 0) red = 50;
cv::line(
graphics.windows.main.matrix,
cv::Point(sorted_array[i].x, sorted_array[i].y),
cv::Point(sorted_array[i + 1].x, sorted_array[i + 1].y),
cv::Scalar(blue, green, red),
1,
cv::LINE_AA
);
}
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
cv::waitKey(0);
break;
}
//-- N Queens
case ALGORITHM_N_QUEENS: {
//-- Generate Random Data
if (dataMethod == USE_RANDOM_DATA) {
generateChessBoardData(
CHESS_BOARD_SIZE,
SHOW_BOX_CHESS_WEIGHTED,
ENVIRONMENT_2D
);
} else if (dataMethod == USE_INPUT_FILE) {
std::cout << TAB ERROR "Loading Data from Input File Method Not Yet Implemented" << std::endl;
} else {
std::cout << TAB ERROR "Data Method Not Supported" << std::endl;
}
//-- Solve N Queens Problem
nqueens.getSolved(
graphics.boxes2D,
graphics
);
//-- Show Solutions on Chess Board
cv::Mat image;
cv::Mat image_black = cv::imread("../NQueens/assets/2.png", cv::IMREAD_COLOR);
cv::Mat image_white = cv::imread("../NQueens/assets/1.png", cv::IMREAD_COLOR);
for (int index = 0; index < nqueens.solutions.size(); ++index) {
graphics.windows.main.matrix.copyTo(graphics.windows.temp1.matrix);
std::cout << MODULE NQUEENS_LABEL "Solution " << index + 1 << ":" << std::endl;
for (int i = 0; i < nqueens.solutions[index].size(); ++i) {
for (int j = 0; j < nqueens.solutions[index].size(); ++j) {
//-- Handle Image Color
if (i % 2 == 0) {
if (j % 2 == 0) {
image = image_white;
} else {
image = image_black;
}
} else {
if (j % 2 == 0) {
image = image_black;
} else {
image = image_white;
}
}
if (nqueens.solutions[index][i] == j) {
std::cout << TERMINAL_BACKGROUND TERMINAL_QUEEN " \033[0m";
//-- Draw Rectangle on graphics.box2D (Chess Board) Where Queen is Placed
// cv::rectangle(
// graphics.windows.temp1.matrix,
// cv::Point(
// graphics.boxes2D[i][j].p1.x,
// graphics.boxes2D[i][j].p1.y
// ),
// cv::Point(
// graphics.boxes2D[i][j].p2.x,
// graphics.boxes2D[i][j].p2.y
// ),
// cv::Scalar(0, 255, 255),
// -1,
// cv::LINE_AA
// );
//-- Draw Queen on graphics.box2D (Chess Board)
graphics.drawImage(
cv::Point(
graphics.boxes2D[i][j].p1.x + 5,
graphics.boxes2D[i][j].p1.y + 5
),
cv::Point(
graphics.boxes2D[i][j].p2.x - 5,
graphics.boxes2D[i][j].p2.y - 5
),
// cv::imread("../NQueens/assets/black.png", cv::IMREAD_COLOR)
image
);
//-- Show Chess Board
cv::imshow(WINDOW_NAME, graphics.windows.temp1.matrix);
cv::waitKey(73);
}
else {
std::cout << TERMINAL_BACKGROUND TERMINAL_EMPTY " \033[0m";
}
}
std::cout << std::endl;
}
cv::waitKey(0);
}
break;
}
}
}
/**
* @brief Algorithms Destructor
* @details This Destructor Finalizes the Algorithm and Shows the Processing Time.
*/
Algorithms::~Algorithms() {
end_time = std::chrono::high_resolution_clock::now();
algorithm_duration = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - begin_time);
std::cout << LOG "Algorithm Process Time: " << algorithm_duration.count() << "s" << RESET << std::endl;
}
/**
* @brief Generate Random Data
* @details This Method Generates Random Data for the Algorithm.
*
* @param amount The Amount of Data to Generate
* @param min The Minimum Value of Data
* @param max The Maximum Value of Data
*/
void Algorithms::generateRandomData(
int amount,
int min,
int max,
ENUM_ALGORITHM_ENVIRONMENT environment,
ENUM_RANDOM_DATA_SHAPES shape,
ENUM_CALCULATE_DATA_THETA calc_theta
) {
// std::cout << LOG "Generating Random Data ..." << RESET << std::endl;
//-- Generate Random Data According to Algorithm Environment
if (environment == ENVIRONMENT_1D) {
std::cout << TAB ERROR "Generating Data for 1D Environment has Not Yet been Implemented" << std::endl;
} else if (environment == ENVIRONMENT_2D) {
//-- Generate Random Data on 2D Environment
graphics.points2D.clear();
bool show_flag;
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> dist_r(min, max);
std::uniform_real_distribution<> dist_theta;
std::uniform_real_distribution<> dist_offset(-37, 37);
if (shape == RANDOM_DATA_SHAPE_SPIRAL or shape == RANDOM_DATA_SHAPE_SPIRAL_ROAD or shape == RANDOM_DATA_SHAPE_SATURN or shape == RANDOM_DATA_SHAPE_qb) {
dist_theta = std::uniform_real_distribution<>(0, 13.5 * M_PI);
} else if (shape == RANDOM_DATA_SHAPE_qb) {
dist_theta = std::uniform_real_distribution<>(0, 37.5 * M_PI);
} else {
dist_theta = std::uniform_real_distribution<>(0, 2 * M_PI);
}
//-- Generate Center Dense Random Points
int x, y;
bool calc_flag = true;
for (int i = 0; i < amount; i++) {
if (shape == RANDOM_DATA_SHAPE_SPIRAL) {
double offset = dist_offset(gen);
const double a = 5;
const double b = 0.12;
double theta = dist_theta(gen);
double r = a * std::exp(b * theta) + offset;
x = static_cast<int>(WINDOW_WIDTH / 2 - r * std::cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * std::sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_SPIRAL_ROAD) {
const double a = 5;
const double b = 0.1;
double theta = dist_theta(gen);
double r = a * std::exp(b * theta);
x = static_cast<int>(WINDOW_WIDTH / 2 + r * std::cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * std::atan(theta));
} else if (shape == RANDOM_DATA_SHAPE_SATURN) {
const double a = 5;
const double b = 0.1;
double theta = dist_theta(gen);
double r = a * std::exp(b * theta);
x = static_cast<int>(WINDOW_WIDTH / 2 + r * std::cos(theta) + 250 * sin(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * std::sin(theta)) + 250 * cos(theta);
} else if (shape == RANDOM_DATA_SHAPE_qb) {
const double a = 3.7;
const double b = 0.037;
double theta = dist_theta(gen);
double r = a * std::exp(b * theta);
x = static_cast<int>(WINDOW_WIDTH / 2 - r * std::cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * std::sin(theta)) + 37 / cos(theta);
} else {
double r = std::abs(dist_r(gen)) / 7;
double theta = dist_theta(gen);
//-- Generate Data According to Shape
if (shape == RANDOM_DATA_SHAPE_LINE) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * sin(theta) * cos(theta));
y = static_cast<int>(WINDOW_WIDTH / 2 + r * sin(theta) * cos(theta));
} else if (shape == RANDOM_DATA_SHAPE_CLUSTER) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_LISSAJOUS_CURVE) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * sin(3 * theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(2 * theta));
} else if (shape == RANDOM_DATA_SHAPE_NAUTILUS) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * theta * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * theta * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_HYPOTROCHOID) {
x = static_cast<int>(WINDOW_WIDTH / 2 + (r - 20) * cos(theta) + 20 * cos((r / 20 - 1) * theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + (r - 20) * sin(theta) - 20 * sin((r / 20 - 1) * theta));
} else if (shape == RANDOM_DATA_SHAPE_ROSE_3) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(3 * theta) * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * cos(3 * theta) * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_ROSE_4) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(2 * theta) * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * cos(2 * theta) * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_ROSE_5) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(5 * theta) * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * cos(5 * theta) * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_ROSE_8) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(4 * theta) * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * cos(4 * theta) * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_LEMNISCATE_OF_BERNOULLI) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta) / (1 + sin(theta) * sin(theta)));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(theta) * cos(theta) / (1 + sin(theta) * sin(theta)));
} else if (shape == RANDOM_DATA_SHAPE_ELLIPSE) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + 0.5 * r * sin(theta));
} else if (shape == RANDOM_DATA_SHAPE_ASTROID) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * pow(cos(theta), 3));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * pow(sin(theta), 3));
} else if (shape == RANDOM_DATA_SHAPE_DELTOID) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * (2 * cos(theta) + cos(2 * theta)));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * (2 * sin(theta) - sin(2 * theta)));
} else if (shape == RANDOM_DATA_SHAPE_STAR_1) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta) + 20 * cos(10 * theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(theta) + 20 * sin(10 * theta));
} else if (shape == RANDOM_DATA_SHAPE_STAR_2) {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta) + 50 * cos(13 * theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(theta) + 50 * sin(13 * theta));
} else {
x = static_cast<int>(WINDOW_WIDTH / 2 + r * cos(theta));
y = static_cast<int>(WINDOW_HEIGHT / 2 + r * sin(theta));
}
}
//-- Handle Show Flag Threshold
if (i % SHOW_FRAME_THRESHOLD == 0) {
show_flag = true;
} else {
show_flag = false;
}
//-- Calculate Theta
double theta;
if (calc_theta == CALCULATE_DATA_THETA_MIDDLE) {
theta = std::atan2(y - WINDOW_HEIGHT / 2, x - WINDOW_WIDTH / 2);
} else if (calc_theta == CALCULATE_DATA_THETA_CORNER) {
theta = std::atan2(y, x);
} else {
theta = 0;
calc_flag = false;
}
//-- Draw Point
if (x >= min && x < max && y >= min && y < max) {
graphics.drawPoint(
env::Point2D(
x,
y,
theta,
POINT_COLOR
),
true,
show_flag,
SHOW_POINT_NORMAL,
SHOW_ON_MAIN_WINDOW,
"None"
);
}
}
//-- Check if Calculating Theta Flag is True
if (calc_flag == false) {
//-- Find Base Index
if (calc_theta == CALCULATE_DATA_THETA_HIGHEST) {
//-- Find Highest Point
int index = 0;
int highest_point = 0;
for (int i = 0; i < graphics.points2D.size(); i++) {
if (graphics.points2D[i].y < graphics.points2D[highest_point].y) {
//-- Show Line from Current Point to Last Highest Point
if (index > 0) {
graphics.drawLine(
graphics.points2D[highest_point],
graphics.points2D[i],
std::to_string(index),
cv::Scalar(126, 128, 88),
LINE_TICKNESS,
false,
true,
SHOW_LINE_WEIGHTED,
SHOW_ON_TEMP_WINDOW
);
}
//-- Show Point Itself
graphics.drawPoint(
graphics.points2D[i],
false,
true,
SHOW_POINT_RING,
SHOW_ON_TEMP_WINDOW,
"None"
);
//-- Update Highest Point
highest_point = i;
base_index = highest_point;
//-- Increase Index
index++;
//-- Show Window
cv::imshow(WINDOW_NAME, graphics.windows.temp1.matrix);
cv::waitKey(SHOW_FRAME_THRESHOLD);
}
}
} else if (calc_theta == CALCULATE_DATA_THETA_LOWEST) {
//-- Find Lowest Point
int index = 0;
int lowest_point = 0;
for (int i = 0; i < graphics.points2D.size(); i++) {
if (graphics.points2D[i].y > graphics.points2D[lowest_point].y) {
//-- Show Line from Current Point to Last Lowest Point
if (index > 0) {
graphics.drawLine(
graphics.points2D[lowest_point],
graphics.points2D[i],
std::to_string(index),
cv::Scalar(126, 128, 88),
LINE_TICKNESS,
false,
true,
SHOW_LINE_WEIGHTED,
SHOW_ON_TEMP_WINDOW
);
}
//-- Show Point Itself
graphics.drawPoint(
graphics.points2D[i],
false,
true,
SHOW_POINT_RING,
SHOW_ON_TEMP_WINDOW,
"None"
);
//-- Update Lowest Point
lowest_point = i;
base_index = lowest_point;
//-- Increase Index
index++;
//-- Show Window
cv::imshow(WINDOW_NAME, graphics.windows.temp1.matrix);
cv::waitKey(SHOW_FRAME_THRESHOLD);
}
}
} else {
std::cout << TAB ERROR "Theta Calculation Method Not Supported" << std::endl;
}
//-- Show Base Point
// graphics.drawPoint(
// graphics.points2D[base_index],
// false,
// true,
// SHOW_POINT_INFO_BOX,
// SHOW_ON_TEMP_WINDOW,
// "Base"
// );
//-- Calculate each Point's Theta According to Base Point
for (int i = 0; i < graphics.points2D.size(); i++) {
graphics.points2D[i].theta = atan2(
graphics.points2D[i].y - graphics.points2D[base_index].y,
graphics.points2D[i].x - graphics.points2D[base_index].x
) * 180 / M_PI;
//-- Show Point Itself with Frame Threshold
// if (i % (SHOW_FRAME_THRESHOLD / 2) == 0) {
// graphics.drawPoint(
// graphics.points2D[i],
// false,
// true,
// SHOW_POINT_INFO_BOX,
// SHOW_ON_TEMP_WINDOW_RESET,
// std::to_string(graphics.points2D[i].theta)
// );
// }
}
std::cout << SUCCESS "All Points Theta Calculated" << RESET << std::endl;
}
std::cout << SUCCESS "Random Data Generated" << RESET << std::endl;
//-- Show Graphics
cv::waitKey(0);
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
} else if (environment == ENVIRONMENT_3D) {
std::cout << TAB ERROR "Generating Data for 3D Environment has Not Yet been Implemented" << std::endl;
} else {
std::cout << ERROR "Environment Not Supported" << std::endl;
}
}
/**
* @brief Generate Chess Board Data
* @details This Method Generates Chess Board Data for the Algorithm.
*
* @param size The Size of the Chess Board
* @param method The Method to Show the Box
* @param environment The Environment to Generate Data
*/
void Algorithms::generateChessBoardData(
int size,
ENUM_SHOW_BOX_METHODS method,
ENUM_ALGORITHM_ENVIRONMENT environment
) {
//-- Generate Chess Board Data According to Algorithm Environment
if (environment == ENVIRONMENT_1D) {
std::cout << TAB ERROR "Generating Data for 1D Environment has Not Yet been Implemented" << std::endl;
} else if (environment == ENVIRONMENT_2D) {
//-- Extract Box Length According to Size and Window Width
int box_length = WINDOW_WIDTH / size;
//-- Generate Chess Board Data on 2D Environment
cv::Scalar color, text_color;
for (int i = 0; i < size; i++) {
std::vector<env::Box2D> row;
for (int j = 0; j < size; j++) {
//-- Handle Color
if ((i + j) % 2 == 0) {
color = CHESS_BOARD_WHITE_COLOR;
text_color = CHESS_BOARD_BLACK_COLOR;
} else {
color = CHESS_BOARD_BLACK_COLOR;
text_color = CHESS_BOARD_WHITE_COLOR;
}
//-- Generate Point 1
env::Point2D point1 = env::Point2D(
j * box_length, // Correct order for X coordinate
i * box_length, // Correct order for Y coordinate
0,
color
);
//-- Generate Point 2
env::Point2D point2 = env::Point2D(
j * box_length + box_length,
i * box_length + box_length,
0,
color
);
//-- Generate Chess Home Name
std::string home_name = "";
if (method == SHOW_BOX_CHESS_WEIGHTED || method == SHOW_BOX_NORMAL_WEIGHTED || method == SHOW_BOX_CHESS_COLORFUL_WEIGHTED) {
home_name = std::string(1, char(65 + j)) + std::to_string(i + 1);
} else {
home_name = "None";
}
//-- Draw Box
graphics.drawBox(
i,
env::Box2D(
point1,
point2
),
home_name,
text_color,
color,
true
);
//-- Add Box to Row
row.push_back(env::Box2D(point1, point2));
}
graphics.boxes2D.push_back(row);
}
std::cout << SUCCESS "Chess Board Data has been Generated" << RESET << std::endl;
//-- Show Graphics
cv::waitKey(0);
cv::imshow(WINDOW_NAME, graphics.windows.main.matrix);
} else if (environment == ENVIRONMENT_3D) {
std::cout << TAB ERROR "Generating Data for 3D Environment has Not Yet been Implemented" << std::endl;
} else {
std::cout << ERROR "Environment Not Supported" << std::endl;
}
}
# endif // ALGORITHMS_OMID_SOJOODI