-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1292 lines (986 loc) · 43.1 KB
/
script.js
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
/* ------------------------------------------------------------------------------------
Author : Romain Besson
Date : 22/10/2021
Published : 27/10/2021
------------------------------------------------------------------------------------ */
// _G Variables //
let options = {
canRotate: false,
width: 0,
randomNumber: 1, // random Number option for random generator
}
let outputFields = {}
let inProgress = false
let g_Holes = []
let g_Rectangles = []
const INFINITY = 10**10
// ---------------- //
// _G Elements //
let main = document.querySelector("main");
let state = document.getElementById("status");
let canvas = document.getElementById("output-canvas");
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
let canvasCTX = canvas.getContext('2d');
let gridWidth = document.getElementById("width-value");
let gridHeight = document.getElementById("height-value");
let outputBox = document.getElementById("output-box");
let outputRectanglesBox = document.getElementById("output-boxes");
let outputRectangles = document.getElementById("outputRectangles");
let coordinateBox = document.getElementById('box-coordinates');
// --------------- //
// Classes //
class Rectangle {
constructor(x1, y1, x2, y2, id, origin) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.id = id;
this.origin = origin;
}
}
class Hole {
constructor(x1, y1, x2, y2, id, origin) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.id = id;
this.origin = origin;
}
}
// ------- //
// Output Info //
let info = Array.from(document.getElementsByClassName('output-info'))
info.forEach(element => {
outputFields[element.attributes.name.value] = element
})
// ----------- //
// Toggle Input //
let toggles = Array.from(document.getElementsByClassName('switch'))
toggles.forEach(element => {
element.children[0].addEventListener("click", () => {
const option = element.attributes.option.value
options[option] = !options[option]
})
})
// ------------ //
// Text-Box Input //
let textBoxes = Array.from(document.getElementsByClassName('input-box'))
textBoxes.forEach(element => {
element.addEventListener("input", () => {
const option = element.attributes.option.value
if (element.value) {
element.value = clamp(parseInt(element.value), parseInt(element.attributes.min.value), parseInt(element.attributes.max.value))
options[option] = parseInt(capNumber(element.value, 0))
} else options[option] = 0
})
})
// -------------- //
function randomizeCoords() {
let coords = ""
for (let i = 0; i < options.randomNumber; i++) {
coords = `${coords + Random(99)}, ${Random(99)}`
if (i !== options.randomNumber - 1) coords = `${coords}\n`
}
coordinateBox.value = coords
}
// Main Function(s) //
function fitIn(shape1, shape2) {
// Check if shape1 fits in shape2
// ONLY by comparing their Widths and Heights AND
// Not their actual 2D positions
const shape1W = (shape1.x2 - shape1.x1)
const shape1H = (shape1.y2 - shape1.y1)
const shape2W = (shape2.x2 - shape2.x1)
const shape2H = (shape2.y2 - shape2.y1)
if (
shape1W < shape2W && // shape1's width is smaller than shape2's width AND
shape1H < shape2H // shape1's height is smaller than shape2's height
) {
return true // shape1 is smaller than shape2 (fits in by size)
}
return false // shape1 is bigger or equal to shape2
}
function _fitIn(shape1, shape2) {
// Check if shape1 fits in shape2
// By comparing all of the corners of shape1
if (
shape1.x1 >= shape2.x1 && // Top Left Corner of shape1 is inside shape2
shape1.y1 >= shape2.y1 && //
shape1.x2 <= shape2.x2 && // Bottom Right of shape1 is inside shape2
shape1.y2 <= shape2.y2 // Therefore, a boundary is formed for shape1 only using 2 corners of shape2
) {
return true // shape1 is inside shape2
}
return false // shape1 is not inside shape2
}
function holeCovered(hole, exception, holes) {
// Iterates through all the holes (Array Pointer) and checks if it is covered by a hole AND
// We also pass an argument 'exception' if we are for example: Breaking a Hole into new Holes
for (let hid = 0; hid < holes.length; hid++) { // Check through all holes if hole1 is inside/covered (by) it
let hole2 = holes[hid]
if (hole2 !== exception && _fitIn(hole, hole2)) return true // hole1 acts as rectangle in this case
}
return false
}
function rotateRectangle(rectangle) { // Returns the inverse of specified rectangle in W&H
let newRectangle = new Rectangle( // We make a copy of rectangle to make sure we don't
rectangle.x1, // change the original rectangle (Pointer) passed as an argument
rectangle.y1,
rectangle.x2,
rectangle.y2,
rectangle.id,
rectangle.origin
)
const x2 = newRectangle.x2
const y2 = newRectangle.y2
newRectangle.x2 = y2
newRectangle.y2 = x2
return newRectangle
}
function removeHole(hole, holes) { // Removes a hole from specified Array (Pointer)
let newHoles = []
for (let hid = 0; hid < holes.length; hid++) {
if (holes[hid] !== hole) {
newHoles.push(holes[hid])
}
}
holes = newHoles
}
function addNewHole(newHole, holes) {
newHole.id = holes.length + 1
holes[holes.length] = newHole
}
function calculateHeight(rectangle, hole) {
return hole.y1 + (rectangle.y2 - rectangle.y1)
}
function createNewHoles(rectangle, hole, holes) {
// Create new holes from a Placement / Overlapping
// ⬛ => Hole
// ⬜ => Rectangle
// So we enumerate all the 15 cases where the Rectangle can clip inside the Hole
let caseMet = false
// ⬜⬜⬜
// ⬜⬜⬜
// ⬜⬜⬜
// [CASE 1]: Perfect fit!
if (
!caseMet &&
rectangle.x1 === hole.x1 && // Rectangle's Left Vertical Edge is on the hole's Left Vertical Edge
rectangle.y1 === hole.y1 && // Rectangle's Top Horizontal Edge is on the hole's Top Horizontal Edge
rectangle.x2 === hole.x2 && // Rectangle's Right Vertical Edge is on the hole's Right Vertical Edge
rectangle.y2 === hole.y2 // Rectangle's Bottom Horizontal Edge is on the hole's Bottom Horizontal Edge
) {
caseMet = true // Only case where we add 0 Holes
}
//
// ⬜⬜⬜
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 2]: Top Bar Horizontal
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge passes through the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 2) // We set Id of hole to 0 since we change it in the addNewHole func.
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) } // 2 is the origin case for this hole therefore: [CASE 2]
}
//
// ⬛⬛⬜
// ⬛⬛⬜
// ⬛⬛⬜
// [CASE 3]: Right Bar Vertical
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 3)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
}
//
// ⬛⬛⬛
// ⬛⬛⬛
// ⬜⬜⬜
// [CASE 4]: Bottom Bar Horizontal
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 4)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
}
//
// ⬜⬛⬛
// ⬜⬛⬛
// ⬜⬛⬛
// [CASE 5]: Left Bar Vertical
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 5)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
}
//
// ⬜⬛⬛
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 6]: Top Left Corner
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge passes through the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 6)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 6)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
//
// ⬛⬛⬜
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 7]: Top Right Corner
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge Crosses in the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.x2 >= hole.x2 && //
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge passes through the Hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 7)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 7)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
//
// ⬛⬛⬛
// ⬛⬛⬛
// ⬛⬛⬜
// [CASE 8]: Bottom Right Corner
if (
!caseMet &&
rectangle.x1 < hole.x2 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 > hole.x1 && //
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 8)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 8)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
// ⬛⬛⬛
// ⬛⬛⬛
// ⬜⬛⬛
// [CASE 9]: Bottom Left Corner
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge passes through the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 9)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 9)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
// ⬛⬜⬛
// ⬛⬜⬛
// ⬛⬜⬛
// [CASE 10]: Middle Vertical Bar
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 10)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 10)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
// ⬛⬛⬛
// ⬜⬜⬜
// ⬛⬛⬛
// [CASE 11]: Middle Horizontal Bar
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge if right to the hole AND
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge is below the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 11)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 11)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
}
// ⬛⬜⬛
// ⬛⬛⬛
// ⬛⬛⬛
// [CASE 12]: Top Rectangle Pop-Out
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 <= hole.y1 && // Rectangle's Top Horizontal Edge is above the hole AND
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge pases through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge passes through the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 12)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 12)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
let newHole3 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 12)
if (!holeCovered(newHole3, hole, holes)) { addNewHole(newHole3, holes) }
}
// ⬛⬛⬛
// ⬛⬛⬜
// ⬛⬛⬛
// [CASE 13]: Right Rectangle Pop-Out
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 >= hole.x2 && // Rectangle's Right Vertical Edge is right to the hole AND
rectangle.y2 > hole.y1 && // Rectangle's Bottom Horizontal Edge is below the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 13)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 13)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
let newHole3 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 13)
if (!holeCovered(newHole3, hole, holes)) { addNewHole(newHole3, holes) }
}
// ⬛⬛⬛
// ⬛⬛⬛
// ⬛⬜⬛
// [CASE 14]: Bottom Rectangle Pop-Out
if (
!caseMet &&
rectangle.x1 > hole.x1 && // Rectangle's Left Vertical Edge passes through the hole AND
rectangle.x1 < hole.x2 && //
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 > hole.x1 && // Rectangle's Right Vertical Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 >= hole.y2 // Rectangle's Bottom Horizontal Edge is below the hole
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, rectangle.x1, hole.y2, 0, 14)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 14)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
let newHole3 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 14)
if (!holeCovered(newHole3, hole, holes)) { addNewHole(newHole3, holes) }
}
// ⬛⬛⬛
// ⬜⬛⬛
// ⬛⬛⬛
// [CASE 15]: Left Rectangle Pop-Out
if (
!caseMet &&
rectangle.x1 <= hole.x1 && // Rectangle's Left Vertical Edge is left to the hole AND
rectangle.y1 > hole.y1 && // Rectangle's Top Horizontal Edge passes through the hole AND
rectangle.y1 < hole.y2 && //
rectangle.x2 > hole.x1 && // Rectangle's Bottom Horizontal Edge passes through the hole AND
rectangle.x2 < hole.x2 && //
rectangle.y2 > hole.y1 && // Rectangle's Right Vertical Edge passes through the hole
rectangle.y2 < hole.y2 //
) {
caseMet = true
let newHole1 = new Hole(hole.x1, hole.y1, hole.x2, rectangle.y1, 0, 15)
if (!holeCovered(newHole1, hole, holes)) { addNewHole(newHole1, holes) }
let newHole2 = new Hole(rectangle.x2, hole.y1, hole.x2, hole.y2, 0, 15)
if (!holeCovered(newHole2, hole, holes)) { addNewHole(newHole2, holes) }
let newHole3 = new Hole(hole.x1, rectangle.y2, hole.x2, hole.y2, 0, 15)
if (!holeCovered(newHole3, hole, holes)) { addNewHole(newHole3, holes) }
}
// Delete previous completed Hole //
if (caseMet) {
removeHole(hole, holes) // Remove Hole since we broke it into multiple holes
g_Holes = sortByHeight(g_Holes) // Re-sort new holes (favor holes that are at the top because they have the least risk to increase canvas Height)
return true
}
return false // We return false (SHOULD never happen) signifying we failed to place Rectangle in Hole
}
function overlap(rectangle, hole) {
// Check corners //
// Check top left corner
if (
(rectangle.x1 > hole.x1) &&
(rectangle.x1 < hole.x2) &&
(rectangle.y1 > hole.y1) &&
(rectangle.y1 < hole.y2)
) {
return true
}
// Check top right corner
if (
(rectangle.x2 > hole.x1) &&
(rectangle.x2 < hole.x2) &&
(rectangle.y1 > hole.y1) &&
(rectangle.y1 < hole.y2)
) {
return true
}
// Check bottom left corner
if (
(rectangle.x1 > hole.x1) &&
(rectangle.x1 < hole.x2) &&
(rectangle.y2 > hole.y1) &&
(rectangle.y2 < hole.y2)
) {
return true
}
// Check bottom right corner
if (
(rectangle.x2 > hole.x1) &&
(rectangle.x2 < hole.x2) &&
(rectangle.y2 > hole.y1) &&
(rectangle.y2 < hole.y2)
) {
return true
}
// ------------- //
// Check Edges don't overlap //
// Check if the top horizontal edge of the rectangle passes through the hole
if (
(rectangle.x1 < hole.x2) &&
(rectangle.x2 > hole.x1) &&
(rectangle.y1 >= hole.y1) &&
(rectangle.y1 < hole.y2)
) {
return true
}
// Check if the bottom horizontal edge of the rectangle passes through the hole
if (
(rectangle.x1 < hole.x2) &&
(rectangle.x2 > hole.x1) &&
(rectangle.y2 <= hole.y2) &&
(rectangle.y2 > hole.y1)
) {
return true
}
// Check if the left vertical edge of the rectangle passes through the hole
if (
(rectangle.y1 < hole.y2) &&
(rectangle.y2 > hole.y1) &&
(rectangle.x1 < hole.x2) &&
(rectangle.x1 >= hole.x1)
) {
return true
}
// Check if the right vertical edge of the rectangle passes through the hole
if (
(rectangle.y1 < hole.y2) &&
(rectangle.y2 > hole.y1) &&
(rectangle.x2 <= hole.x2) &&
(rectangle.x2 > hole.x1)
) {
return true
}
// ------------------------- //
return false // No overlap detected
}
function getBestHole(rectangle, method, canRotate) {
let bestHole = null
let doRotation = false
if (method === 1) { // [METHOD 1]: Choose hole that makes canvas height the smallest
let lowestHeight = Infinity
for (let hid = 0; hid < g_Holes.length; hid++) {
// Check Normal Rotation //
const normalFits = fitIn(rectangle, g_Holes[hid]) // Check that the Normal Rectangle Orientation fits in the Hole
const normalH = calculateHeight(rectangle, g_Holes[hid]) // Get the Height increase for the Rectangle based on Hole's location
if (normalFits && normalH < lowestHeight) { // Check that Height increase is worthy of this Hole
lowestHeight = normalH
bestHole = g_Holes[hid]
doRotation = false
}
// --------------------- //
// Check Rotated Solution //
const rotationFits = fitIn(rotateRectangle(rectangle), g_Holes[hid]) // Check that the Rotated Rectangle fits in the Hole (IMPORTANT that rotateRectangle() doesn't rotate the actual Rectangle arg (Pointer))
const rotationH = calculateHeight(rotateRectangle(rectangle), g_Holes[hid]) // Get the Height increase for the Rotated Rectangle based on Hole's location
if (canRotate && rotationFits && rotationH < lowestHeight) { // Check that Rotated Height is worthy of the rectangle being rotated
lowestHeight = rotationH
bestHole = g_Holes[hid]
doRotation = true
}
// ---------------------- //
}
} else if (method === 2) { // [METHOD 2]: Choose Highest hole in Y position
let highestY = Infinity
for (let hid = 0; hid < g_Holes.length; hid++) {
if (g_Holes[hid].y1 < highestY) {
if (!fitIn(rectangle, g_Holes[hid])) {
if (canRotate && fitIn(rotateRectangle(rectangle), g_Holes[hid])) {
doRotation = true
highestY = g_Holes[hid].y1
bestHole = g_Holes[hid]
}
} else {
if (canRotate && fitIn(rotateRectangle(rectangle), g_Holes[hid]) && rotateRectangle(rectangle).y2 < rectangle.y2) {
doRotation = true
}
highestY = g_Holes[hid].y1
bestHole = g_Holes[hid]
}
}
}
} else if (method === 3) { // [METHOD 3]: Choose the Hole that matches the most with the Rectangle (least difference in W&H)
const rectA = (rectangle.x2 - rectangle.x1) * (INFINITY-rectangle.y1)
let lowestADiff = Infinity
for (let hid = 0; hid < g_Holes.length; hid++) {
const holeA = (g_Holes[hid].x2 - g_Holes[hid].x1) * (INFINITY-g_Holes[hid].y1)
if (Math.abs(holeA - rectA) < lowestADiff) {
lowestADiff = Math.abs(holeA - rectA)
bestHole = g_Holes[hid]
}
}
}
return { bestHole: bestHole, doRotation: doRotation }
}
function Main() {
if (!inProgress) {
clearCanvas() // Reset Canvas of Rectangles & Text
inProgress = true // Stop user from Stopping Current Sort or Starting a New Sort, etc ...
// Indicate USER that we have started Main //
state.attributes.style.value = "color:rgb(0, 255, 0)"
state.innerHTML = "Working"
// -------------------------------------------- //
// Check Width //
if (options.width === 0) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = "Failed: Width is 0"
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
//
// Check Format + Format Box Coords //
let boxes = processCoords(coordinateBox.value)
if (!boxes.success) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = "Failed: Wrong Format"
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
// -------------------------------- //
// Current Sort/Session Info //
let sessionInfo = {
CanRotate: options.canRotate,
Width: options.width,
StartTime: 0,
EndTime: 0,
RunTime: 0
}
// ---------------- //
// Set Global Arrays //
g_Rectangles = sortByArea(boxes.coords) // Iterate over the biggest rectangles first
g_Holes = [new Hole(0, 0, sessionInfo.Width, INFINITY, 1, 0)] // First hole the size of the Infinity with origin Zero as break Case Origin
// ----------------- //
// Check if Solve is Possible //
for (let rid = 0; rid < g_Rectangles.length; rid++) {
if (g_Rectangles[rid].x2 - g_Rectangles[rid].x1 > 1000 || g_Rectangles[rid].y2 - g_Rectangles[rid].y1 > 1000) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = `Failed: Box n°${g_Rectangles[rid].id} is too big.`
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
if (g_Rectangles[rid].x2 - g_Rectangles[rid].x1 > sessionInfo.Width && g_Rectangles[rid].y2 - g_Rectangles[rid].y1 > sessionInfo.Width || (!sessionInfo.CanRotate && g_Rectangles[rid].x2 - g_Rectangles[rid].x1 > sessionInfo.Width)) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = `Failed: Box n°${g_Rectangles[rid].id} is too big.`
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
}
// -------------------------- //
sessionInfo.StartTime = performance.now()
// Start Main Iteration over Rectangles //
for (let rid = 0; rid < g_Rectangles.length; rid++) {
let res = getBestHole(g_Rectangles[rid], 1, sessionInfo.CanRotate)
if (res.bestHole !== null) { // Mainly for debugging, this SHOULD never occur ...
let bestHole = res.bestHole
if (res.doRotation) { // If Best Hole needs rotation of rectangle --> rotate Current Rectangle
g_Rectangles[rid] = rotateRectangle(g_Rectangles[rid])
}
// Place Current Rectangle in Best Hole //
const w = (g_Rectangles[rid].x2 - g_Rectangles[rid].x1)
const h = (g_Rectangles[rid].y2 - g_Rectangles[rid].y1)
g_Rectangles[rid].x2 = bestHole.x1 + w
g_Rectangles[rid].y2 = bestHole.y1 + h
g_Rectangles[rid].x1 = bestHole.x1
g_Rectangles[rid].y1 = bestHole.y1
// ----------------------------------- //
g_Rectangles[rid].origin = bestHole.origin // Debugging purposes to test cases that are most redundant, or faulty
let newHoles = [] // New array that will overwrite the current g_Holes
for (let hid = 0; hid < g_Holes.length; hid++) {
if (overlap(g_Rectangles[rid], g_Holes[hid])) { // If the Current Rectangle overlaps with a hole, we break the hole into new holes ...
if (!createNewHoles(g_Rectangles[rid], g_Holes[hid], newHoles)) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = "Failed: Couldn't place rectangle n°" + g_Rectangles[rid].id
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
} else { // if it doesn't overlap, we add the hole to the newHoles overwrite array
newHoles[newHoles.length] = g_Holes[hid]
}
}
g_Holes = sortByHeight(newHoles)
if (!createNewHoles(g_Rectangles[rid], bestHole, g_Holes)) {
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = "Failed: Couldn't place rectangle n°" + g_Rectangles[rid].id
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
g_Holes = sortByHeight(g_Holes)
} else { // If we can't find a Best Hole for a rectangle (should never happen), we abort since it's an abnormal case
state.attributes.style.value = "color:rgb(255, 0, 0)"
state.innerHTML = "Failed: Couldn't find hole for rectangle n°" + g_Rectangles[rid].id
inProgress = false
return setTimeout(() => { if (!inProgress) { Reset() } }, 2000)
}
}
// End Main For Loop //
// Show Run Time (Time it took to calculate position + orientation of all holes) //
sessionInfo.EndTime = performance.now()
sessionInfo.RunTime = capNumber((sessionInfo.EndTime - sessionInfo.StartTime) / 1000, 3)
if (sessionInfo.RunTime < 60) {
outputFields.runTime.innerHTML = sessionInfo.RunTime + " s"
} else {
const inMinutes = capNumber(sessionInfo.RunTime / 60, 0)
const inSeconds = capNumber((sessionInfo.RunTime / 60) - inMinutes, 2)
outputFields.runTime.innerHTML = inMinutes + " m " + inSeconds + " s"
}
// ----------------------------------------------------------------------------- //
g_Rectangles = sortByY(g_Rectangles) // Sort Rectangles In Order they appear on screen (User Friendly Display Method)
// Get Canvas Height + Rectangles Combined Area //
let endRectArea = 0
let shortestHeight = 0
g_Rectangles.forEach(rectangle => {
const w = (rectangle.x2 - rectangle.x1)
const h = (rectangle.y2 - rectangle.y1)
endRectArea = endRectArea + w * h
if (rectangle.y2 > shortestHeight) {
shortestHeight = rectangle.y2
}
})
// ------------------------------ //
// (Show + Scale) Height On Canvas //
canvas.style.height = shortestHeight * 2 + "px"
canvas.height = canvas.clientHeight
gridHeight.style.marginTop = shortestHeight * 2 + 18 + "px"
gridHeight.innerHTML = shortestHeight
const newHeight = (2 - Math.round(gridHeight.getBoundingClientRect().width) / 2).toString()
gridHeight.style.marginLeft = Math.floor(newHeight) + "px"
// ------------------------------ //
// (Show + Scale) Width On Canvas //
canvas.style.width = sessionInfo.Width * 2 + "px"
canvas.width = canvas.clientWidth
gridWidth.innerHTML = sessionInfo.Width
const newWidth = (sessionInfo.Width * 2 - Math.round(gridWidth.getBoundingClientRect().width) / 2).toString()
gridWidth.style.marginLeft = Math.floor(newWidth) + "px"
outputBox.style.marginLeft = sessionInfo.Width * 2 + 310 + "px"
outputRectanglesBox.style.marginLeft = sessionInfo.Width * 2 + 310 + "px"
// ------------------------------ //
// Calculate Theoretical Minimum Height //
// To Calculate the Theoretical Minimum Height:
// 1) We have all the rectangles so we accumulate their Area (W*H)
// 2) And then we divide it by the input width the user has given ...
// since H of rectangle is A/W
let minHeight = 0
g_Rectangles.forEach(rectangle => {
const w = rectangle.x2 - rectangle.x1
const h = rectangle.y2 - rectangle.y1
minHeight = minHeight + w * h
});
minHeight = minHeight / sessionInfo.Width
outputFields.theoreticalMinHeight.innerHTML = Math.round(minHeight) // Display It
// ------------------------------------ //
// Draw + Show Rectangles //
outputRectangles.innerHTML = "<p></p><p></p>"
g_Rectangles.forEach(rectangle => {
const w = (rectangle.x2 - rectangle.x1)
const h = (rectangle.y2 - rectangle.y1)
outputRectangles.innerHTML = `${outputRectangles.innerHTML + rectangle.id}'${scaleString(rectangle.id, 3)} : ${w}${scaleString(w, 3)} (width) x ${h}${scaleString(h, 3)} (height) [ ${rectangle.x1}${scaleString(rectangle.x1, 5)}, ${rectangle.y1}${scaleString(rectangle.y1, 5)}, ${rectangle.x2}${scaleString(rectangle.x2, 5)}, ${rectangle.y2}${scaleString(rectangle.y2, 5)}]<p></p><p></p>`
let nRectangle = new Rectangle(rectangle.x1 * 2, rectangle.y1 * 2, rectangle.x2 * 2, rectangle.y2 * 2, rectangle.id, rectangle.origin)
drawRectangle(nRectangle)
})
// ---------------------- //