SCAR Code:
program New;
{.include SRL/SRL.scar}
function FindEdges(P: TPointArray): TPointArray;
var
b: array of array of Boolean;
i, x, y, l, c: Integer;
Box: TBox;
begin
SetLength(Result, 0);
l := Length(p);
Box := GetTPABounds(p);
x := (Box.x2 - Box.x1) + 3;
y := (Box.y2 - Box.y1) + 3;
SetLength(b, x);
for i := 0 to x -1 do
SetLength(b[i], y);
for i := 0 to l -1 do
b[p[i].x +1 - Box.x1][p[i].y +1 - Box.y1] := True;
SetLength(Result, l);
c := 0;
for i := 0 to l -1 do
for x := -1 to 1 do
for y := -1 to 1 do
try
if not b[p[i].x + 1 + x - Box.x1][p[i].y + 1 + y - Box.y1] then
begin
Result[c] := p[i];
Inc(c);
x := 2;
Break;
end;
except end;
SetLength(Result, c);
end;
procedure QuickSortExtendedTPA(var A: TExtendedArray; var B: TPointArray; iLo, iHi: Integer) ;
var
Lo, Hi: Integer;
P: TPoint;
T, M: Extended;
begin
Lo := iLo;
Hi := iHi;
M := A[(Lo + Hi) shr 1];
repeat
while (A[Lo] < M) do Inc(Lo);
while (A[Hi] > M) do Dec(Hi);
if (Lo <= Hi) then
begin
T := A[Lo];
P := B[Lo];
A[Lo] := A[Hi];
B[Lo] := B[Hi];
A[Hi] := T;
B[Hi] := P;
Inc(Lo);
Dec(Hi);
end;
until (Lo > Hi);
if (Hi > iLo) then QuickSortExtendedTPA(A, B, iLo, Hi);
if (Lo < iHi) then QuickSortExtendedTPA(A, B, Lo, iHi);
end;
function EdgeRoadWalkEx(var rx, ry: Integer; Color, Tolerance, Degree: Integer; SameRoad: Boolean): Boolean;
var
p, c: TPointArray;
q, z: T2DPointArray;
i, ii, l, ll, Dist, cc, ec: Integer;
d: TExtendedArray;
begin
FindColorsTolerance(p, Color, MMX1, MMY1, MMX2, MMY2, Tolerance);
Degree := Degree mod 360;
q := SplitTPAEx(p, 2, 2);
SetLength(p, Length(q));
ll := 0;
for ii := 0 to High(q) do
begin
if (Length(q[ii]) < 200) then Continue;
c := FindEdges(q[ii]);
l := Length(c);
//DebugTPA(c);
//Wait(3000);
cc := 0;
ec := 0;
for i := 0 to l -1 do
begin
Dist := Distance(MMCX, MMCY, c[i].x, c[i].y);
//WriteLn(IntTostr(Dist));
if (Dist < 4) then
Inc(cc);
if (Dist < 65) then
c[i] := Point(-1, -1)
else
Inc(ec);
end;
//WriteLn(IntToStr(ec)+', '+IntToStr(cc));
if (ec > 5) and (((cc > 5) and SameRoad) or (not SameRoad)) then
begin
z := SplitTPAEx(c, 3, 3);
for ec := 0 to High(z) do
if (Length(z[ec]) > 6) and (z[ec][0].x > -1) then
begin
SetLength(p, ll + 1);
p[ll] := MiddleTPA(z[ec]);
Inc(ll);
end;
end;
end;
if (ll = 0) then Exit;
if (ll > 1) then
begin
SetLength(p, ll);
SetLength(d, ll);
for i := 0 to ll -1 do
begin
d[i] := Degrees(ArcTan2(p[i].y - MMCY, p[i].x - MMCX) + 1.570796);
while (d[i] < 0.0) do
d[i] := d[i] + 360.0;
d[i] := MinE(Abs(Degree - d[i]), MinE(Abs((Degree + 360) - d[i]), Abs(Degree - (d[i] + 360))));
end;
QuickSortExtendedTPA(d, p, 0, ll -1);
end;
rx := p[0].x;
ry := p[0].y;
SetLength(d, 1);
d[0] := Degrees(ArcTan2(ry - MMCY, rx - MMCX) + 1.570796);
while (d[0] < 0.0) do
d[0] := d[0] + 360.0;
Result := (MinE(Abs(Degree - d[0]), MinE(Abs((Degree + 360) - d[0]), Abs(Degree - (d[0] + 360)))) < 30.0);
end;
function EdgeRoadWalk(Color, Degree: Integer; SameRoad: Boolean; xMod, yMod: Integer): Boolean;
var
x, y: Integer;
begin
if EdgeRoadWalkEx(x, y, Color, 0, Degree, SameRoad) then
begin
MFNF(x, y, xMod, yMod);
wait(100);
FFlag(10);
Result := True;
end;
end;
begin
SetupSRL;
RoadColor := FindRoadColor;
WriteLn(BoolToStr(EdgeRoadWalk(RoadColor, 90, True, 0, 1)));
end.