function TSearch.CanConnect(pt1, pt2: TPoint): Boolean;
begin //判断是否可以连接(共3种情况)
Result := CanLine(pt1, pt2) or CanLineOneCorner(pt1, pt2) or CanLineTwoCorner(pt1, pt2);
end;
function TSearch.CanLine(pt1, pt2: TPoint): Boolean;
var //测试2点是否可以直接连接(中间无障碍)
i: Integer;
pt: TPoint;
begin
Result := False;
if pt1.X = pt2.X then
begin
if pt1.Y > pt2.Y then
begin
pt.Y := pt1.Y;
pt1.Y := pt2.Y;
pt2.Y := pt.Y;
end;
for i := pt1.Y +1 to pt2.Y -1 do
begin
if Maps[pt1.X,i] <> -1 then
exit;
end;
Result := True;
exit;
end;
if pt1.Y = pt2.Y then
begin
if pt1.X > pt2.X then
begin
pt.X := pt1.X;
pt1.X := pt2.X;
pt2.X := pt.X;
end;
for i := pt1.X +1 to pt2.X -1 do
begin
if Maps[i,pt2.Y] <> -1 then
exit;
end;
Result := True;
exit;
end;
end;
function TSearch.CanLineOneCorner(pt1, pt2: TPoint): Boolean;
var //判断2点是否由2条直线连接(一个拐点)
pt: TPoint;
begin
Result := False;
pt.X := pt1.X;
pt.Y := pt2.Y; //拐点坐标(左下拐点)
if (Maps[pt.X, pt.Y] = -1) and CanLine(pt,pt1) and CanLine(pt,pt2) then
begin //
Result := True;
exit;
end;
pt.X := pt2.X;
pt.Y := pt1.Y; //拐点坐标(右上拐点)
if (Maps[pt.X, pt.Y] = -1) and CanLine(pt,pt1) and CanLine(pt,pt2) then
begin
Result := True;
exit;
end;
end;
function TSearch.CanLineTwoCorner(pt1, pt2: TPoint): Boolean;
var //判断2点是否由3条直线连接(2个拐点)
pt: TPoint;
i: Integer;
begin
Result := False;
pt.X := pt1.X;
for i := 0 to iVCount - 1 do
begin //由第1个点开始向下寻找
pt.Y := i;
if (Maps[pt.X, pt.Y] = -1) and CanLine(pt,pt1) and CanLineOneCorner(pt,pt2) then
begin //如果临时点可以经过1条或者2条直线连接就TRUE
Result := True;
exit;
end;
end;
pt.Y := pt1.Y;
for i := 0 to iHCount - 1 do
begin //由第1个点开始向右寻找
pt.X := i;
if (Maps[pt.X, pt.Y] = -1) and CanLine(pt,pt1) and CanLineOneCorner(pt,pt2) then
begin //如果临时点可以经过1条或者2条直线连接就TRUE
Result := True;
exit;
end;
end;
end;