Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created July 6, 2012 20:21
Show Gist options
  • Save Sephi-Chan/287a04bd8af35746cd65 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/287a04bd8af35746cd65 to your computer and use it in GitHub Desktop.
from pylab import *
def interception(A, B, H, Sa, Sb):
''' Returns None if A cannot catch B before B reaches H.
Otherwise it returns the time and position of the interception.
See attached documentation for justifications. '''
sin_beta = det(array((A-B,H-B))) / ( norm(A-B) * norm(H-B) )
sin_alpha = (Sb / Sa) * sin_beta
if abs(sin_alpha) > 1 :
print "B moves too fast to be ever caught !"
return None
else:
sin_gamma = ( sin_alpha * sqrt(1 - sin_beta**2)
+ sin_beta * sqrt(1 - sin_alpha**2) )
BC = norm(B-A) * (sin_alpha / sin_gamma)
if BC > norm(H-A):
print "B reaches H before being caught by A !"
return None
else:
t = BC / Sb
C = B + BC * (H-B)/norm(H-B)
print "A intercepted B !"
return t,C
##### EXAMPLE
# Define the constants
A = array(( 1.0 , 5.0 ))
B = array(( 4.0 , 1.0 ))
H = array(( 6.0 , 7.0 ))
Sa = 1.1
Sb = 1.0
# Find the intersection
t,C = interception(A, B, H, Sa, Sb)
# Plot the results
scatter(*zip(A,B,H,C), s=100, color='r')
for label, point in zip(['A','B','H','C'], [A,B,H,C]):
annotate( label, xy = point, xytext = (-10, 10),
textcoords = 'offset points', fontsize = 24)
annotate("", xy=H, xytext=B, xycoords='data', textcoords='data',size=20,
arrowprops=dict(arrowstyle="simple",connectionstyle="arc3"))
annotate("", xy=C, xytext=A, xycoords='data', textcoords='data',size=20,
arrowprops=dict(arrowstyle="simple",connectionstyle="arc3"))
title("A intercepts B in C", fontsize = 24)
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment