Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Oh Math, Up Yours! solution in Clear category for Shooting Range by obone
from math import sqrt
def shot(wall1, wall2, shot_point, later_point):
length = lambda x1, y1, x2, y2: sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
sign = lambda x: 1 - (x < 0)
# Wall line coefficients
Aw = wall1[1] - wall2[1]
Bw = wall2[0] - wall1[0]
Cw = wall1[0] * wall2[1] - wall1[1] * wall2[0]
# Shot line coefficients
Ab = shot_point[1] - later_point[1]
Bb = later_point[0] - shot_point[0]
Cb = shot_point[0] * later_point[1] - shot_point[1] * later_point[0]
# Non-crossing lines
if Bw * Ab == Bb * Aw:
return -1
# (xc, yc) - cross point of wall and shot lines
yc = (Cw * Ab - Cb * Aw) / (Bb * Aw - Bw * Ab)
xc = -(Cw + Bw * yc) / Aw if Aw else -(Cb + Bb * yc) / Ab
# Shot in opposite direction
if sign(shot_point[0] - xc) != sign(shot_point[0] - later_point[0]):
return -1
# (xm, ym) - middle point of wall
xm, ym = (wall1[0] + wall2[0]) / 2, (wall1[1] + wall2[1]) / 2
# Half len of wall
wlen = length(xm, ym, *wall2)
# Distance from the center
blen = length(xm, ym, xc, yc)
# Missed shot
if blen > wlen:
return -1
return round(100 * (wlen - blen) / wlen)
Aug. 19, 2019
Comments: