project2/p2q1-scanline.rb
# scanline fill implementation adapted from somewhere on the internet
scanline fill implementation adapted from somewhere on the internet
scanline fill implementation adapted from somewhere on the internet
def lightning(a, y, x)
  return a if a[y][x] == 0
  height = a.length     
  width = a[0].length   
  sl = 0
  sr = 0
  st = []
  st.push([y, x])
  until st.empty?
    yy, xx = st.pop
    xx -= 1 while xx > 0 && a[yy][xx-1] == 1
    top_cont = false
    btm_cont = false
    while xx < width && a[yy][xx] == 1
      a[yy][xx] = 0
      if top_cont == false && yy > 0 && a[yy-1][xx] == 1
        st.push([yy-1, xx])
        top_cont = true
      elsif top_cont == true && yy > 0 && a[yy-1][xx] == 0
        top_cont = false
      end
      if btm_cont == false && yy < height-1 && a[yy+1][xx] == 1
        st.push([yy+1, xx])
        btm_cont = true
      elsif btm_cont == true && yy < height-1 && a[yy+1][xx] == 0
        btm_cont = false
      end
      xx += 1
    end
  end
  return a
end
def lightning ( a , y , x ) return a if a [ y ] [ x ] == 0 height = a . length width = a [ 0 ] . length sl = 0 sr = 0 st = [ ] st . push ( [ y , x ] ) until st . empty? yy , xx = st . pop xx -= 1 while xx > 0 && a [ yy ] [ xx - 1 ] == 1 top_cont = false btm_cont = false while xx < width && a [ yy ] [ xx ] == 1 a [ yy ] [ xx ] = 0 if top_cont == false && yy > 0 && a [ yy - 1 ] [ xx ] == 1 st . push ( [ yy - 1 , xx ] ) top_cont = true elsif top_cont == true && yy > 0 && a [ yy - 1 ] [ xx ] == 0 top_cont = false end if btm_cont == false && yy < height - 1 && a [ yy + 1 ] [ xx ] == 1 st . push ( [ yy + 1 , xx ] ) btm_cont = true elsif btm_cont == true && yy < height - 1 && a [ yy + 1 ] [ xx ] == 0 btm_cont = false end xx += 1 end end return a end
scanline fill implementation adapted from somewhere on the internet
scanline fill implementation adapted from somewhere on the internet
# nothing to do if this is already 0 # retrieve the dimensions for bounds checking later # y # x # prepare the stack # find the leftmost continuous 1 on this row # keep track of whether there is a continuous chunk on the top/bottom # iterate from left to right # this reduces number of stack push/pop operations # clear the current cell # check upstairs # we just found the leftmost cell of a new chunk # there was a chunk but it ended, reset flag # check downstairs # move to the right
a
y
x