Skip to content

Commit

Permalink
small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Feb 20, 2025
1 parent cc630dc commit 904df7c
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,17 @@ function hasManySiblings(img, x1, y1, width, height) {
const y0 = Math.max(y1 - 1, 0);
const x2 = Math.min(x1 + 1, width - 1);
const y2 = Math.min(y1 + 1, height - 1);
const pos = y1 * width + x1;
const val = img[y1 * width + x1];
let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;

// go through 8 adjacent pixels
for (let x = x0; x <= x2; x++) {
for (let y = y0; y <= y2; y++) {
if (x === x1 && y === y1) continue;
if (img[pos] === img[y * width + x]) zeroes++;
zeroes += +(val === img[y * width + x]);
if (zeroes > 2) return true;
}
}

return false;
}

Expand Down

0 comments on commit 904df7c

Please sign in to comment.