Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from decimal import Decimal as dec
- def light_through_glass(panes, iterations):
- # All simulated rays with direction [left, right]
- # Always starts with a 100% ray in the leftmost space, heading right
- # space0 pane0 space1 pane1 ... spacen panen
- rays = [[dec(0)]*(panes+1), [dec(100)]+[dec(0)]*(panes)]
- # Output, duh
- out = [dec(0)]*2 #[reflect, through]
- # Graphic blingbling gangsta yo!
- percent = 10
- print('0%'),
- for i in range(iterations):
- # Graphic bling-bling
- if float(i)/float(iterations)*100 >= percent:
- print(str(percent)+'%'),
- percent += 10
- # New nice empty array for the iterated rays
- # (when we've done stuff to them ehehe)
- # And just like rays they are 1 longer than panes _ | _ | _<--see?
- newrays = [[dec(0)]*(panes+1),[dec(0)]*(panes+1)]
- # 'd' as in direction! 0 is left, 1 is right
- for d in (0, 1):
- # parse all positions in rays[] no, MOOVE
- # n is the position among the panes, r is the strength-thingy
- for n,r in enumerate(rays[d]):
- # go frak yourself if the space is pitch black dark eh
- if r == 0:
- continue
- # if the rays are at the end and heading away, put them
- # out of their misery (to out[], either reflect or through
- # depending on n's position)
- if n == panes*d:
- out[d] += r
- # no we're talking yo!
- else:
- # dump a 10%-ish ray on the same space, but reverse dir
- newrays[abs(d-1)][n] += r/10
- # push away a 90% ray to the next space in the ray's
- # direction, shablam!
- newrays[d][n-1+d*2] += r * dec('0.9')
- # dump the old crap and in with the new AHAAH
- rays = newrays
- # gtfo :*
- return out
- if __name__ == '__main__':
- panes = 100
- iterations = 1000
- print('panes: {0}, iterations: {1}'.format(panes, iterations))
- data = light_through_glass(panes, iterations)
- print('\nreflect: {0}\nthrough: {1}\nloss: {2}'.format(data[0], data[1],
- 100-sum(data)))
- # good stuff
Add Comment
Please, Sign In to add comment