def get_away_from_trajectory(position, start, end, min_distance): try: point = closest_point_on_line(position, start, end) dist = position - point except ZeroDivisionError: point = position dist = perpendicular(end - start) * min_distance/2 if dist.norm < min_distance: return point - dist.norm * min_distance else: return position
def test_closest_point_on_line_outside_range(): reference = Position(-50, 50) start = Position(10, 0) end = Position(100, 0) assert closest_point_on_line(reference, start=start, end=end) == Position(-50, 0)
def test_closest_point_on_line_at_zero(): reference = Position(0, 50) start = Position(-100, 0) end = Position(100, 0) assert closest_point_on_line(reference, start=start, end=end) == Position(0, 0)
def test_closest_point_on_line_diagonal_reverse(): reference = Position(50, 50) end = Position(0, 0) start = Position(100, 100) assert closest_point_on_line(reference, start=start, end=end) == Position(50, 50)
def test_closest_point_on_line_horizontal(): reference = Position(50, 50) start = Position(0, 10) end = Position(0, 100) assert closest_point_on_line(reference, start=start, end=end) == Position(0, 50)
def test_closest_point_on_line_vertical(): reference = Position(50, 50) start = Position(10, 0) end = Position(100, 0) assert closest_point_on_line(reference, start=start, end=end) == Position(50, 0)