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
示例#2
0
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)
示例#3
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)
示例#4
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)
示例#5
0
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)
示例#6
0
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)
示例#7
0
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)
示例#8
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)
示例#9
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)
示例#10
0
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)
示例#11
0
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)