SRL Archive - OSR Help

Removing a point from a TPA if it matches a color


TomTuff

2011-01-23 17:58:46

I'm trying to sort through a TPA and remove a point if it matches a color within a certain tolerance. I tried doing it manually with a few loops but that didn't work, and I don't see anything in the wizzyplugin that would help me out. Any ideas?

Feroc1ty

2011-01-23 18:11:07

n = integer
for i := 0 to high(array) do
begin
if not array.color is somehow(within tolerance) then n = n + 1;
newarray[n] = array
end


something to that extent if it made sense, just have a loop with another integer in it that will have index for a new array which will replace the old one

mormonman

2011-01-23 18:22:00

function RemoveLikeColorsTPA(Color, Tolerance: Integer; TPA: TPointArray): TPointArray;
var
NewArray: TPointArray;
i, Index, Hi, Color1: Integer;
begin
if Length(TPA) < 1 then Exit;
SetLength(NewArray, Length(TPA));
Hi := High(TPA);
Index := 0;

for i := 0 to Hi do
begin
Color1 := GetColor(TPA[i].x, TPA[i].y);
if not SimilarColors(Color1, Color, Tolerance) then
begin
NewArray[Index] := TPA[i];
Inc(Index);
end;
end;
SetLength(NewArray, Index);
Result := NewArray;
end;

Edit: read the first post wrong.

Sex

2011-01-23 18:38:05

function RemoveLikeColorsTPA(Color, Tolerance: Integer; TPA: TPointArray): TPointArray;
var
NewArray: TPointArray;
i, Index, h, Color1: Integer;
begin
if Length(TPA) < 1 then
exit;
h := High(TPA);

for i := 0 to h do
begin
:= GetColor(TPA[i].x, TPA[i].y);
if SimilarColors(Color, GetColor(TPA[i].x, TPA[i].y), Tolerance) then
begin
swap(TPA[High(TPA)], TPA[i]);
SetArrayLength(TPA, GetArrayLength(TPA - 1));
end;
end;
Result := TPA;
end;
Should work?

Nava2

2011-01-23 19:46:01

function RemoveLikeColorsTPA(Color, Tolerance: Integer; TPA: TPointArray): TPointArray;
var
cols: TIntegerArray;
i, h, L: Integer;
begin
if Length(TPA) < 1 then
exit;
h := High(TPA);
SetLength(Result, h+1);

cols := getColors(TPA);

L := 0;
for i := 0 to h do
begin
if (not SimilarColors(Color, cols[i], Tolerance)) then
begin
// if the colour is not similar, add the index into the array.
Result[L] := TPA[i];
Inc(L);
end;

SetLength(Result, L);
end;

Should be significantly faster then others posted.

Only grabs the client ONCE, rather than every time. Also, it does not do memory management every time a colour is valid, instead it only allocates then copies it over once.

Wanted

2011-01-23 20:07:23

To use this practically you'd have to do it like this:

{.include SRL/SRL.scar}
{.include SRL/SRL/misc/debug.scar}

type
TColorPointArray = record
Points: TPointArray;
Colors: TIntegerArray;
end;

function FindTCPA(var TCPA: TColorPointArray; Color, xs, ys, xe, ye, Tol: Integer): Boolean;
begin
Result := FindColorsTolerance(TCPA.Points, Color, xs, ys, xe, ye, Tol);
if (not (Result)) then
Exit;
TCPA.Colors := GetColors(TCPA.Points);
end;

function RemoveColorsInTPA(TCPA: TColorPointArray; Color: LongInt): TColorPointArray;
var
H, I, L: LongInt;
begin
H := High(TCPA);
for I := 0 to H do
if (TCPA.Colors[I] <> Color) then
begin
Inc(L);
SetLength(Result, L);
Result.Points[L - 1] := TCPA.Points[I];
Result.Colors[L - 1] := TCPA.Colors[I];
end;
end;

function RemoveColorsInTPATol(TCPA: TColorPointArray; Color, Tol: LongInt): TColorPointArray;
var
H, I, L: LongInt;
begin
H := High(TCPA);
for I := 0 to H do
if (not (SimilarColors(TCPA.Colors[I], Color, Tol))) then
begin
Inc(L);
SetLength(Result, L);
Result.Points[L - 1] := TCPA.Points[I];
Result.Colors[L - 1] := TCPA.Colors[I];
end;
end;

var
TCPA: TColorPointArray;

begin
SetUpSRL;
ActivateClient;
ColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.2, 0.2);
FindTCPA(TCPA, 12344556, MSX1, MSY1, MSX2, MSY2, 5);
RemoveColorsInTPATol(TCPA, 1235932, 5);
SetColorspeed2Modifiers(0.2, 0.2);
ColorToleranceSpeed(1);
DebugTPA(TCPA.Points, '');
end.

Nava2

2011-01-23 20:47:50

From: IceFire908
To use this practically you'd have to do it like this:

//..snip


Why? o.O What was wrong with my solution?

Wanted

2011-01-23 20:52:07

From: Nava2
Why? o.O What was wrong with my solution?


Well you ninja'd me technically.

But really a TPA is only a recording of points, if you take the same points and apply it to a TPA of a different time period in coordination with getcolors you're not working with the original colors, it's important to record the colors at the same time you record the points, hence the purpose of TColorPointArray and FindTCPA.

TomTuff

2011-01-23 21:04:08

From: IceFire908
Well you ninja'd me technically.

But really a TPA is only a recording of points, if you take the same points and apply it to a TPA of a different time period in coordination with getcolors you're not working with the original colors, it's important to record the colors at the same time you record the points, hence the purpose of TColorPointArray and FindTCPA.


This is true, but for the purpose I would be using it for I would using it for, the objects (bankers) don't move. So, under normal circumstances, yes yours would be better, but I'll probably use one of the other, less script-bloating solutions.


Edit:
Also, I meant to post this before I left but i guess i missed the "submit reply" button :p


function FilterPointsColorTolerance(Color, Tolerance: LongInt; TPA: TPointArray): TPointArray;
var
i, j, x, y: Integer;
begin
for i := 0 to High(TPA) do
if not(FindColorTolerance(x, y, Color, TPA[i].x, TPA[i].y, TPA[i].x, TPA[i].y, Tolerance)) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := TPA[i];
end;
end;

Sir R. M8gic1an

2011-01-24 00:23:23

Tom, yours really is much slower and if the TPA is big it'll lag.

+ rep to Nava & Ice

~RM