Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TPADistanceToTPA(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest)): Integer;
- var
- h1, h2, i1, i2, d: Integer;
- begin
- Result := -1;
- h1 := High(TPA1);
- h2 := High(TPA2);
- if ((h1 < 0) or (h2 < 0)) then
- Exit;
- Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
- case method of
- dtpClosest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d < Result) then
- Result := d;
- end;
- dtpFarthest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d > Result) then
- Result := d;
- end;
- end;
- end;
- function TPADistanceToTPAEx(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pt1, m_pt2: TPoint): Integer;
- var
- h1, i1, h2, i2, d: Integer;
- begin
- Result := -1;
- h1 := High(TPA1);
- h2 := High(TPA2);
- if ((h1 < 0) or (h2 < 0)) then
- Exit;
- Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
- m_pt1 := TPA1[0];
- m_pt2 := TPA2[0];
- case method of
- dtpClosest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d < Result) then
- begin
- Result := d;
- m_pt1 := TPA1[i1];
- m_pt2 := TPA2[i2];
- end;
- end;
- dtpFarthest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d > Result) then
- begin
- Result := d;
- m_pt1 := TPA1[i1];
- m_pt2 := TPA2[i2];
- end;
- end;
- end;
- end;
- function TPADistanceToTPAEx2(TPA1, TPA2: TPointArray; method: (dtpClosest, dtpFarthest); var m_pts1, m_pts2: TPointArray): Integer;
- var
- h1, i1, h2, i2, d, r, m: Integer;
- begin
- Result := -1;
- h1 := High(TPA1);
- h2 := High(TPA2);
- if ((h1 < 0) or (h2 < 0)) then
- Exit;
- Result := Distance(TPA1[0].X, TPA1[0].Y, TPA2[0].X, TPA2[0].Y);
- m := Max(h1, h2);
- SetLength(m_pts1, (m + 1));
- SetLength(m_pts2, (m + 1));
- m_pts1[0] := TPA1[0];
- m_pts2[0] := TPA2[0];
- case method of
- dtpClosest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d <= Result) then
- begin
- if (d < Result) then
- begin
- Result := d;
- r := 0;
- end;
- m_pts1[r] := TPA1[i1];
- m_pts2[r] := TPA2[i2];
- Inc(r);
- end;
- end;
- dtpFarthest:
- for i1 := 0 to h1 do
- for i2 := 0 to h2 do
- begin
- d := Distance(TPA1[i1].X, TPA1[i1].Y, TPA2[i2].X, TPA2[i2].Y);
- if (d >= Result) then
- begin
- if (d > Result) then
- begin
- Result := d;
- r := 0;
- end;
- m_pts1[r] := TPA1[i1];
- m_pts2[r] := TPA2[i2];
- Inc(r);
- end;
- end;
- end;
- SetLength(m_pts1, r);
- SetLength(m_pts2, r);
- end;
- var
- t: Integer;
- mPt1, mPt2: TPoint;
- TPA1, TPA2, mTPA1, mTPA2: TPointArray;
- begin
- ClearDebug;
- TPA1 := TPAFromBox(Box(0, 0, 10, 10));
- TPA2 := TPAFromBox(Box(1354, 0, 1365, 10));
- t := GetSystemTime;
- WriteLn('TPADistanceToTPA [closest]: ' + IntToStr(TPADistanceToTPA(TPA1, TPA2, dtpClosest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- t := GetSystemTime;
- WriteLn('TPADistanceToTPA [farthest]: ' + IntToStr(TPADistanceToTPA(TPA1, TPA2, dtpFarthest)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- WriteLn('');
- t := GetSystemTime;
- WriteLn('TPADistanceToTPAEx [closest]:' + IntToStr(TPADistanceToTPAEx(TPA1, TPA2, dtpClosest, mPt1, mPt2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- WriteLn('mPt1: ' + IntToStr(mPt1.X) + ',' + IntToStr(mPt1.Y));
- WriteLn('mPt2: ' + IntToStr(mPt2.X) + ',' + IntToStr(mPt2.Y));
- t := GetSystemTime;
- WriteLn('TPADistanceToTPAEx [farthest]: ' + IntToStr(TPADistanceToTPAEx(TPA1, TPA2, dtpFarthest, mPt1, mPt2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- WriteLn('mPt1: ' + IntToStr(mPt1.X) + ',' + IntToStr(mPt1.Y));
- WriteLn('mPt2: ' + IntToStr(mPt2.X) + ',' + IntToStr(mPt2.Y));
- WriteLn('');
- t := GetSystemTime;
- WriteLn('TPADistanceToTPAEx2 [closest]: ' + IntToStr(TPADistanceToTPAEx2(TPA1, TPA2, dtpClosest, mTPA1, mTPA2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- WriteLn('mTPA1: ' + TPAToStr(mTPA1));
- WriteLn('mTPA2: ' + TPAToStr(mTPA2));
- SetLength(mTPA1, 0);
- SetLength(mTPA2, 0);
- t := GetSystemTime;
- WriteLn('TPADistanceToTPAEx2 [farthest]: ' + IntToStr(TPADistanceToTPAEx2(TPA1, TPA2, dtpFarthest, mTPA1, mTPA2)) + ' - ' + IntToStr(GetSystemTime - t) + ' ms.');
- WriteLn('mTPA1: ' + TPAToStr(mTPA1));
- WriteLn('mTPA2: ' + TPAToStr(mTPA2));
- SetLength(TPA1, 0);
- SetLength(TPA2, 0);
- SetLength(mTPA1, 0);
- SetLength(mTPA2, 0);
- end.
Advertisement
Add Comment
Please, Sign In to add comment