Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
math.hypot solution in Clear category for Shooting Range by David_Jones
from math import hypot
def shot(wall1, wall2, shot_point, later_point):
(x1,y1) = wall1
(x2,y2) = wall2
(x3,y3) = shot_point
(x4,y4) = later_point
if x1 == x2 and x3 == x4:
return -1
x = (((x2-x1)*(x4*y3-x3*y4)-(x4-x3)*(x2*y1-x1*y2)) /
((x4-x3)*(y2-y1)-(x2-x1)*(y4-y3)))
if x1 == x2:
y = ((y4-y3)*x+x4*y3-x3*y4) / (x4-x3)
else:
y = ((y2-y1)*x+x2*y1-x1*y2) / (x2-x1)
if (x-x3)*(x4-x3)+(y-y3)*(y4-y3) < 0:
return -1
(x0,y0) = ((x1+x2)/2, (y1+y2)/2)
points = 100 * (1 - hypot(x-x0,y-y0)/hypot(x1-x0,y1-y0))
return -1 if points < 0 else round(points)
June 12, 2019