Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1be1853

Browse files
committed
Deduplicate further
1 parentcf34946 commit1be1853

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

‎Recursive/FloodFill.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*@see https://www.techiedelight.com/flood-fill-algorithm/
1010
*/
1111

12-
constneighbors=[
12+
constneighborOffsets=[
1313
[-1,-1],
1414
[-1,0],
1515
[-1,1],
@@ -32,6 +32,15 @@ function checkLocation(rgbData, location) {
3232
}
3333
}
3434

35+
function*neighbors(rgbData,location){
36+
for(constoffsetofneighborOffsets){
37+
constneighborLocation=[location[0]+offset[0],location[1]+offset[1]]
38+
if(isInside(rgbData,neighborLocation)){
39+
yieldneighborLocation
40+
}
41+
}
42+
}
43+
3544
/**
3645
* Implements the flood fill algorithm through a breadth-first approach using a queue.
3746
*
@@ -96,13 +105,8 @@ function breadthFirstFill(
96105

97106
if(rgbData[currentLocation[0]][currentLocation[1]]===targetColor){
98107
rgbData[currentLocation[0]][currentLocation[1]]=replacementColor
99-
100-
for(leti=0;i<neighbors.length;i++){
101-
constx=currentLocation[0]+neighbors[i][0]
102-
consty=currentLocation[1]+neighbors[i][1]
103-
if(x>=0&&x<rgbData.length&&y>=0&&y<rgbData[0].length){
104-
queue.push([x,y])
105-
}
108+
for(constneighborLocationofneighbors(rgbData,currentLocation)){
109+
queue.push(neighborLocation)
106110
}
107111
}
108112
}
@@ -118,15 +122,8 @@ function breadthFirstFill(
118122
functiondepthFirstFill(rgbData,location,targetColor,replacementColor){
119123
if(rgbData[location[0]][location[1]]===targetColor){
120124
rgbData[location[0]][location[1]]=replacementColor
121-
122-
for(leti=0;i<neighbors.length;i++){
123-
constnewLocation=[
124-
location[0]+neighbors[i][0],
125-
location[1]+neighbors[i][1]
126-
]
127-
if(isInside(rgbData,newLocation)){
128-
depthFirstFill(rgbData,newLocation,targetColor,replacementColor)
129-
}
125+
for(constneighborLocationofneighbors(rgbData,location)){
126+
depthFirstFill(rgbData,neighborLocation,targetColor,replacementColor)
130127
}
131128
}
132129
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp