Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Just some calculations solution in Clear category for Shooting Range by polettix
def shot(w1, w2, b0, b1):
'''
Median and extension delta for wall are:
w0 = (w1 + w2) / 2
wd = w2 - w0
This means that:
w1 = w0 - 1 * wd
w2 = w0 + 1 * wd
w = w0 + alpha * wd
where w is on the wall iff -1 <= alpha <= 1
Bullet trajectory is:
t = b1 - b0
Generic bullet position is:
b = b0 + beta * t
with beta >= 0
Crossing is when w = b, so we have to find alpha and
beta for this, check that they are within bounds and
use abs(alpha). Equation w = b can be rewritten as:
/ \ / \ / \
| wd[0] t[0] | | alpha | | b0[0] - w0[0] |
| | | | = | |
| wd[1] t[1] | | -beta | | b0[1] - w0[1] |
\ / \ / \ /
'''
w0 = (w1[0] + w2[0]) / 2.0, (w1[1] + w2[1]) / 2.0
wd = w2[0] - w0[0], w2[1] - w0[1]
t = b1[0] - b0[0], b1[1] - b0[1]
# Matrix coefficients
A11 = wd[0]
A21 = wd[1]
A12 = t[0]
A22 = t[1]
# Determinant. If abs() too low, just assume failure
det = A11 * A22 - A12 * A21
if abs(det) < 1e-5: return -1
# Matrix inverse
I11 = A22 / det
I12 = - A12 / det
I21 = - A21 / det
I22 = A11 / det
# Known terms (right hand side of equation)
y1 = b0[0] - w0[0]
y2 = b0[1] - w0[1]
# abs(alpha) -- we need to deal only with the absolute value. It MUST
# be <=1
abs_alpha = abs(I11 * y1 + I12 * y2)
if abs_alpha > 1: return -1
# beta, remember it's "-beta" in the equation so we change sign. It MUST
# be >= 0 for trajectory to point towards the wall or its extension
beta = -(I21 * y1 + I22 * y2)
if beta < 0: return -1
# score is calculated according to abs_alpha, i.e. relative distance
# of hit point with respect to wall center w0
return int(round(100 * (1 - abs_alpha)))
Aug. 21, 2015
Comments: