Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Shooting Range by Tanis1981
#from __future__ import division
import math
def line(p1, p2):
A = (p1[1] - p2[1])
B = (p2[0] - p1[0])
C = (p1[0]*p2[1] - p2[0]*p1[1])
return A, B, -C
def intersection(L1, L2):
D = L1[0] * L2[1] - L1[1] * L2[0]
Dx = L1[2] * L2[1] - L1[1] * L2[2]
Dy = L1[0] * L2[2] - L1[2] * L2[0]
if D != 0:
x = Dx / D
y = Dy / D
return x,y
else:
return False
def shot(wall1, wall2, shot_point, later_point):
line1 = line(wall1, wall2)
line2 = line(shot_point, later_point)
intersection_point = intersection(line1, line2)
if intersection_point == False:
result = -1
else:
distance_interval = math.dist(wall1, wall2)
dist1 = math.dist(wall1, intersection_point)
dist2 = math.dist(wall2, intersection_point)
if dist1 > distance_interval or dist2 > distance_interval:
result = -1
elif math.dist(shot_point, intersection_point) <= math.dist(later_point, intersection_point):
result = -1
else:
if dist1 > dist2:
dist = dist2
else:
dist = dist1
result = round(dist / (distance_interval/2) * 100)
return result
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert shot((2, 2), (5, 7), (11, 2), (8, 3)) == 100, "1st case"
assert shot((2, 2), (5, 7), (11, 2), (7, 2)) == 0, "2nd case"
assert shot((2, 2), (5, 7), (11, 2), (8, 4)) == 29, "3th case"
assert shot((2, 2), (5, 7), (11, 2), (9, 5)) == -1, "4th case"
assert shot((2, 2), (5, 7), (11, 2), (10.5, 3)) == -1, "4th case again"
April 24, 2023
Comments: