Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Shooting Range by suic
def shot(wall1, wall2, shot_point, later_point):
wall = line(wall1, wall2)
trajectory = line(shot_point, later_point)
if cross_product(direction(wall1, wall2),
direction(shot_point, later_point)) == 0:
return -1
intersection = intersect(wall, trajectory)
if not same_direction(shot_point, later_point, intersection):
return -1
midpoint = section_midpoint(wall1, wall2)
line_len = section_length(wall1, wall2)
distance = section_length(midpoint, intersection)
if (2 * distance) > line_len:
return -1
else:
return int(round((1 - ((2 * distance) / line_len)) * 100, 0))
def line(A, B):
"""Calculate line equation for given points."""
return (A[1] - B[1],
B[0] - A[0],
A[0] * B[1] - A[1] * B[0])
def direction(A, B):
return B[0] - A[0], B[1] - A[1]
def cross_product(v1, v2):
return v1[0] * v2[1] - v1[1] * v2[0]
def same_direction(A, B, C):
ab_dir = B[0] - A[0], B[1] - A[1]
ac_dir = C[0] - A[0], C[1] - A[1]
return all([sign(ab_dir[i]) == sign(ac_dir[i]) for i in range(2)])
def sign(x): return (x > 0) - (x < 0)
def intersect(l1, l2):
x = (l1[1] * l2[2] - l2[1] * l1[2]) / (l2[1] * l1[0] - l1[1] * l2[0])
y = (l1[0] * l2[2] - l2[0] * l1[2]) / (l2[0] * l1[1] - l1[0] * l2[1])
return x, y
def section_length(A, B):
return ((A[0] - B[0]) ** 2 + (A[1] - B[1]) ** 2) ** 0.5
def section_midpoint(A, B):
return (A[0] + B[0]) / 2, (A[1] + B[1]) / 2
Aug. 27, 2014
Comments: