SRL Archive - SRL Archives

SortTPADist


Special Ed

2008-04-17 22:24:50

Sorts TPA by it's Distance from (Cx, Cy). It's got a wide range of uses, I developed this with the intention of finding the closest rock to your RS character but I've envisioned many more possibilities, and I'm sure the great minds of SRL will think of even more!


procedure SortTPADist(var TPA: TPointArray; Cx, Cy: Integer);
var
ID: TPointArray;
i, ArrL: Byte;
begin
ArrL:= GetArrayLength(TPA);
SetArrayLength(ID, ArrL);
for i:= 0 to ArrL - 1 do
begin
ID[i].x:= Distance(Cx, Cy, TPA[i].x, TPA[i].y);
ID[i].y:= i;
end;
ID:= RearrangeTPA(ID, 0, ArrL, True, False);
for i:= 0 to ArrL - 1 do TPA[i]:= TPA[ID[ArrL - 1 - i].y];
end;

From what I've seen there isn't anything like it… there are things that are similar but nothing I've found can do this. Although, I'm pretty unobservant so who knows :p

bullzeye95

2008-04-18 00:05:17

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.
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 ;))

Santa_Clause

2008-04-18 01:53:36

SortTPAFrom…?

n3ss3s

2008-04-18 07:20:21

WizzyPlugin's End User "from" sorting functions:


// * procedure SortTPAFrom(var a: TPointArray; const From: TPoint);
// * procedure SortATPAFrom(var a: T2DPointArray; const From: TPoint);
// * procedure SortATPAFromFirstPoint(var a: T2DPointArray; const From: TPoint);


Yes, it has a wide range of uses, the better something sounds the more likely it's already made =]

bullzeye95

2008-04-18 07:41:10

From: n3ss3s
WizzyPlugin's End User "from" sorting functions:



Yes, it has a wide range of uses, the better something sounds the more likely it's already made =]


I knew it sounded really familiar… but I just assumed that since he made it, that it must just be in one of the other few plugins I've managed to get my hands on.
:redface:

Special Ed

2008-04-18 15:52:45

Ahh… I had looked through wizzups plugin and hadn't seen those. Like I said, I tend to miss things :)

I would like to know though, why wasn't it working for you bullzeye? What did it return?

bullzeye95

2008-04-18 16:24:50

It seemed that it didn't sort it at all.

Special Ed

2008-04-18 21:32:57

From: bullzeye95
It seemed that it didn't sort it at all.


Hmmm… works / worked fine for me… Lemme test it again.

EDIT: Huh… weird… It doesn't work for me either now. I know it was working fine yesterday because I was testing in on rocks. Anything you see that is wrong?

bullzeye95

2008-04-18 22:15:00

Well, I see that RearrangeTPA is a function that RETURNS a new TPA, instead of EDITING the passed TPA =]

Boreas

2008-04-18 23:06:52

I'm weird that way :p

Special Ed

2008-04-19 12:12:47

Ahhh… When I tested it the rocks must've just been found in the correct order so my procedure did nothing… but it looked like it did. Thanks for the fix! Jeeze Boreas you made it like that on purpose didn't you? Just to throw me off :p

n3ss3s

2008-04-19 12:21:21

From: Special Ed
Ahhh… When I tested it the rocks must've just been found in the correct order so my procedure did nothing… but it looked like it did. Thanks for the fix! Jeeze Boreas you made it like that on purpose didn't you? Just to throw me off :p


I don't doubt a bit that you didn't use FindColorsSpiralTolerance =D

Special Ed

2008-04-19 13:02:14

It wasn't using FindColorsSpiralTolerance. I was using Benlands "FindRock" procedure. Which for some reason will return doubles of some rocks and doesn't always return the rocks in order of distance. Although it may be because the colors need updating… regardless it was still needed.