Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import random as rd
  2. import math as mt
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. ## count how many lines you've crossed
  7.  
  8. def find_int_between(x,y):
  9. if y > x:
  10. return max(0,mt.ceil(y) - mt.floor(x)-1)
  11. else:
  12. return max(0, mt.ceil(x) - mt.floor(y)-1)
  13.  
  14.  
  15. ## set this to whatever
  16. distance = 1
  17. n = 100
  18. success = 0
  19.  
  20. for i in range(n):
  21.  
  22. x = rd.random()*10000
  23. y = rd.random()*10000
  24.  
  25. start_pt = [x,y]
  26.  
  27. angle = rd.uniform(0, 360)
  28. x_1 = x + distance * mt.cos(angle)
  29. y_1 = y + distance * mt.sin(angle)
  30.  
  31. end_pt = [x_1,y_1]
  32.  
  33. distance_check = mt.sqrt(((start_pt[0]-end_pt[0])**2)+((start_pt[1]-end_pt[1])**2) )
  34.  
  35. x_axis_cross = find_int_between(end_pt[0],(start_pt[0]))
  36. y_axis_cross = find_int_between(end_pt[1],(start_pt[1]))
  37.  
  38. ## number of lines crossed
  39. total_cross = abs(x_axis_cross) + abs(y_axis_cross)
  40. if total_cross == 1:
  41. success+=1
  42. # print(total_cross)
  43.  
  44. print(success)
  45. print(success/n)
  46. ## check
  47. # print(start_pt)
  48. # print(distance_check)
  49. # print(end_pt)
  50. # print(angle)
  51.  
  52. ## check to visualize what you're doing
  53.  
  54. # x = [start_pt[0],end_pt[0]]
  55. # y = [start_pt[1],end_pt[1]]
  56. # plt.plot(x, y)
  57. # plt.grid(True)
  58. # plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement