SRL Archive - OSR Help

Sorting a T2DIntegerArray based off one of the sub values?


Xtensity

2010-07-11 00:41:05

Basically I'm taking a T2DIntegerArray of an image. The reason I don't use a T2DPointArray is because I need to store the color located at each pixel.

I'm having trouble creating a function that sorts the T2DIntegerArray; based on a specific value in each TIntegerArray;…..

Meaning, I want to make it so, all points of the same color are grouped together in the array…. So when I run through the array, it does all of the same colors at once so it isn't changing. If you still don't understand what I'm saying… I want this:

T2DIA:T2DIntegerArray;

T2DIA[0][0]:=52991
T2DIA[0][1]:=30
T2DIA[0][2]:=40
T2DIA[1][0]:=9230079
T2DIA[1][1]:=30
T2DIA[1][2]:=41
T2DIA[2][0]:=52991
T2DIA[2][1]:=30
T2DIA[2][2]:=42
T2DIA[3][0]:=9230079
T2DIA[3][1]:=30
T2DIA[3][2]:=43
T2DIA[4][0]:=52991
T2DIA[4][1]:=30
T2DIA[4][2]:=44

to be resorted like this:


T2DIA[0][0]:=52991
T2DIA[0][1]:=30
T2DIA[0][2]:=40
T2DIA[1][0]:=52991
T2DIA[1][1]:=30
T2DIA[1][2]:=42
T2DIA[2][0]:=52991
T2DIA[2][1]:=30
T2DIA[2][2]:=44
T2DIA[3][0]:=9230079
T2DIA[3][1]:=30
T2DIA[3][2]:=41
T2DIA[4][0]:=9230079
T2DIA[4][1]:=30
T2DIA[4][2]:=43


Except on a larger scale of around 40,000+ pixels… the above example only has 5 for simplicity of comprehension. I've been trying to create a function to do this for the past 7 or so hours but all my attempts have failed. One method I was thinking of was trying to integrate bubblesort into the function somehow, though it is really stumping me.

Any clues?

Frement

2010-07-11 05:59:32

I don't understand how you want to sort that.

Sir R. M8gic1an

2010-07-11 06:06:42

err… I just woke up, so I'm not quite thinking too much yet.

But my suggestion would be somewhat different to what you ask.

Make a new array where you sort these things. It'll make it so much easier on you.

~RM

Nava2

2010-07-11 09:53:29

Use a TPointArray and a TIntegerArray…

Separate them, there are algorithms in WizzyPlugin to sort these together as well.

If there isn't one, you can easily modify them so that when the TPoints/ints are moved around, you can make the other move too.

Narcle

2010-07-11 11:13:37

Function SortIntArray(IntArr: array of integer): array of array of integer;
var
i, ii, H: integer;
SkipNext: boolean;
begin
H := High(IntArr);
for i := 0 to H do
begin
SkipNext := false;
for ii := 0 to High(Result) do
if IntArr[i] = Result[ii][0] then
begin
SetArrayLength(Result[ii], Length(Result[ii])+1);
Result[ii][High(Result[ii])] := IntArr[i];
SkipNext := true;
Break;
end;
if SkipNext then
Continue;
SetArrayLength(Result, Length(Result)+1);
SetArrayLength(Result[High(Result)], 1);
Result[High(Result)][0] := IntArr[i];
end;
H := High(Result);
for i := 0 to H do
for ii := 0 to H do
if High(Result[i]) > High(Result[ii]) then
IntArrSwap(Result[i], Result[ii]);
end;

Old function of mine. I never got around to optimizing it. Its kinda slow.

Also if you want to keep the TPoint with the color I'd make a record for it.


type
TPointInt = Record
Point: TPoint;
Color: integer;
end;

Something like that.

Xtensity

2010-07-11 17:00:31

Hm, interesting, will look into types and records; completely forgot about those.