Guest User

Untitled

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. from decimal import Decimal as dec
  2.  
  3. def light_through_glass(panes, iterations):
  4.     # All simulated rays with direction [left, right]
  5.     # Always starts with a 100% ray in the leftmost space, heading right
  6.     # space0 pane0 space1 pane1 ... spacen panen
  7.     rays = [[dec(0)]*(panes+1), [dec(100)]+[dec(0)]*(panes)]
  8.     # Output, duh
  9.     out = [dec(0)]*2 #[reflect, through]
  10.     # Graphic blingbling gangsta yo!
  11.     percent = 10
  12.     print('0%'),
  13.     for i in range(iterations):
  14.         # Graphic bling-bling
  15.         if float(i)/float(iterations)*100 >= percent:
  16.             print(str(percent)+'%'),
  17.             percent += 10
  18.         # New nice empty array for the iterated rays
  19.         # (when we've done stuff to them ehehe)
  20.         # And just like rays they are 1 longer than panes _ | _ | _<--see?
  21.         newrays = [[dec(0)]*(panes+1),[dec(0)]*(panes+1)]
  22.         # 'd' as in direction! 0 is left, 1 is right
  23.         for d in (0, 1):
  24.             # parse all positions in rays[] no, MOOVE
  25.             # n is the position among the panes, r is the strength-thingy
  26.             for n,r in enumerate(rays[d]):
  27.                 # go frak yourself if the space is pitch black dark eh
  28.                 if r == 0:
  29.                     continue
  30.                 # if the rays are at the end and heading away, put them
  31.                 # out of their misery (to out[], either reflect or through
  32.                 # depending on n's position)
  33.                 if n == panes*d:
  34.                     out[d] += r
  35.                 # no we're talking yo!
  36.                 else:
  37.                     # dump a 10%-ish ray on the same space, but reverse dir
  38.                     newrays[abs(d-1)][n] += r/10
  39.                     # push away a 90% ray to the next space in the ray's
  40.                     # direction, shablam!
  41.                     newrays[d][n-1+d*2] += r * dec('0.9')
  42.         # dump the old crap and in with the new AHAAH
  43.         rays = newrays
  44.     # gtfo :*
  45.     return out
  46.  
  47. if __name__ == '__main__':
  48.     panes = 100
  49.     iterations = 1000
  50.     print('panes: {0}, iterations: {1}'.format(panes, iterations))
  51.     data = light_through_glass(panes, iterations)
  52.     print('\nreflect: {0}\nthrough: {1}\nloss: {2}'.format(data[0], data[1],
  53.                                                     100-sum(data)))
  54. # good stuff
Add Comment
Please, Sign In to add comment