procedure FillFloodMaps(x,y,dist:integer; fillHeight:single);
type
TCheckedDirections = array [0..7] of boolean;
TCheckCoordinate = record
Directions: TCheckedDirections;
x: integer;
y: integer;
end;
TPixelList = array of TCheckCoordinate;
var
PixelList: TPixelList;
DEM_NoDataValue,DEM_Height: single;
i:integer;
removePixel: boolean;
procedure IncPixelList(Ax,Ay,AOrigin:integer);
var Fi:integer;
begin
setLength(PixelList, length(PixelList)+1);
for Fi:=0 to 7 do
begin
if Fi=AOrigin then {Set origin direction to true so we don't go backwards}
PixelList[high(PixelList)].Directions[Fi]:=true
else
PixelList[high(PixelList)].Directions[Fi]:=false;
end;
PixelList[high(PixelList)].x:=Ax;
PixelList[high(PixelList)].y:=Ay;
end;
procedure DecPixelList;
begin
setLength(PixelList, length(PixelList)-1);
end;
function CheckPixel(Ax,Ay,Ai:integer):boolean;
begin
result:=false;
//Set the coordinates to check
case Ai of
0: begin Ax:=Ax+1; end;
1: begin Ax:=Ax+1; Ay:=Ay+1; end;
2: begin Ay:=Ay+1; end;
3: begin Ax:=Ax-1; Ay:=Ay+1; end;
4: begin Ax:=Ax-1; end;
5: begin Ax:=Ax-1; Ay:=Ay-1; end;
6: begin Ay:=Ay-1; end;
7: begin Ax:=Ax+1; Ay:=Ay-1; end;
end;
//If the coordinates are within bounds and the FloodMaps can be drawn
//add the pixel to the PixelList.
if (Ax>=0) AND (Ax<Flood.Width) AND (Ay>=0) AND (Ay<Flood.Height) then
begin
if FlowMap[Ax,Ay] >= dist then
if FloodHeightMap[Ax,Ay] < fillHeight then
begin
DEM.GetPixel(Ax,Ay,single(DEM_Height));
if (DEM_Height<>DEM_NoDataValue) AND (DEM_Height<fillHeight) then
begin
FloodHeightMap[Ax,Ay]:=fillHeight;
FloodDepthMap[Ax,Ay]:=fillHeight-DEM_Height;
IncPixelList(Ax,Ay,(Ai+4) mod 8);
result:=true;
end;
end;
end;
end;
begin
DEM.GetNoDataValue(single(DEM_NoDataValue));
setLength(PixelList,0);
IncPixelList(x,y,-1);
while length(PixelList)>0 do
begin
x:=PixelList[high(PixelList)].x;
y:=PixelList[high(PixelList)].y;
removePixel:=true;
for i:=0 to 7 do
begin
if PixelList[high(PixelList)].Directions[i]=false then
begin
PixelList[high(PixelList)].Directions[i]:=true;
removePixel:=false;
if CheckPixel(x,y,i) then break; {If FloodMaps can be drawn break the loop and continue with the next pixel}
end;
end;
if removePixel then DecPixelList; {All directions checked, remove last pixel from PixelList and continue with prvious pixel}
end;
end;