Janilabo

Janilabo | TPADistanceToTPA(Ex(2))

Nov 4th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.13 KB | None | 0 0
  1. function TPADistanceToTPA(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest)): Integer;
  2. var
  3.   h1, h2, i1, i2, d: Integer;
  4. begin
  5.   Result := -1;
  6.   h1 := High(TPA1);
  7.   h2 := High(TPA2);  
  8.   if ((h1 < 0) or (h2 < 0)) then
  9.     Exit;      
  10.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  11.   case method of
  12.     dtpClosest:
  13.     for i1 := 0 to h1 do
  14.       for i2 := 0 to h2 do  
  15.       begin      
  16.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  17.         if (d < Result) then
  18.           Result := d;
  19.       end;
  20.     dtpFarthest:
  21.     for i1 := 0 to h1 do
  22.       for i2 := 0 to h2 do
  23.       begin      
  24.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  25.         if (d > Result) then
  26.           Result := d;
  27.       end;    
  28.   end;
  29. end;
  30.  
  31. function TPADistanceToTPAEx(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pt1, m_pt2: TPoint): Integer;
  32. var
  33.   h1, i1, h2, i2, d: Integer;
  34. begin
  35.   Result := -1;
  36.   h1 := High(TPA1);
  37.   h2 := High(TPA2);      
  38.   if ((h1 < 0) or (h2 < 0)) then
  39.     Exit;      
  40.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  41.   m_pt1 := TPA1[0];
  42.   m_pt2 := TPA2[0];
  43.   case method of
  44.     dtpClosest:
  45.     for i1 := 0 to h1 do
  46.       for i2 := 0 to h2 do
  47.       begin      
  48.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  49.         if (d < Result) then
  50.         begin
  51.           Result := d;
  52.           m_pt1 := TPA1[i1];
  53.           m_pt2 := TPA2[i2];
  54.         end;
  55.       end;
  56.     dtpFarthest:
  57.     for i1 := 0 to h1 do
  58.       for i2 := 0 to h2 do
  59.       begin      
  60.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  61.         if (d > Result) then
  62.         begin
  63.           Result := d;
  64.           m_pt1 := TPA1[i1];
  65.           m_pt2 := TPA2[i2];
  66.         end;
  67.       end;  
  68.   end;
  69. end;
  70.  
  71. function TPADistanceToTPAEx2(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pts1, m_pts2: TPointArray): Integer;
  72. var
  73.   h1, i1, h2, i2, d, r, m: Integer;
  74. begin
  75.   Result := -1;
  76.   h1 := High(TPA1);
  77.   h2 := High(TPA2);  
  78.   if ((h1 < 0) or (h2 < 0)) then
  79.     Exit;      
  80.   Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
  81.   m := Max(h1, h2);
  82.   SetLength(m_pts1, (m + 1));
  83.   SetLength(m_pts2, (m + 1));
  84.   m_pts1[0] := TPA1[0];
  85.   m_pts2[0] := TPA2[0];
  86.   case method of
  87.     dtpClosest:
  88.     for i1 := 0 to h1 do
  89.       for i2 := 0 to h2 do
  90.       begin      
  91.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  92.         if (d <= Result) then
  93.         begin
  94.           if (d < Result) then
  95.           begin
  96.             Result := d;
  97.             r := 0;
  98.           end;
  99.           m_pts1[r] := TPA1[i1];
  100.           m_pts2[r] := TPA2[i2];
  101.           Inc(r);
  102.         end;
  103.       end;
  104.     dtpFarthest:
  105.     for i1 := 0 to h1 do
  106.       for i2 := 0 to h2 do
  107.       begin      
  108.         d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
  109.         if (d >= Result) then
  110.         begin
  111.           if (d > Result) then
  112.           begin
  113.             Result := d;
  114.             r := 0;
  115.           end;
  116.           m_pts1[r] := TPA1[i1];
  117.           m_pts2[r] := TPA2[i2];
  118.           Inc(r);
  119.         end;
  120.       end;  
  121.   end;
  122.   SetLength(m_pts1, r);
  123.   SetLength(m_pts2, r);  
  124. end;
  125.  
  126. var
  127.   t: Integer;    
  128.   mPt1, mPt2: TPoint;
  129.   TPA1, TPA2, mTPA1, mTPA2: TPointArray;
  130.  
  131. begin
  132.   ClearDebug;
  133.   TPA1 := TPAFromBox(Box(0, 0, 10, 10));
  134.   TPA2 := TPAFromBox(Box(1354, 0, 1365, 10));
  135.   t := GetSystemTime;
  136.   WriteLn('TPADistanceToTPA [closest]: ' + IntToStr(TPADistanceToTPA(TPA1, TPA2, dtpClosest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');    
  137.   t := GetSystemTime;
  138.   WriteLn('TPADistanceToTPA [farthest]: ' + IntToStr(TPADistanceToTPA(TPA1, TPA2, dtpFarthest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');  
  139.   WriteLn('');
  140.   t := GetSystemTime;
  141.   WriteLn('TPADistanceToTPAEx [closest]:' + IntToStr(TPADistanceToTPAEx(TPA1, TPA2, dtpClosest, mPt1, mPt2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  142.   WriteLn('mPt1: ' + IntToStr(mPt1.X) + ',' + IntToStr(mPt1.Y));
  143.   WriteLn('mPt2: ' + IntToStr(mPt2.X) + ',' + IntToStr(mPt2.Y));
  144.   t := GetSystemTime;
  145.   WriteLn('TPADistanceToTPAEx [farthest]: ' + IntToStr(TPADistanceToTPAEx(TPA1, TPA2, dtpFarthest, mPt1, mPt2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  146.   WriteLn('mPt1: ' + IntToStr(mPt1.X) + ',' + IntToStr(mPt1.Y));
  147.   WriteLn('mPt2: ' + IntToStr(mPt2.X) + ',' + IntToStr(mPt2.Y));
  148.   WriteLn('');
  149.   t := GetSystemTime;
  150.   WriteLn('TPADistanceToTPAEx2 [closest]: ' + IntToStr(TPADistanceToTPAEx2(TPA1, TPA2, dtpClosest, mTPA1, mTPA2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  151.   WriteLn('mTPA1: ' + TPAToStr(mTPA1));
  152.   WriteLn('mTPA2: ' + TPAToStr(mTPA2));
  153.   SetLength(mTPA1, 0);
  154.   SetLength(mTPA2, 0);
  155.   t := GetSystemTime;
  156.   WriteLn('TPADistanceToTPAEx2 [farthest]: ' + IntToStr(TPADistanceToTPAEx2(TPA1, TPA2, dtpFarthest, mTPA1, mTPA2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  157.   WriteLn('mTPA1: ' + TPAToStr(mTPA1));
  158.   WriteLn('mTPA2: ' + TPAToStr(mTPA2));
  159.   SetLength(TPA1, 0);
  160.   SetLength(TPA2, 0);      
  161.   SetLength(mTPA1, 0);
  162.   SetLength(mTPA2, 0);
  163. end.
Advertisement
Add Comment
Please, Sign In to add comment