Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Shooting Range by UFO665
def coeffs(x1, y1, x2, y2):
a = y2 - y1
b = x1 - x2
c = y1 * (x2 - x1) - x1 * (y2 - y1)
return a, b, c
def distance(x1, y1, x2, y2):
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
def shot(wall1, wall2, shot_point, later_point):
x1, y1 = wall1
x2, y2 = wall2
a1, b1, c1 = coeffs(x1, y1, x2, y2)
x3, y3 = shot_point
x4, y4 = later_point
a2, b2, c2 = coeffs(x3, y3, x4, y4)
if min(distance(x1, y1, x3, y3), distance(x2, y2, x3, y3)) < min(distance(x1, y1, x4, y4), distance(x2, y2, x4, y4)):
return -1
x = -(c1 * b2 - c2 * b1) / float(a1 * b2 - a2 * b1)
y = -(a1 * c2 - a2 * c1) / float(a1 * b2 - a2 * b1)
cx, cy = (x1 + x2) / 2.0, (y1 + y2) / 2.0
iRes = int(round(100 * (distance(x1, y1, cx, cy) - distance(x, y, cx, cy)) / distance(x1, y1, cx, cy)))
return iRes if iRes >= 0 else -1
Feb. 12, 2016