Janilabo

Janilabo | TPADistanceToPt(Ex(2))

Nov 4th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.79 KB | None | 0 0
  1. function TPADistanceToPt(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest)): Integer;
  2. var
  3.   h, i, d: Integer;
  4. begin
  5.   Result := -1;
  6.   h := High(TPA);  
  7.   if (h < 0) then
  8.     Exit;      
  9.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  10.   if (h > 0) then
  11.   case method of
  12.     dtpClosest:
  13.     for i := 1 to h do
  14.     begin      
  15.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  16.       if (d < Result) then
  17.         Result := d;
  18.     end;
  19.     dtpFarthest:
  20.     for i := 1 to h do
  21.     begin      
  22.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  23.       if (d > Result) then
  24.         Result := d;
  25.     end;    
  26.   end;
  27. end;
  28.  
  29. function TPADistanceToPtEx(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest); var m_pt: TPoint): Integer;
  30. var
  31.   h, i, d: Integer;
  32. begin
  33.   Result := -1;
  34.   h := High(TPA);  
  35.   if (h < 0) then
  36.     Exit;      
  37.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  38.   if (h > 0) then
  39.   case method of
  40.     dtpClosest:
  41.     for i := 1 to h do
  42.     begin      
  43.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  44.       if (d < Result) then
  45.       begin
  46.         Result := d;
  47.         m_pt := TPA[i];
  48.       end;
  49.     end;
  50.     dtpFarthest:
  51.     for i := 1 to h do
  52.     begin      
  53.       d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  54.       if (d > Result) then
  55.       begin
  56.         Result := d;
  57.         m_pt := TPA[i];
  58.       end;
  59.     end;    
  60.   end;
  61. end;
  62.  
  63. function TPADistanceToPtEx2(TPA: TPointArray; p: TPoint; method: (dtpClosest, dtpFarthest); var m_pts: TPointArray): Integer;
  64. var
  65.   h, i, d, r: Integer;
  66. begin
  67.   Result := -1;
  68.   h := High(TPA);  
  69.   if (h < 0) then
  70.     Exit;      
  71.   Result := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  72.   SetLength(m_pts, (h + 1));
  73.   m_pts[i] := TPA[i];
  74.   if (h > 0) then
  75.   begin
  76.     case method of
  77.       dtpClosest:
  78.       for i := 1 to h do
  79.       begin      
  80.         d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  81.         if (d <= Result) then
  82.         begin
  83.           if (d < Result) then
  84.           begin
  85.             Result := d;
  86.             r := 0;
  87.           end;
  88.           m_pts[r] := TPA[i];
  89.           Inc(r);
  90.         end;
  91.       end;
  92.       dtpFarthest:
  93.       for i := 1 to h do
  94.       begin      
  95.         d := Distance(TPA[i].X, TPA[i].Y, p.X, p.Y);
  96.         if (d >= Result) then
  97.         begin  
  98.           if (d > Result) then
  99.           begin
  100.             Result := d;
  101.             r := 0;
  102.           end;
  103.           m_pts[r] := TPA[i];
  104.           Inc(r);
  105.         end;
  106.       end;    
  107.     end;
  108.     SetLength(m_pts, r);  
  109.   end;
  110. end;
  111.  
  112. var
  113.   t: Integer;    
  114.   cPt, mPt: TPoint;
  115.   TPA, mTPA: TPointArray;
  116.  
  117. begin
  118.   ClearDebug;
  119.   TPA := TPAFromBox(Box(0, 0, 250, 250));
  120.   cPt := TPACenter(TPA);
  121.   TPARemove(TPA, cPt);  
  122.   t := GetSystemTime;
  123.   WriteLn('TPADistanceToPt [closest]: ' + IntToStr(TPADistanceToPt(TPA, cPt, dtpClosest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');    
  124.   t := GetSystemTime;
  125.   WriteLn('TPADistanceToPt [farthest]: ' + IntToStr(TPADistanceToPt(TPA, cPt, dtpFarthest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');  
  126.   t := GetSystemTime;
  127.   WriteLn('TPADistanceToPtEx [closest]:' + IntToStr(TPADistanceToPtEx(TPA, cPt, dtpClosest, mPt)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  128.   t := GetSystemTime;
  129.   WriteLn('TPADistanceToPtEx [farthest]: ' + IntToStr(TPADistanceToPtEx(TPA, cPt, dtpFarthest, mPt)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  130.   t := GetSystemTime;
  131.   WriteLn('TPADistanceToPtEx2 [closest]: ' + IntToStr(TPADistanceToPtEx2(TPA, cPt, dtpClosest, mTPA)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  132.   t := GetSystemTime;
  133.   WriteLn('TPADistanceToPtEx2 [farthest]: ' + IntToStr(TPADistanceToPtEx2(TPA, cPt, dtpFarthest, mTPA)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
  134.   SetLength(TPA, 0);
  135.   SetLength(mTPA, 0);
  136. end.
Advertisement
Add Comment
Please, Sign In to add comment