Skip to main content
2 of 4
deleted 17 characters in body
Chas Brown
  • 9.8k
  • 1
  • 13
  • 39

Python 2, 317 300 bytes

def f(w,h,B):
 A=[w*[2]for c in' '*h];p=w+h
 for x,y,c in B:A[y][x]=c<'0'
 for i in range(2*p):
	x,y,u,v=[[[(0,2*p+~i,1,0),(w+p+~i,h-1,0,-1)][i<w+p],(w-1,i-w,-1,0)][i<p],(i,0,0,1)][i<w]
	while w>x>=0<=y<h:u,v=[(-v,-u),(v,u),(u,v)][A[y][x]];x+=u;y+=v
	print[[y+w,2*p+~y][x<0],[w+p+~x,x][y<h]][w>x>=0],

Try it online!

Chas Brown
  • 9.8k
  • 1
  • 13
  • 39