Narcle
2008-09-23 19:25:38
Can anyone post for me the SortTPAFrom source? I need it for basis for new TTile sort.
2008-09-23 19:25:38
2008-09-23 19:31:47
procedure SortTPAFrom(var a: TPointArray; const From: TPoint); stdcall;
var
i, l: Integer;
DistArr: TIntegerArray;
begin
l := High(a);
if (l < 0) then Exit;
SetLength(DistArr, l + 1);
for i := 0 to l do
DistArr[i] := Round(Sqr(From.x - a[i].x) + Sqr(From.y - a[i].y));
QuickTPASort(DistArr, a, 0, l, True);
end;
procedure QuickTPASort(var A: TIntegerArray; var B: TPointArray; iLo, iHi: Integer; SortUp: Boolean);
var
Lo, Hi, Mid, T: Integer;
TP: TPoint;
begin
if (Length(A) <> Length(B)) then Exit;
Lo := iLo;
Hi := iHi;
Mid := A[(Lo + Hi) shr 1];
repeat
if SortUp then
begin
while (A[Lo] < Mid) do Inc(Lo);
while (A[Hi] > Mid) do Dec(Hi);
end else
begin
while (A[Lo] > Mid) do Inc(Lo);
while (A[Hi] < Mid) do Dec(Hi);
end;
if (Lo <= Hi) then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
TP := B[Lo];
B[Lo] := B[Hi];
B[Hi] := TP;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if (Hi > iLo) then QuickTPASort(A, B, iLo, Hi, SortUp);
if (Lo < iHi) then QuickTPASort(A, B, Lo, iHi, SortUp);
end;