-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcSpatialGrid.cls
926 lines (728 loc) · 24.8 KB
/
cSpatialGrid.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cSpatialGrid"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Type tCELL
pIDX() As Long
NP As Long
MaxNP As Long
End Type
Private mW As Long
Private mH As Long
Private GridSize As Long
Private CellW As Long
Private CellH As Long
Private CELL() As tCELL
Private NP As Long
Private MaxNP As Long
Private PX() As Double
Private PY() As Double
Private MaxDistance2 As Double
Private PairP1() As Long
Private PairP2() As Long
Private PairDX() As Double
Private PairDY() As Double
Private PairD() As Double
Private Npairs As Long
Private MAXNpair As Long
'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal ByteLen As Long)
Private Declare Function GetMem4 Lib "MSVBVM60" (ByVal Src As Long, ByVal dest As Long) As Long
Private Declare Function ArrPtr Lib "MSVBVM60" Alias "VarPtr" (Arr() As Any) As Long
Private Declare Function vbaCopyBytes Lib "msvbvm60.dll" Alias "__vbaCopyBytes" (ByVal Length As Long, Dst As Long, Src As Long) As Long
Public Property Get NofPoints() As Long
NofPoints = NP
End Property
Public Property Get getPointX(I&) As Double
getPointX = PX(I)
End Property
Public Property Get getPointY(I&) As Double
getPointY = PY(I)
End Property
Private Sub pvArrCopySingle(dest() As Double, Src() As Double)
Dim Size As Long
Dim W As Long
Dim H As Long
' W = UBound(Src, 1)
' H = UBound(Src, 2)
' Size = (W + 1) * (H + 1) * LenB(Src(0, 0))
If pvArrayExists(ArrPtr(Src)) Then
W = UBound(Src)
Size = (W + 1) * LenB(Src(0))
If pvArrayExists(ArrPtr(dest)) Then
' If (W - UBound(dest, 1)) Or (H - UBound(dest, 2)) Then
If (W - UBound(dest, 1)) Then
'ReDim dest(W, H)
ReDim dest(W)
End If
Else
'''Array DEST has No Dimension
'ReDim dest(W, H)
ReDim dest(W)
End If
' ' CopyMemory ByVal VarPtr(dest(0, 0)), ByVal VarPtr(Src(0, 0)), Size
'
' CopyMemory ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0)), Size
vbaCopyBytes Size, ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0))
End If
End Sub
Private Sub pvArrCopyLong(dest() As Long, Src() As Long)
Dim Size As Long
Dim W As Long
Dim H As Long
' W = UBound(Src, 1)
' H = UBound(Src, 2)
' Size = (W + 1) * (H + 1) * LenB(Src(0, 0))
If pvArrayExists(ArrPtr(Src)) Then
W = UBound(Src)
Size = (W + 1) * LenB(Src(0))
If pvArrayExists(ArrPtr(dest)) Then
' If (W - UBound(dest, 1)) Or (H - UBound(dest, 2)) Then
If (W - UBound(dest, 1)) Then
'ReDim dest(W, H)
ReDim dest(W)
End If
Else
'''Array DEST has No Dimension
'ReDim dest(W, H)
ReDim dest(W)
End If
' CopyMemory ByVal VarPtr(dest(0, 0)), ByVal VarPtr(Src(0, 0)), Size
' CopyMemory ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0)), Size
vbaCopyBytes Size, ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0))
End If
End Sub
Private Function pvArrayExists(ByVal ppArray As Long) As Long
GetMem4 ppArray, VarPtr(pvArrayExists)
End Function
Friend Sub Init(WorldW As Long, WorldH As Long, MaxDist As Long)
mW = WorldW
mH = WorldH
GridSize = MaxDist
MaxDistance2 = MaxDist * MaxDist
If CellW <> mW \ GridSize Or CellH <> mH \ GridSize Then 'Per Mantenere MaxNP
CellW = mW \ GridSize
CellH = mH \ GridSize
ReDim CELL(CellW, CellH)
End If
NP = 0
Npairs = 0
End Sub
Friend Sub ResetPoints()
Dim X As Long
Dim Y As Long
For Y = 0 To CellH
For X = 0 To CellW
CELL(X, Y).NP = 0&
Next
Next
NP = 0
' MaxNP = 0
End Sub
Friend Sub InsertPoint(ByVal X As Double, ByVal Y As Double)
NP = NP + 1
If NP > MaxNP Then
MaxNP = NP * 2
'If NP > UBound(pX) Then
MaxNP = NP * 3
ReDim Preserve PX(MaxNP)
ReDim Preserve PY(MaxNP)
End If
PX(NP) = X
PY(NP) = Y
pvAddToCell X \ GridSize, Y \ GridSize, NP
End Sub
'''Friend Sub InsertPointWithIDX(ByVal X As Double, ByVal Y As Double, IDX As Long)
'''
'''
''' NP = NP + 1
''' 'If NP > MaxNP Then
''' ' MaxNP = NP * 2
''' If NP > UBound(pX) Then
''' MaxNP = NP * 3
''' ReDim Preserve pX(MaxNP)
''' ReDim Preserve pY(MaxNP)
''' End If
''' pX(NP) = X
''' pY(NP) = Y
'''
''' pvAddToCell X \ GridSize, Y \ GridSize, IDX 'NP
'''
'''End Sub
Friend Sub InsertALLpoints(X() As Double, Y() As Double, Optional LimitNofPointTo As Long = 0)
'Remember to Call ResetPoints First
Dim I As Long
Dim u As Long
If LimitNofPointTo Then ' To avoid pre-Call REDIMing of X Y in some cases
u = LimitNofPointTo
Else
u = UBound(X)
End If
For I = 1 To u
NP = NP + 1
If NP > MaxNP Then
' MaxNP = NP + 64 '* 2
MaxNP = 1 + NP * 1.5
' ReDim Preserve PX(MaxNP)
' ReDim Preserve PY(MaxNP)
End If
' PX(NP) = X(I)
' PY(NP) = Y(I)
pvAddToCell X(I) \ GridSize, Y(I) \ GridSize, NP
Next
pvArrCopySingle PX, X
pvArrCopySingle PY, Y
End Sub
Private Sub pvAddToCell(ByVal cellX As Long, ByVal cellY As Long, ByVal currPindex As Long)
With CELL(cellX, cellY)
.NP = .NP + 1
If .NP > .MaxNP Then
.MaxNP = .NP + 64 '* 2
ReDim Preserve .pIDX(.MaxNP)
End If
.pIDX(.NP) = currPindex
End With
End Sub
Friend Sub GetPairs(RP1() As Long, RP2() As Long, Rdx() As Double, Rdy() As Double, rPairsCount As Long)
'CORE SUB
Dim X As Long
Dim Y As Long
Dim DX As Double
Dim DY As Double
Dim I As Long
Dim J As Long
Dim cWm1 As Long
Dim cHm1 As Long
Dim Xp1 As Long
Dim Yp1 As Long
Dim Ym1 As Long
Dim IJ As Long
Dim ii As Long
Dim PXI As Double
Dim PYI As Double
cWm1 = CellW - 1&
cHm1 = CellH - 1&
Npairs = 0&
' O
' /
' X-O
' |\
' O O
For Y = 1& To cHm1
Yp1 = Y + 1&
Ym1 = Y - 1&
For X = 0& To cWm1
Xp1 = X + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1& to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP 'SELF
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'RIGHT
For J = 1& To CELL(Xp1, Y).NP
IJ = CELL(Xp1, Y).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'down
For J = 1& To CELL(X, Yp1).NP
IJ = CELL(X, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'DownRight Diag
For J = 1& To CELL(Xp1, Yp1).NP
IJ = CELL(Xp1, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'upRight Diag
For J = 1& To CELL(Xp1, Ym1).NP
IJ = CELL(Xp1, Ym1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
Next
End With
Next
Next
'-------- NOW Bottom ROW
' O
' /
' X-O
Y = CellH
Ym1 = Y - 1&
For X = 0& To cWm1
Xp1 = X + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1& to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'RIGHT ( )
For J = 1& To CELL(Xp1, Y).NP
IJ = CELL(Xp1, Y).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'upRight Diag
For J = 1& To CELL(Xp1, Ym1).NP
IJ = CELL(Xp1, Ym1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
Next
End With
'-------- NOW TOP ROW
' X-O
' |\
' O O
With CELL(X, 0&)
For I = 1& To .NP '- 1& ' Should be -1& to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'RIGHT ( )
For J = 1& To CELL(Xp1, 0&).NP
IJ = CELL(Xp1, 0&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'DownRight Diag
For J = 1& To CELL(Xp1, 1&).NP
IJ = CELL(Xp1, 1&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
'Down
For J = 1& To CELL(X, 1&).NP
IJ = CELL(X, 1&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
Next
End With
Next X
' -----------------------------RIGHT Column
' X
' |
' O
X = CellW
For Y = 0& To cHm1
Yp1 = Y + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1& to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
' DOWN )
For J = 1& To CELL(X, Yp1).NP
IJ = CELL(X, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
Next
End With
Next
' -------- FINALLY bottm right cell
With CELL(CellW, CellH)
'SELF
For I = 1& To .NP - 1&
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
If DX * DX + DY * DY < MaxDistance2 Then pvAddPair ii, IJ, DX, DY
Next
Next
End With
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
rPairsCount = Npairs
' rP1 = PairP1
' rP2 = PairP2
' rDX = PairDX
' rDY = PairDY
'FASTER
pvArrCopyLong RP1, PairP1
pvArrCopyLong RP2, PairP2
pvArrCopySingle Rdx, PairDX
pvArrCopySingle Rdy, PairDY
' 'Check for ERRORS (Duplicates)
' For X = 1& To Npairs - 1&
' For Y = X + 1& To Npairs
' If (PairP1(X) = PairP1(Y)) And (PairP2(X) = PairP2(Y)) Then Stop
' If (PairP1(X) = PairP2(Y)) And (PairP2(X) = PairP1(Y)) Then Stop
' Next
' Next
End Sub
Friend Sub GetPairsWDist(RP1() As Long, RP2() As Long, Rdx() As Double, Rdy() As Double, RD() As Double, rPairsCount As Long)
'CORE SUB
Dim X As Long
Dim Y As Long
Dim DX As Double
Dim DY As Double
Dim I As Long
Dim J As Long
Dim D As Double
Dim cWm1 As Long
Dim cHm1 As Long
Dim Xp1 As Long
Dim Yp1 As Long
Dim Ym1 As Long
Dim IJ As Long
Dim ii As Long
Dim PXI As Double
Dim PYI As Double
cWm1 = CellW - 1&
cHm1 = CellH - 1&
Npairs = 0&
' O
' /
' X-O
' |\
' O O
For Y = 1& To cHm1
Yp1 = Y + 1&
Ym1 = Y - 1&
For X = 0& To cWm1
Xp1 = X + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP 'SELF
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'RIGHT
For J = 1& To CELL(Xp1, Y).NP
IJ = CELL(Xp1, Y).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'down
For J = 1& To CELL(X, Yp1).NP
IJ = CELL(X, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'DownRight Diag
For J = 1& To CELL(Xp1, Yp1).NP
IJ = CELL(Xp1, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'upRight Diag
For J = 1& To CELL(Xp1, Ym1).NP
IJ = CELL(Xp1, Ym1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
Next
End With
Next
Next
'-------- NOW Bottom ROW
' O
' /
' X-O
Y = CellH
Ym1 = Y - 1&
For X = 0& To cWm1
Xp1 = X + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'RIGHT ( )
For J = 1& To CELL(Xp1, Y).NP
IJ = CELL(Xp1, Y).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'upRight Diag
For J = 1& To CELL(Xp1, Ym1).NP
IJ = CELL(Xp1, Ym1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
Next
End With
'-------- NOW TOP ROW
' X-O
' |\
' O O
With CELL(X, 0&)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'RIGHT ( )
For J = 1& To CELL(Xp1, 0&).NP
IJ = CELL(Xp1, 0&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'DownRight Diag
For J = 1& To CELL(Xp1, 1&).NP
IJ = CELL(Xp1, 1&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
'Down
For J = 1& To CELL(X, 1&).NP
IJ = CELL(X, 1&).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
Next
End With
Next X
' RIGHT Column
' X
' |
' O
X = CellW
For Y = 0& To cHm1
Yp1 = Y + 1&
With CELL(X, Y)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
' DOWN )
For J = 1& To CELL(X, Yp1).NP
IJ = CELL(X, Yp1).pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
Next
End With
Next
' -------- FINALLY bottm right cell
With CELL(CellW, CellH)
'SELF
For I = 1& To .NP - 1&
ii = .pIDX(I)
PXI = PX(ii)
PYI = PY(ii)
For J = I + 1& To .NP
IJ = .pIDX(J)
DX = PX(IJ) - PXI
DY = PY(IJ) - PYI
D = DX * DX + DY * DY
If D < MaxDistance2 Then pvAddPairWDist ii, IJ, DX, DY, D
Next
Next
End With
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
rPairsCount = Npairs
pvArrCopyLong RP1, PairP1
pvArrCopyLong RP2, PairP2
pvArrCopySingle Rdx, PairDX
pvArrCopySingle Rdy, PairDY
pvArrCopySingle RD, PairD
End Sub
Private Sub pvAddPair(ByVal P1 As Long, ByVal P2 As Long, ByVal DX As Double, ByVal DY As Double)
Stop
Npairs = Npairs + 1
If Npairs > MAXNpair Then
MAXNpair = Npairs + 128 '* 2
ReDim Preserve PairP1(MAXNpair)
ReDim Preserve PairP2(MAXNpair)
ReDim Preserve PairDX(MAXNpair)
ReDim Preserve PairDY(MAXNpair)
End If
PairP1(Npairs) = P1
PairP2(Npairs) = P2
PairDX(Npairs) = DX
PairDY(Npairs) = DY
End Sub
Private Sub pvAddPairWDist(ByVal P1 As Long, ByVal P2 As Long, _
ByVal DX As Double, ByVal DY As Double, ByVal D As Double)
Npairs = Npairs + 1
If Npairs > MAXNpair Then
' MAXNpair = Npairs + 128 '* 2
If Npairs > 4 Then
MAXNpair = Npairs * 1.3
Else
MAXNpair = Npairs + 8
End If
ReDim Preserve PairP1(MAXNpair)
ReDim Preserve PairP2(MAXNpair)
ReDim Preserve PairDX(MAXNpair)
ReDim Preserve PairDY(MAXNpair)
ReDim Preserve PairD(MAXNpair)
End If
PairP1(Npairs) = P1
PairP2(Npairs) = P2
PairDX(Npairs) = DX
PairDY(Npairs) = DY
PairD(Npairs) = D
End Sub
Friend Sub QueryPointsInCircle(cX As Double, cY As Double, R As Double, rDist() As Double, rIdx() As Long, UBcount As Long)
Dim X As Long
Dim Y As Long
Dim xx As Long
Dim yy As Long
Dim J As Long
Dim IJ As Long
Dim DX As Double
Dim DY As Double
Dim D As Double
Dim gg As Long
Dim R2 As Double
Dim XF As Long
Dim XT As Long
Dim YF As Long
Dim YT As Long
X = cX \ GridSize
Y = cY \ GridSize
ReDim rIdx(0)
Npairs = 0
gg = R \ GridSize
If gg < 1 Then gg = 1 'Commented 2023
'If GG < 1 Then Stop
'If GG > 2 Then Debug.Print GG & " " & R
R2 = R * R
XF = X - gg: If XF < 0& Then XF = 0&
XT = X + gg: If XT > CellW Then XT = CellW
YF = Y - gg: If YF < 0& Then YF = 0&
YT = Y + gg: If YT > CellH Then YT = CellH
For yy = YF To YT
For xx = XF To XT
With CELL(xx, yy)
For J = 1 To .NP
IJ = .pIDX(J)
DX = PX(IJ) - cX
DY = PY(IJ) - cY
D = DX * DX + DY * DY
If D <= R2 Then
pvAddPointWDist IJ, D
End If
Next
End With
Next
Next
pvArrCopySingle rDist, PairD
pvArrCopyLong rIdx, PairP1
UBcount = Npairs
End Sub
Private Sub pvAddPointWDist(ByVal P1 As Long, ByVal D As Double)
Npairs = Npairs + 1
If Npairs > MAXNpair Then
If Npairs > 4 Then
MAXNpair = Npairs * 1.3
Else
MAXNpair = Npairs * 1.5 'Npairs + 8
End If
ReDim Preserve PairP1(MAXNpair)
'ReDim Preserve PairP2(MAXNpair)
'ReDim Preserve PairDX(MAXNpair)
'ReDim Preserve PairDY(MAXNpair)
ReDim Preserve PairD(MAXNpair)
End If
PairP1(Npairs) = P1
'PairP2(Npairs) = P2
'PairDX(Npairs) = dx
'PairDY(Npairs) = dy
PairD(Npairs) = D
End Sub