fork download
  1. import random
  2. random.seed(42) #reproducible results
  3.  
  4. def rp():
  5. """Choose a random point uniformly in the unit circle"""
  6. while True:
  7. x = random.uniform(-1, 1)
  8. y = random.uniform(-1, 1)
  9. if x*x + y*y < 1:
  10. return (x, y)
  11.  
  12. def ccw(a, b):
  13. """Compute a 2x2 determinant to decide orientation of angle between
  14. two points as seen from the origin."""
  15. return a[0]*b[1] - a[1]*b[0]
  16.  
  17. containsCenter = 0
  18. areaWithCenter = 0
  19. numTotal = 250000 # higher means more time but more precision as well
  20. for i in xrange(numTotal):
  21. p1 = rp()
  22. p2 = rp()
  23. p3 = rp()
  24. c12 = ccw(p1, p2)
  25. c23 = ccw(p2, p3)
  26. c31 = ccw(p3, p1)
  27. if c12*c23 > 0 and c23*c31 > 0 and c31*c12 > 0:
  28. containsCenter += 1
  29. areaWithCenter += abs(c12*c23*c31) # shoelace formula
  30.  
  31. areaWithCenter = areaWithCenter/containsCenter
  32. containsCenter = float(containsCenter)/numTotal
  33. print(containsCenter)
  34. print(areaWithCenter)
  35.  
Success #stdin #stdout 2.46s 10544KB
stdin
Standard input is empty
stdout
0.250156
0.0297397699519