問題設定
Scratchでの塗りつぶしは「境界を維持しつつ、対象領域だけを走査する」必要があります。 とくに大量のピクセルを処理する場合、再帰よりもキューを使う幅優先探索のほうが安全です。
Scratchブロック設計
define flood fill (startX) (startY) if <touching [border v]?> then stop [this script v] end set [queue v] to (join (startX) [,] (startY)) repeat until <(length of [queue v]) = [0]> // dequeue and paint end
デモ
Click an empty area to run queue-based flood fill. Border cells are dark, filled cells are medium gray.
Color key: border, target, fill
塗りつぶし処理は4近傍(上下左右)だけを対象にし、境界色セルを超えないように実装します。