Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Shooting Range by Moff
from collections import namedtuple
def det2(m):
return m[0][0] * m[1][1] - m[0][1] * m[1][0]
Point = namedtuple('Point', 'x y')
class Line(namedtuple('Line', 'a b c')):
@classmethod
def from_points(cls, p1, p2):
return cls(p2.y-p1.y, p1.x - p2.x, p1.y * p2.x - p1.x * p2.y)
def intersect(self, other):
if self == other:
return float('inf')
d = det2([[self.a, self.b], [other.a, other.b]])
if abs(d) < 1E-6:
return None
return Point(- det2([[self.c, self.b], [other.c, other.b]]) / d,
- det2([[self.a, self.c], [other.a, other.c]]) / d)
def cut(p1, p2, q):
if p1.x != p2.x:
return (q.x - p1.x) / (p2.x - p1.x)
else:
return (q.y - p1.y) / (p2.y - p1.y)
def shot(*argv):
w1, w2, s1, s2 = (Point(*t) for t in argv)
p = Line.from_points(w1, w2).intersect(Line.from_points(s1, s2))
if p is None:
return -1
if cut(s1, s2, p) < 0:
return -1
q = abs(cut(w1, w2, p) - 0.5)
return -1 if q > 0.5 else round((0.5 - q) * 200)
Aug. 6, 2015