Created
June 24, 2016 17:54
-
-
Save bnaul/ea1fe0906f21015d075d33fe91453a38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class Point: | |
def __init__(self,x,y): | |
self.x = x | |
self.y = y | |
def __repr__(self): | |
return "Point({}, {})".format(self.x, self.y) | |
def ccw(A,B,C): | |
return (C.y-A.y)*(B.x-A.x) > (B.y-A.y)*(C.x-A.x) | |
def intersect(A,C): | |
return (ccw(A[0],C[0],C[1]) != ccw(A[1],C[0],C[1]) | |
and ccw(A[0],A[1],C[0]) != ccw(A[0],A[1],C[1])) | |
N = 1e5 | |
totals = np.zeros(int(N), dtype=int) + 4 | |
for j in range(int(N)): | |
segments = [] | |
for i in range(3): | |
theta1 = np.random.uniform(0.0, 2 * np.pi) | |
p1 = Point(np.cos(theta1), np.sin(theta1)) | |
theta2 = np.random.uniform(0.0, 2 * np.pi) | |
p2 = Point(np.cos(theta2), np.sin(theta2)) | |
segments.append((p1, p2)) | |
totals[j] += (intersect(segments[0], segments[1]) | |
+ intersect(segments[0], segments[2]) | |
+ intersect(segments[1], segments[2])) | |
print(totals.mean()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment