SRL Archive - Suggestions

SortATPAByBoundaries


maigel

2012-01-24 22:50:55

Hi,

So recently I was working on a script that uses TPAs. I followed this tutorial to help my out a bit:
http://villavu.com/forum/showthread.php?t=49067

Now I needed a way to sort my ATPA array by the actual surface area the boxes had instead of the amount of matched pixels there were in the box. It turned out there wasn't any function to do this so I made one myself and I tought it might come in handy for others too ;)


(*
Description: Sorts an ATPA array by its boundaries' surface area (sort by size sorts by the amount of dots)
*)
procedure SortATPAByBoundaries( atpa : T2DPointArray);
var
i, j, maxIndex : Integer;
box1, box2 : TBox;
temp : TPointArray;
begin
for i := 0 to High(atpa) do
begin
maxIndex := i;
for j := i + 1 to High(atpa) do
begin
box1 := GetTPABounds(atpa[j]);
box2 := GetTPABounds(atpa[maxIndex]);
if( (box1.X2 - box1.X1) * (box1.Y2 - box1.Y1) > (box2.X2 - box2.X1) * (box2.Y2 - box2.Y1) ) then
maxIndex := j;
end;
temp := atpa[i];
atpa[i] := atpa[maxIndex];
atpa[maxIndex] := temp;
end;
end;

mormonman

2012-01-24 23:39:07

Nice…but there are built in functions that are fast and easy :)

procedure SortATPAByBoundaries(ATPA : T2DPointArray; BigFirst: Boolean);
var
baArr: TIntegerArray;
bounds: TBox;
i, Len, l, w: Integer;
begin
Len := Length(ATPA);
if (Len < 1) then Exit;

SetLength(baArr, Len);
for i := 0 to (Len - 1) do
begin
bounds := GetTPABounds(ATPA[i]);
l := (bounds.X2 - bounds.X1); w := (bounds.Y2 - bounds.Y1);
baArr[i] := (l * w);
end;
QuickATPASort(baArr, ATPA, 0, Len-1, not BigFirst);
end;

maigel

2012-01-25 04:52:33

I see … Thanks for clearing that out :D

Coh3n

2012-03-06 20:03:20

This is a function that would be suited better for Simba (since that's where all the other TPA functions are). Moving this to Simba suggestions.