Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Bitmap-Flood-fill/Scala/bitmap-flood-fill.scala
Normal file
43
Task/Bitmap-Flood-fill/Scala/bitmap-flood-fill.scala
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import java.awt.Color
|
||||
import scala.collection.mutable
|
||||
|
||||
object Flood {
|
||||
def floodFillStack(bm:RgbBitmap, x: Int, y: Int, targetColor: Color): Unit = {
|
||||
// validate
|
||||
if (bm.getPixel(x,y) == targetColor) return
|
||||
|
||||
// vars
|
||||
val oldColor = bm.getPixel(x,y)
|
||||
val pixels = new mutable.Stack[(Int,Int)]
|
||||
|
||||
// candy coating methods
|
||||
def paint(fx: Int, fy:Int) = bm.setPixel(fx,fy,targetColor)
|
||||
def old(cx: Int, cy: Int): Boolean = bm.getPixel(cx,cy) == oldColor
|
||||
def push(px: Int, py: Int) = pixels.push((px,py))
|
||||
|
||||
// starting point
|
||||
push(x,y)
|
||||
|
||||
// work
|
||||
while (pixels.nonEmpty) {
|
||||
val (x, y) = pixels.pop()
|
||||
var y1 = y
|
||||
while (y1 >= 0 && old(x, y1)) y1 -= 1
|
||||
y1 += 1
|
||||
var spanLeft = false
|
||||
var spanRight = false
|
||||
while (y1 < bm.height && old(x, y1)) {
|
||||
paint(x,y1)
|
||||
if (x > 0 && spanLeft != old(x-1,y1)) {
|
||||
if (old(x - 1, y1)) push(x - 1, y1)
|
||||
spanLeft = !spanLeft
|
||||
}
|
||||
if (x < bm.width - 1 && spanRight != old(x+1,y1)) {
|
||||
if (old(x + 1, y1)) push(x + 1, y1)
|
||||
spanRight = !spanRight
|
||||
}
|
||||
y1 += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue