Uhm, it doesn't seem to work for me =/
So... I got bored and made one with quicksort, if you want to look at that.
SCAR Code:
procedure QuickSortTPADist(var A: TPointArray; cx, cy, iLo, iHi: Integer);
var
Lo, Hi, Pivot, N: Integer;
T: TPoint;
begin
Lo := iLo;
Hi := iHi;
N:= (Lo + Hi) div 2;
Pivot := Distance(cx, cy, A[N].x, A[N].y);
repeat
while Distance(cx, cy, A[Lo].x, A[Lo].y) < Pivot do Inc(Lo);
while Distance(cx, cy, A[Hi].x, A[Hi].y) > Pivot do Dec(Hi);
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSortTPADist(A, cx, cy, iLo, Hi) ;
if Lo < iHi then QuickSortTPADist(A, cx, cy, Lo, iHi) ;
end;
(not a planned hijack, I just can't stand to leave things nonfunctional
)