2 karma · joined Aug 4, 2015 (11y ago) · 2 submissions
comment 11y ago · link · parent
My math might be wrong; doesn't seem to be a floating point precision issue.
from math import sqrt
from random import random
def randomPoints(width):
return [
[random()*width-width/2, random()*width-width/2],
[random()*width-width/2, random()*width-width/2]
]
def chordCalc(points):
def quadraticEquation(a,b,c):
descriminant=(b**2 - 4*a*c)
if descriminant <= 0:
return None
x1 = (-b+sqrt(descriminant))/(2*a)
x2 = (-b-sqrt(descriminant))/(2*a)
y1 = sqrt(1-x1**2)
y2 = sqrt(1-x2**2)
return [[x1, y1],[x2,y2]]
m = (points[0][1] - points[1][1]) / (points[0][0] - points[1][0])
b = points[0][1] - m*points[0][0]
qa = 1+m**2
qb = 2*b*m
qc = b**2-1
return quadraticEquation(qa, qb, qc)
def distance(points):
return sqrt((points[0][1] - points[1][1])**2
+ (points[0][0] - points[1][0])**2)
chords=0
largeChords=0
sqrt3=sqrt(3)
while True:
if random() > 0.9999:
print float(largeChords)/chords
points = randomPoints(500) #adjust me down to 0.5 and see probability go to ~0.33
intersections=chordCalc(points)
if intersections is None:
continue
chords += 1
if distance(intersections) > sqrt3:
largeChords += 1comment 11y ago · link · parent
"The probability of being hit is the probability of being inside the area of fire divided by the total area"
No.
The probability of being hit is the probability of being inside the area of fire.
The probability of being inside the area of fire is the area of fire divided by the entire area.