Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Shooting Range by TovarischZhukov
# migrated from python 2.7
def shot(w1, w2, shp, lp):
retval=-1
a1 = 0
a2 = 0
x = -1
y="a1*x+b1"
if lp[0]!= shp[0]:
a1 = (lp[1]-shp[1])/float(lp[0]-shp[0])
else:
y="a2*x+b2"
if w1[0]==w2[0]: return -1
x=lp[0]
b1 = lp[1]-a1*lp[0]
if w1[0]!= w2[0]:
a2 = (w2[1]-w1[1])/float(w2[0]-w1[0])
else:
y="a1*x+b1"
x=w1[0]
b2 = w1[1]-a2*w1[0]
if a1!= a2 and x==-1:
x=(b2-b1)/float(a1-a2)
if w1[1]==w2[1]:
y=w1[1]
if lp[1]==shp[1]: return -1
else:
y=eval(y)
print(x,y)
if min(w1[1],w2[1])<=y<=max(w1[1],w2[1]) and min(w1[0],w2[0])<=x<=max(w1[0],w2[0]) and \
(x>=lp[0]>=shp[0] or x<=lp[0]<=shp[0]) and (y>=lp[1]>=shp[1] or y<=lp[1]<=shp[1]):
centr= ((w1[0]+w2[0])/2.0, (w1[1]+w2[1])/2.0)
lenw = ((w1[0]-centr[0])**2+(w1[1]-centr[1])**2)**0.5
lenp = ((x-centr[0])**2+(y-centr[1])**2)**0.5
retval=int(round((1-abs(lenp/float(lenw)))*100))
return retval
Dec. 20, 2015