Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Straightforward solution in Clear category for Shooting Range by nickie
from math import hypot
def distance(p, q):
return hypot(p[0]-q[0], p[1]-q[1])
def shot(wall1, wall2, shot_point, later_point):
x1, y1 = wall1
x2, y2 = wall2
x3, y3 = shot_point
x4, y4 = later_point
# http://en.wikipedia.org/wiki/Line-line_intersection
numer_x = (x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)
numer_y = (x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)
denom = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)
# if the two lines are parallel, there's no intersection
if denom == 0:
return -1
# intersection point
p = (numer_x / denom, numer_y / denom)
# did we shoot in the wrong direction?
if distance(p, later_point) >= distance(p, shot_point):
return -1
# did we hit the wall?
d1 = distance(p, wall1)
d2 = distance(p, wall2)
d = distance(wall1, wall2)
if d1 + d2 > d:
return -1
# calculate the score
return round(100*(1-abs(d1-d2)/d))
Aug. 30, 2014