Sabzi
2009-08-31 14:46:35
type
TPAEx = record
Width, Height, AreaSize, Radius, Length: Integer;
MidPoint: TPoint;
end;
function GetTPAEx(var TPA: TPointArray): TPAEx;
var
B: TBox;
x, y, L: Integer;
T: TPoint;
begin
B := GetTPABounds(TPA);
MiddleTPAEx(TPA, x, y);
SortTPAFrom(TPA, Point(x, y));
T := TPA[High(TPA)];
L := Length(TPA);
with Result do
begin
Width := B.x2 - B.x1;
Height := B.y2 - B.y1;
AreaSize := Result.Width * Result.Height;
Radius := Distance(x, y, T.x, T.y);
Midpoint := Point(x, y);
Length := L;
end;
end;
function FilterATPABy(by: string; var ATPA: T2DPointArray; Length, AreaSize, Width, Height, Radius: TIntegerArray): T2DPointArray;
var
H, I, O: Integer;
TE: array of TPAEx;
begin
H := High(ATPA);
SetArrayLength(TE, H + 1);
for I := 0 to H do
TE[I] := GetTPAEx(ATPA[I]);
case Lowercase(by) of
'length':
begin
for I := 0 to H do
begin
if ((TE[I].Length < Length[0]) or (TE[I].Length > Length[1])) then Continue;
SetLength(Result, O + 1);
Result[O] := ATPA[I];
Inc(O);
end;
end;
'wandh':
begin
for I := 0 to H do
begin
if ((TE[I].Width < Width[0]) or (TE[I].Width > Width[1]) or (TE[I].Height < Height[0]) or (TE[I].Height > Height[1])) then Continue;
SetLength(Result, O + 1);
Result[O] := ATPA[I];
Inc(O);
end;
end;
'areasize':
begin
for I := 0 to H do
begin
if ((TE[I].AreaSize < AreaSize[0]) or (TE[I].AreaSize > AreaSize[1])) then Continue;
SetLength(Result, O + 1);
Result[O] := ATPA[I];
Inc(O);
end;
end;
'radius':
begin
for I := 0 to H do
begin
if ((TE[I].Radius < Radius[0]) or (TE[I].Radius > Radius[1])) then Continue;
SetLength(Result, O + 1);
Result[O] := ATPA[I];
Inc(O);
end;
end;
else WriteLn{srlwarnthingy}('you give a baaaad by!');
end;
end;
I did not know to place it here ot the the test corner so move it if you think this fits there more.
Credit to caused because my brain started when I tried to help him and failed due to tiredness. But I have tested these and they are working!
Pretty self explanatory only thing you might not understand the TIntegerArrays in FilterATPABy. First integer in the array is the minimum value second is the maximum one.
I can't write descriptions and can't find good names.(open to these)
Also I must mention that I made these mainly because I want to understand TPAs and ATPAs more. So these might be useless and not effective or just simply bad. But it's always fun playing with TPAs and ATPAs for me.
You will get runtime error for example when you filter by areasize but gives the radius values so be careful with those.