Skip to content

Commit cf34946

Browse files
vil02appgurueu
andcommitted
refactor: add and use isInside
Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com>
1 parent 0e42fe0 commit cf34946

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Recursive/FloodFill.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ const neighbors = [
2020
[1, 1]
2121
]
2222

23+
function isInside(rgbData, location) {
24+
const x = location[0]
25+
const y = location[1]
26+
return x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length
27+
}
28+
2329
function checkLocation(rgbData, location) {
24-
if (
25-
location[0] < 0 ||
26-
location[0] >= rgbData.length ||
27-
location[1] < 0 ||
28-
location[1] >= rgbData[0].length
29-
) {
30+
if (!isInside(rgbData, location)) {
3031
throw new Error('location should point to a pixel within the rgbData')
3132
}
3233
}
@@ -119,10 +120,12 @@ function depthFirstFill(rgbData, location, targetColor, replacementColor) {
119120
rgbData[location[0]][location[1]] = replacementColor
120121

121122
for (let i = 0; i < neighbors.length; i++) {
122-
const x = location[0] + neighbors[i][0]
123-
const y = location[1] + neighbors[i][1]
124-
if (x >= 0 && x < rgbData.length && y >= 0 && y < rgbData[0].length) {
125-
depthFirstFill(rgbData, [x, y], targetColor, replacementColor)
123+
const newLocation = [
124+
location[0] + neighbors[i][0],
125+
location[1] + neighbors[i][1]
126+
]
127+
if (isInside(rgbData, newLocation)) {
128+
depthFirstFill(rgbData, newLocation, targetColor, replacementColor)
126129
}
127130
}
128131
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy