Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Intersecting rays solution in Clear category for Shooting Range by Kolia951
from math import floor, sqrt
def two_lines(line1, line2):
"""
Finds a coordinate where two lines intersects
"""
(x1, y1), (x2, y2) = line1
(x3, y3), (x4, y4) = line2
x_part1 = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))
x_part2 = ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
y_part1 = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))
y_part2 = ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4))
try:
px = x_part1 / x_part2
py = y_part1 / y_part2
except:
return "infinity"
return px, py
def center(line):
"""
Finds a coordinate of line's center point
"""
(x1, y1), (x2, y2) = line
px = (x1 + x2) / 2
py = (y1 + y2) / 2
return px, py
def lenght(line):
"""
Find a lenght of the lline
"""
(x1, y1), (x2, y2) = line
result = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return result
def shot(wall1, wall2, shot1, shot2):
# shot is not towards to wall
if shot2[0] > shot1[0]:
return -1
# parallel shot
if two_lines((wall1, wall2), (shot1, shot2)) == "infinity":
return -1
wall_center_x, wall_center_y = center((wall1, wall2))
hit_x, hit_y = two_lines((wall1, wall2), (shot1, shot2))
rate_total = lenght(((wall_center_x, wall_center_y), (wall2)))
hit_rate = lenght(((wall_center_x, wall_center_y), (hit_x, hit_y)))
points = 100 - round((hit_rate * 100 / rate_total))
if points < 0:
return -1
else:
return points
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"
Feb. 16, 2023
Comments: