-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpa.pas
2811 lines (2600 loc) · 69 KB
/
tpa.pas
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
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
TPA functions for the Mufasa Macro Library
}
unit tpa;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, mufasatypes;
function FastTPASort(const TPA: TPointArray;const Dists: TIntegerArray; maxDist: Integer; CloseFirst: Boolean): TPointArray;
procedure QuickSort(var A: TIntegerArray; iLo, iHi: Integer);
//Start Wizzyplugin
procedure tSwap(var a, b: TPoint);
procedure tpaSwap(var a, b: TPointArray);
procedure SwapE(var a, b: Extended);
procedure RAaSTPAEx(var a: TPointArray; const w, h: Integer);
procedure RAaSTPA(var a: TPointArray; const Dist: Integer);
function NearbyPointInArrayEx(const P: TPoint; w, h:Integer; a: TPointArray): Boolean;
function NearbyPointInArray(const P: TPoint; Dist:Integer; a: TPointArray): Boolean;
function ReArrangeandShortenArrayEx(const a: TPointArray; w, h: Integer): TPointArray;
function ReArrangeandShortenArray(const a: TPointArray; Dist: Integer): TPointArray;
function TPAtoATPAEx(const TPA: TPointArray; w, h: Integer): T2DPointArray;
function TPAtoATPA(const TPA: TPointArray; Dist: Integer): T2DPointArray;
procedure QuickTPASort(var A: TIntegerArray; var B: TPointArray; iLo, iHi: Integer; SortUp: Boolean);
procedure QuickATPASort(var A: TIntegerArray; var B: T2DPointArray; iLo, iHi: Integer; SortUp: Boolean);
procedure SortTPAByX(var a: TPointArray; const LowToHi: Boolean);
procedure SortTPAByY(var a: TPointArray; const LowToHi: Boolean);
function FindTPARows(a: TPointArray): T2DPointArray;
function FindTPAColumns(a: TPointArray): T2DPointArray;
procedure SortTPAFrom(var a: TPointArray; const From: TPoint);
procedure SortATPAFrom(var a: T2DPointArray; const From: TPoint);
procedure SortATPAFromFirstPoint(var a: T2DPointArray; const From: TPoint);
procedure SortATPAFromMidPoint(var a: T2DPointArray; const From: TPoint);
procedure SortATPAFromFirstPointX(var a: T2DPointArray; const From: TPoint);
procedure SortATPAFromFirstPointY(var a: T2DPointArray; const From: TPoint);
procedure InvertTPA(var a: TPointArray);
procedure InvertATPA(var a: T2DPointArray);
function MiddleTPAEx(const TPA: TPointArray; var x, y: Integer): Boolean;
function MiddleTPA(const tpa: TPointArray): TPoint;
procedure MedianTPAEx(var tpa: TPointArray; out x, y: integer);
function MedianTPA(var tpa: TPointArray): TPoint;
procedure SortATPASize(var a: T2DPointArray; const BigFirst: Boolean);
procedure SortATPAFromSize(var a: T2DPointArray; const Size: Integer; CloseFirst: Boolean);
procedure FilterTPAsBetween(var atpa: T2DPointArray; const minLength, maxLength: integer);
function CombineTPA(const Ar1, Ar2: TPointArray): TPointArray;
function CombineIntArray(const Ar1, Ar2: TIntegerArray): TIntegerArray;
function InIntArrayEx(const a: TIntegerArray; var Where: Integer; const Number: Integer): Boolean;
function InIntArray(const a: TIntegerArray; Number: Integer): Boolean;
procedure ClearSameIntegers(var a: TIntegerArray);
procedure ClearSameIntegersAndTPA(var a: TIntegerArray; var p: TPointArray);
function SplitTPAEx(const arr: TPointArray; w, h: Integer): T2DPointArray;
function SplitTPA(const arr: TPointArray; Dist: Integer): T2DPointArray;
function TPAPosNext(const Find: TPoint; const V: TPointArray; const PrevPos: Integer = -1;
const IsSortedAscending: Boolean = False): Integer;
function GlueTPAs(const V1, V2: TPointArray; const IsSortedAscending,byDifference: Boolean):TPointArray;
function FloodFillTPA(const TPA : TPointArray) : T2DPointArray;
procedure FilterPointsPie(var Points: TPointArray; const SD, ED, MinR, MaxR: Extended; Mx, My: Integer; Natural: Boolean);
procedure FilterPointsDist(var Points: TPointArray; const MinDist,MaxDist: Extended; Mx, My: Integer);
procedure FilterPointsLine(var Points: TPointArray; Radial: Extended; Radius, MX, MY: Integer);
procedure FilterPointsBox(var points: TPointArray; x1, y1, x2, y2: integer);
procedure FilterTPADist(var TPA: TPointArray; maxDist: integer);
function RemoveDistTPointArray(x, y, dist: Integer;const ThePoints: TPointArray; RemoveHigher: Boolean): TPointArray;
function GetATPABounds(const ATPA: T2DPointArray): TBox;
function GetTPABounds(const TPA: TPointArray): TBox;
function FindTPAinTPA(SearchTPA: TPointArray; const TotalTPA: TPointArray; var Matches: TPointArray): Boolean;
function FindTextTPAinTPA(Height : integer;const SearchTPA, TotalTPA: TPointArray; var Matches: TPointArray): Boolean;
function GetSamePointsATPA(const ATPA : T2DPointArray; var Matches : TPointArray) : boolean;
function FindGapsTPA(const TPA: TPointArray; MinPixels: Integer): T2DPointArray;
procedure SortCircleWise(var tpa: TPointArray; const cx, cy, StartDegree: Integer; SortUp, ClockWise: Boolean);
procedure LinearSort(var tpa: TPointArray; cx, cy, sd: Integer; SortUp: Boolean);
function MergeATPA(const ATPA : T2DPointArray) : TPointArray;
procedure AppendTPA(var TPA : TPointArray; const ToAppend : TPointArray);
function TPAFromLine(const x1, y1, x2, y2: Integer): TPointArray;
function EdgeFromBox(const Box: TBox): TPointArray;
function TPAFromBox(const Box : TBox) : TPointArray;
function TPAFromEllipse(const CX, CY, XRadius, YRadius : Integer): TPointArray;
function TPAFromCircle(const CX, CY, Radius: Integer): TPointArray;
procedure FillEllipse(var a: TPointArray);
function FindTPAEdges(const p: TPointArray): TPointArray;
function PointInTPA(const p: TPoint;const arP: TPointArray): Boolean;
function ClearTPAFromTPA(const arP, ClearPoints: TPointArray): TPointArray;
procedure ClearDoubleTPA(var TPA: TPointArray);
Function ReturnPointsNotInTPA(Const TotalTPA: TPointArray; const Box: TBox): TPointArray;
Procedure TPACountSort(Var TPA: TPointArray;const max: TPoint;Const SortOnX : Boolean);
Procedure TPACountSortBase(Var TPA: TPointArray;const maxx, base: TPoint; const SortOnX : Boolean);
procedure InvertTIA(var tI: TIntegerArray);
function SumIntegerArray(const Ints : TIntegerArray): Integer;
function AverageTIA(const tI: TIntegerArray): Integer;
function AverageExtended(const tE: TExtendedArray): Extended;
function SameTPA(const aTPA, bTPA: TPointArray): Boolean;
function TPAInATPA(const TPA: TPointArray;const InATPA: T2DPointArray; var Index: LongInt): Boolean;
procedure OffsetTPA(var TPA : TPointArray; const Offset : TPoint);
procedure OffsetATPA(var ATPA : T2DPointArray; const Offset : TPoint);
implementation
uses
math;
{/\
Very Fast TPA Sort, uses an adepted CountSort algorithm.
/\}
Function FastTPASort(const TPA: TPointArray;const Dists: TIntegerArray; maxDist: Integer; CloseFirst: Boolean): TPointArray;
{
If you want to understand this algorithm, it might be helpful to read about
CountSort. This algorithm is quite similar to CountSort.
}
Var
Oc, D: TIntegerArray;
I, H, J: Integer;
ind: array of array of integer;
Begin
SetLength(Oc, maxDist + 1); // Oc stores the occurances of each distance.
H := High(Dists); // The amount of values in the given Array of Points...
// We store it instead of calling High(Dists) each time.
// Get the occurance of each distance, and store it in Oc.
// We need the occurances of each distance, so we can later on set the 2d Array
// to it's proper length.
For I := 0 To H Do
Oc[Dists[I]] := Oc[Dists[I]] + 1;
// What we are basically going to do now is the following:
// Sort the points by their distances using countsort.
// But because (-2, -2) and (2, 2) have the same distance from (0, 0), we also
// store the index in the array 'TPA', which contains the actual points.
// This way we can retreive the actuals points.
// D nothing but a counter. It contains the amount of points (indices) stored
// for EACH distance, so it's length is equal to the amount of Distances.
// ind is the array that has as first index the Distance. The second depth of
// the array is the index which is used to retreive the original point.
SetLength(D, maxDist + 1);
SetLength(ind, maxDist + 1);
For I := 0 To maxDist Do // set the ind length of each ind[distance] to Oc[distance]
SetLength(ind[i], Oc[i] + 1);
For I := 0 To H Do // Performs the actual index sorting
Begin
// put an index (of Dists AND TPA) into a distance array.
ind[Dists[I]][D[Dists[I]]] := I;
// inc by one, so we won't override previous indexes.
D[Dists[i]] := D[Dists[i]] + 1;
End;
// Here we are back to the CountSort style of writing back.
// Now we are going to write back to the Result.
// The Result's length is obviously of the same length as 'TPA'.
SetLength(Result, H + 1);
H := 0;
// CloseFirst just means: Start writing from dist 0 to maxDist, or
// start writing from maxDist to dist 0. The first one places the closest points
// at the start of the result. The higher the index, the more far away the point.
// Not Closefirst means put the most far away point in Result first.
If CloseFirst Then
Begin
For I := 0 To maxDist Do // Put back to result
For J := 0 To oC[I] - 1 Do
Begin
Result[H] := TPA[ind[I][J]];
H := H + 1;
End;
End
Else
For I := maxDist Downto 0 Do // Put back to result
For J := 0 To oC[I] - 1 Do
Begin
Result[H] := TPA[ind[I][J]];
H := H + 1;
End;
// Voila!
End;
procedure QuickSort(var A: TIntegerArray; iLo, iHi: Integer) ;
var
Lo, Hi, Pivot, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Pivot := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Pivot do Inc(Lo) ;
while A[Hi] > Pivot do Dec(Hi) ;
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo) ;
Dec(Hi) ;
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi) ;
if Lo < iHi then QuickSort(A, Lo, iHi) ;
end;
{const
flnC=545947;
fsqrtA:single=0.5;
{$ASMMODE INTEL}
function fsqrt(x: Single): Single;
begin
asm
sub dword ptr x, ($3F800000-flnC)
fild dword ptr x
fmul fsqrtA
fistp dword ptr x
add dword ptr x,($3F800000-flnC)
fld x
end;
end; }
procedure tSwap(var a, b: TPoint);
var
c: TPoint;
begin
c := a;
a := b;
b := c;
end;
procedure tpaSwap(var a, b: TPointArray);
var
c: TPointArray;
begin
c := a;
a := b;
b := c;
end;
procedure SwapE(var a, b: Extended);
var
c: extended;
begin
c := a;
a := b;
b := c;
end;
{/\
Leaves one point per box with side lengths W and H to the TPA.
/\}
procedure RAaSTPAEx(var a: TPointArray; const w, h: Integer);
var
i, c, NoTP, l: Integer;
t: TPoint;
Found : boolean;
begin
NoTP := 0;
l := High(a);
for i := 0 to l do
begin
Found := false;
for c := 0 to NoTP - 1 do
if (Abs(a[i].x - a[c].x) <= w) and (Abs(a[i].y - a[c].y) <= h) then
begin
Found := true;
Break;
end;
if not Found then
// if (c >= NoTP) then
begin
t := a[i];
a[i] := a[NoTP];
a[NoTP] := t;
Inc(NoTP);
end;
end;
SetLength(a, NoTP);
end;
{/\
Leaves one point per box with the side length Dist.
/\}
procedure RAaSTPA(var a: TPointArray; const Dist: Integer);
var
i, c, NoTP, l: Integer;
t: TPoint;
Found : boolean;
begin
NoTP := 0;
l := High(a);
for i := 0 to l do
begin
Found := false;
for c := 0 to NoTP - 1 do
if (Round(sqrt(Sqr(a[i].x - a[c].x) + Sqr(a[i].y - a[c].y))) <= Dist) then
begin
Found := True;
Break;
end;
if not Found then
// if (c >= NoTP) then
begin
t := a[i];
a[i] := a[NoTP];
a[NoTP] := t;
Inc(NoTP);
end;
end;
SetLength(a, NoTP);
end;
{/\
Returns true if the point P is near a point in the TPA a with the max X and Y distances W and H.
/\}
function NearbyPointInArrayEx(const P: TPoint; w, h:Integer; a: TPointArray): Boolean;
var
i, l: Integer;
begin
Result := False;
l := High(a);
for i := 0 to l do
if (Abs(P.x - a[i].x) <= w) and (Abs(P.y - a[i].y) <= h) then
begin
Result := True;
Exit;
end;
end;
{/\
Returns true if the point P is near a point in the TPA a with the max distance Dist.
/\}
function NearbyPointInArray(const P: TPoint; Dist:Integer; a: TPointArray): Boolean;
var
i, l: Integer;
begin
Result := False;
l := High(a);
for i := 0 to l do
if (Round(sqrt(Sqr(P.x - a[i].x) + Sqr(P.y - a[i].y))) <= Dist) then
begin
Result := True;
Exit;
end;
end;
{/\
Results the TPointArray a with one point per box with side lengths W and H left.
/\}
function ReArrangeandShortenArrayEx(const a: TPointArray; w, h: Integer): TPointArray;
var
i, t, c, l: Integer;
Found: Boolean;
begin
l := High(a);
c := 0;
SetLength(Result, l + 1);
for i := 0 to l do
begin
Found := False;
for t := 0 to c -1 do
if (Abs(Result[t].x - a[i].x) <= w) and (Abs(Result[t].y - a[i].y) <= h) then
begin
Found := True;
Break;
end;
if not Found then
// if (t >= c) then
begin
Result[c] := a[i];
Inc(c);
end;
end;
SetLength(Result, c);
end;
{/\
Results the TPointArray a with one point per box with side length Dist left.
/\}
function ReArrangeandShortenArray(const a: TPointArray; Dist: Integer): TPointArray;
var
i, t, c, l: Integer;
Found: Boolean;
begin
l := High(a);
c := 0;
SetLength(Result, l + 1);
for i := 0 to l do
begin
Found := False;
for t := 0 to c -1 do
if (Round(sqrt(Sqr(Result[t].x - a[i].x) + Sqr(Result[t].y - a[i].y))) <= Dist) then
begin
Found := True;
Break;
end;
if not found then
// if (t >= c) then
begin
Result[c] := a[i];
Inc(c);
end;
end;
SetLength(Result, c);
end;
{/\
Splits the TPA to boxes with sidelengths W and H and results them as a T2DPointArray.
/\}
function TPAtoATPAEx(const TPA: TPointArray; w, h: Integer): T2DPointArray;
var
a, b, c, l: LongInt;
Found: Boolean;
begin
SetLength(Result, 0);
l := High(TPA);
c := 0;
for a := 0 to l do
begin
Found := false;
for b := 0 to c -1 do
if (Abs(TPA[a].X - Result[b][0].X) <= w) and (Abs(TPA[a].Y - Result[b][0].Y) <= h) then
begin
Found := True;
Break;
end;
if Found then
// if (b < c) then
begin
SetLength(Result[b], Length(Result[b]) + 1);
Result[b][High(Result[b])] := TPA[a];
end else
begin
SetLength(Result, c + 1);
SetLength(Result[c], 1);
Result[c][0] := TPA[a];
Inc(c);
end;
end;
end;
{/\
Splits the TPA to boxes with sidelength Dist and results them as a T2DPointArray.
/\}
function TPAtoATPA(const TPA: TPointArray; Dist: Integer): T2DPointArray;
var
a, b, c, l: LongInt;
Found: Boolean;
begin
SetLength(Result, 0);
l := High(tpa);
c := 0;
for a := 0 to l do
begin
Found := false;
for b := 0 to c -1 do
if (Round(sqrt(Sqr(TPA[a].X - Result[b][0].X) + Sqr(TPA[a].Y - Result[b][0].Y))) <= Dist) then
begin
Found := True;
Break;
end;
if Found then
// if (b < c) then
begin
SetLength(Result[b], Length(Result[b]) + 1);
Result[b][High(Result[b])] := TPA[a];
end else
begin
SetLength(Result, c + 1);
SetLength(Result[c], 1);
Result[c][0] := TPA[a];
Inc(c);
end;
end;
end;
{/\
Sorts the given TPointArray.
/\}
procedure QuickTPASort(var A: TIntegerArray; var B: TPointArray; iLo, iHi: Integer; SortUp: Boolean);
var
Lo, Hi, Mid, T: Integer;
TP: TPoint;
begin
if (Length(A) <> Length(B)) then Exit;
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) shr 1];
repeat
if SortUp then
begin
while (A[Lo] < Mid) do Inc(Lo);
while (A[Hi] > Mid) do Dec(Hi);
end else
begin
while (A[Lo] > Mid) do Inc(Lo);
while (A[Hi] < Mid) do Dec(Hi);
end;
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
TP := B[Lo];
B[Lo] := B[Hi];
B[Hi] := TP;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if (Hi > iLo) then QuickTPASort(A, B, iLo, Hi, SortUp);
if (Lo < iHi) then QuickTPASort(A, B, Lo, iHi, SortUp);
end;
{/\
Sorts the given T2DPointArray.
/\}
procedure QuickATPASort(var A: TIntegerArray; var B: T2DPointArray; iLo, iHi: Integer; SortUp: Boolean);
var
Lo, Hi, Mid, T: Integer;
TP: TPointArray;
begin
if (Length(A) <> Length(B)) then Exit;
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) shr 1];
repeat
if SortUp then
begin
while (A[Lo] < Mid) do Inc(Lo);
while (A[Hi] > Mid) do Dec(Hi);
end else
begin
while (A[Lo] > Mid) do Inc(Lo);
while (A[Hi] < Mid) do Dec(Hi);
end;
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
TP := B[Lo];
B[Lo] := B[Hi];
B[Hi] := TP;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if (Hi > iLo) then QuickATPASort(A, B, iLo, Hi, SortUp);
if (Lo < iHi) then QuickATPASort(A, B, Lo, iHi, SortUp);
end;
procedure SortTPAByX(var a: TPointArray; const LowToHi: Boolean);
var
i, l: Integer;
Arr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(Arr, l + 1);
for i := 0 to l do
Arr[i] := a[i].x;
QuickTPASort(arr, a, 0, l, LowToHi);
end;
procedure SortTPAByY(var a: TPointArray; const LowToHi: Boolean);
var
i, l: Integer;
Arr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(Arr, l + 1);
for i := 0 to l do
Arr[i] := a[i].y;
QuickTPASort(arr, a, 0, l, LowToHi);
end;
function FindTPARows(a: TPointArray): T2DPointArray;
var
bounds: TBox;
l: Integer;
begin
l := High(a);
if (l < 1) then
if l = 0 then
begin
SetLength(Result, 1);
Result[0] := a;
Exit;
end else
Exit;
bounds := GetTPABounds(a);
SortTPAByX(a, True);
Result := TPAtoATPAEx(a, bounds.x2 - bounds.x1, 0);
end;
function FindTPAColumns(a: TPointArray): T2DPointArray;
var
bounds: TBox;
l: Integer;
begin
l := High(a);
if (l < 1) then
if l = 0 then
begin
SetLength(Result, 1);
Result[0] := a;
Exit;
end else
Exit;
bounds := GetTPABounds(a);
SortTPAByY(a, True);
Result := TPAtoATPAEx(a, 0, bounds.y2 - bounds.y1);
end;
{/\
Sorts the TPointArray a from the point From.
Closest one to the point is [0], second closest is [1] etc.
/\}
procedure SortTPAFrom(var a: TPointArray; const From: TPoint);
var
i, l: Integer;
DistArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
DistArr[i] := Round(Sqr(From.x - a[i].x) + Sqr(From.y - a[i].y));
QuickTPASort(DistArr, a, 0, l, True);
end;
{/\
Sorts the T2DPointArray a from the point From.
/\}
procedure SortATPAFrom(var a: T2DPointArray; const From: TPoint);
var
i, l: Integer;
begin
l := High(a);
if (l < 0) then Exit;
for i := 0 to l do
SortTPAFrom(a[i], From);
end;
{/\
Sorts the T2DPointArray a from the point From.
/\}
procedure SortATPAFromFirstPoint(var a: T2DPointArray; const From: TPoint);
var
i, l: Integer;
DistArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
begin
if length(a[i]) = 0 then continue;
DistArr[i] := Round(Sqr(From.x - a[i][0].x) + Sqr(From.y - a[i][0].y));
end;
QuickATPASort(DistArr, a, 0, l, True);
end;
{/\
Sorts the T2DPointArray a from the midpoint of each TPA by From.
/\}
procedure SortATPAFromMidPoint(var a: T2DPointArray; const From: TPoint);
var
i, l: Integer;
DistArr: TIntegerArray;
MidPt: TPoint;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
begin
MidPt := MiddleTPA(a[i]);
DistArr[i] := Round(Sqr(From.x - MidPt.x) + Sqr(From.y - MidPt.y));
end;
QuickATPASort(DistArr, a, 0, l, True);
end;
{/\
Sorts the T2DPointArray a from the first X point of each TPA by from.
/\}
procedure SortATPAFromFirstPointX(var a: T2DPointArray; const From: TPoint);
var
i, l: Integer;
DistArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then
Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
begin
if (length(a[i]) <= 0) then
continue;
DistArr[i] := Round(Sqr(From.x - a[i][0].x));
end;
QuickATPASort(DistArr, a, 0, l, True);
end;
{/\
Sorts the T2DPointArray a from the first Y point of each TPA by from.
/\}
procedure SortATPAFromFirstPointY(var a: T2DPointArray; const From: TPoint);
var
i, l: Integer;
DistArr: TIntegerArray;
begin
l := high(a);
if (l < 0) then
Exit;
setLength(DistArr, l + 1);
for i := 0 to l do
begin
if (length(a[i]) <= 0) then
continue;
DistArr[i] := Round(Sqr(From.y - a[i][0].y));
end;
QuickATPASort(DistArr, a, 0, l, True);
end;
{/\
Inverts / Reverses the TPointArray a.
/\}
procedure InvertTPA(var a: TPointArray);
var
i, l: Integer;
b: tpointarray;
begin
l := High(a);
if (l < 0) then Exit;
b := Copy(a);
for i := l downto 0 Do
a[l - i] := b[i];
end;
{/\
Inverts / Reverts the T2DPointArray a.
/\}
procedure InvertATPA(var a: T2DPointArray);
var
i, l: Integer;
b: T2DPointArray;
begin
l := High(a);
if (l < 0) then Exit;
b := Copy(a);
for i := l downto 0 do
a[l - i] := b[i];
end;
{/\
Stores the coordinates of the middle of the TPointArray a to X and Y.
/\}
function MiddleTPAEx(const TPA: TPointArray; var x, y: Integer): Boolean;
var
i, l: Integer;
begin
Result := False;
l := High(tpa);
if (l < 0) then Exit;
x := 0;
y := 0;
for i := 0 to l do
begin
x := x + tpa[i].x;
y := y + tpa[i].y;
end;
x := x div (l + 1);
y := y div (l + 1);
Result := True;
end;
{/\
Returns the middle of the TPointArray tpa.
/\}
function MiddleTPA(const tpa: TPointArray): TPoint;
var
i, l: Integer;
begin
FillChar(result,sizeof(TPoint),0);
l := High(tpa);
if (l < 0) then Exit;
Result.x := 0;
Result.y := 0;
for i := 0 to l do
begin
Result.x := Result.x + tpa[i].x;
Result.y := Result.y + tpa[i].y;
end;
Result.x := Result.x div (l + 1);
Result.y := Result.y div (l + 1);
end;
{/\
Returns the x and y coords of the point in the tpa which is closest to the middle of the tpa.
/\}
procedure MedianTPAEx(var tpa: TPointArray; out x, y: integer);
var
p: TPoint;
begin
x := -1;
y := -1;
if (length(tpa) < 1) then
exit;
p := middleTPA(tpa);
sortTPAFrom(tpa, p);
x := tpa[0].x;
y := tpa[0].y;
end;
{/\
Returns the *point in the tpa* closest to the middle of the tpa.
/\}
function MedianTPA(var tpa: TPointArray): TPoint;
begin
MedianTPAEx(tpa, result.x, result.y);
end;
{/\
Sorts the T2DPointArray a from either largest or smallest, by the amount of points in the TPAs.
/\}
procedure SortATPASize(var a: T2DPointArray; const BigFirst: Boolean);
var
i, l: Integer;
SizeArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(SizeArr, l + 1);
for i := 0 to l do
SizeArr[i] := Length(a[i]);
QuickATPASort(SizeArr, a, 0, l, not BigFirst);
end;
{/\
Combines the TPointArrays Ar1 and Ar2, and results the combination.
/\}
procedure SortATPAFromSize(var a: T2DPointArray; const Size: Integer; CloseFirst: Boolean);
var
i, l: Integer;
SizeArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(SizeArr, l + 1);
for i := 0 to l do
SizeArr[i] := Abs(Length(a[i]) - Size);
QuickATPASort(SizeArr, a, 0, l, CloseFirst);
end;
procedure FilterTPAsBetween(var atpa: T2DPointArray; const minLength, maxLength: integer);
var
tmpATPA: T2DPointArray;
l, i, x, a: integer;
begin
if (minLength > maxLength) then
begin
writeln('FilterTPAsBetween: Tour min length is greater than your max length');
exit;
end;
l := length(atpa);
a := 0;
if (l < 1) then
exit;
for i := 0 to (l - 1) do
begin
x := length(atpa[i]);
if (not inRange(x, minLength, maxLength)) then
begin
setLength(tmpATPA, a + 1);
tmpATPA[a] := atpa[i];
inc(a);
end;
end;
atpa := tmpATPA;
end;
{/\
Combines the TPointArrays Ar1 and Ar2, and results the combination.
/\}
function CombineTPA(const Ar1, Ar2: TPointArray): TPointArray;
var
i, l1, l2: Integer;
begin
Result := Copy(Ar1);
l1 := Length(Result);
l2 := Length(Ar2);
SetLength(Result, l1 + l2);
for i := 0 to l2 -1 do
Result[i + l1] := Ar2[i];
end;
{/\
Combines the TIntegerArrays Ar1 and Ar2, and results the combination.
/\}
function CombineIntArray(const Ar1, Ar2: TIntegerArray): TIntegerArray;
var
i, l1, l2: Integer;
begin
Result := Copy(Ar1);
l1 := Length(Result);
l2 := Length(Ar2);
SetLength(Result, l1 + l2);
for i := 0 to l2 -1 do
Result[i + l1] := Ar2[i];
end;
{/\
Returns true if the integer Number was found in the integer array a, and stores the index to Where.
/\}
function InIntArrayEx(const a: TIntegerArray; var Where: Integer; const Number: Integer): Boolean;
var
i, l: Integer;
begin
Result := False;
l := High(a);
for i := 0 to l do
if (a[i] = Number) then
begin
Where := i;
Result := True;
Exit;
end;
end;
{/\
Returns true if the integer Number was found in the integer array a.
/\}
function InIntArray(const a: TIntegerArray; Number: Integer): Boolean;
var
i, l: Integer;