Advertisement
WarPie90

Simba 1.4 intersection method

Jan 8th, 2023 (edited)
1,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.80 KB | None | 0 0
  1. program new;
  2. {$I SRL/osr.simba}
  3.  
  4. type TInt64Array = array of Int64;
  5. {$X+}
  6. procedure Quick64Sort(a:TInt64Array; ilo, ihi:Int32);
  7. begin
  8.   var lo := ilo;
  9.   var hi := ihi;
  10.   var pivot := a[(lo + hi) div 2];
  11.  
  12.   while True do
  13.   begin
  14.     while(a[lo] < pivot) do lo += 1;
  15.     while(a[hi] > pivot) do hi -= 1;
  16.     if lo <= hi then
  17.     begin
  18.       var tmp := a[lo];
  19.       a[lo] := a[hi];
  20.       a[hi] := tmp;
  21.       lo += 1;
  22.       hi -= 1;
  23.     end;
  24.     if (lo > hi) then break;
  25.   end;
  26.  
  27.   if(hi > ilo) then Quick64Sort(a, ilo, hi)
  28.   if(lo < ihi) then Quick64Sort(a, lo, ihi)
  29. end
  30.  
  31. function BinarySearch(value: Int64; list: TInt64Array): Int32;
  32. begin
  33.   var l := Low(list);
  34.   var h := High(list);
  35.   Result := -1;
  36.   while l <= h do
  37.   begin
  38.     var m := (l + h) div 2;
  39.     if list[m] > value then      h := m - 1
  40.     else if list[m] < value then l := m + 1
  41.     else                         Exit(m);
  42.   end;
  43. end;
  44. {$X-}
  45.  
  46. function Intersection(A,B: TPointArray): TPointArray;
  47. var
  48.   i: Int32;
  49.   aInt,bInt: TInt64Array;
  50. begin
  51.   SetLength(aInt, Length(A));
  52.   for i:=0 to High(A) do aInt[i] := Int64(A[i]);
  53.   Quick64Sort(aInt, 0, High(aInt));
  54.  
  55.   SetLength(bInt, Length(B));
  56.   for i:=0 to High(B) do bInt[i] := Int64(B[i]);
  57.   Quick64Sort(bInt, 0, High(bInt));
  58.  
  59.   for i:=0 to High(aInt) do
  60.     if BinarySearch(aInt[i], bInt) <> -1 then
  61.       Result += TPoint(aInt[i]);
  62. end;
  63.  
  64.  
  65.  
  66. var
  67.   a,b,c: TPointArray;
  68.   img: TMufasaBitmap;
  69.   t: Double;
  70. begin
  71.   a := TPAFromBox([100,100,300,300]);
  72.   b := TPAFromBox([200,200,400,400]);
  73.   WriteLn(Length(a) + Length(b));
  74.  
  75.   t := PerformanceTimer();
  76.   c := Intersection(A,B);
  77.   WriteLn(PerformanceTimer() - t,'ms');
  78.  
  79.   img.Init();
  80.   img.SetSize(600,600);
  81.   img.DrawTPA(a, 255);
  82.   img.DrawTPA(b, $FF0000);
  83.  
  84.   img.DrawTPA(c, $00FF00);
  85.   img.Debug();
  86. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement