def test_line_line(): l1 = Line(1, 1, 0) l2 = Line(1, -1, 0) assert {P2(0, 0)} == intersect(l1, l2) assert {P2(1, 1)} == intersect(l2, Line(1, 1, 2))
def test_line_equality(): line = Line(1, -1, 0) assert line == Line(-1, 1, 0) assert line != Line(1, 1, 0) assert not line == None
def test_closest_point(): horz = Line.ByPoints(P2(-1, 10), P2(1, 10)) assert horz.closest(P2(0, 0)).approx(P2(0, 10)) assert horz.closest(P2(5, 10)).approx(P2(5, 10)) vert = Line.ByPoints(P2(10, 1), P2(10, -2)) assert vert.closest(P2(0, 5)).approx(P2(10, 5)) assert vert.closest(P2(10, 5)).approx(P2(10, 5))
def test_polygon_line(): polygon = Polygon([P2(-1, -1), P2(1, -1), P2(1, 1), P2(-1, 1)]) line = Line(1, 1, 0) assert {P2(-1, 1), P2(1, -1)} == intersect(polygon, line) assert {P2(-1, 1), P2(1, -1)} == intersect(line, polygon) line = Line(1, 1, 1) assert {P2(0, 1), P2(1, 0)} == intersect(polygon, line) assert {P2(0, 1), P2(1, 0)} == intersect(line, polygon)
def test_intersection(): horz = Line.ByPoints(P2(-1, 10), P2(1, 10)) vert = Line.ByPoints(P2(10, -1), P2(10, 1)) assert horz.intersection(vert), P2(10, 10) assert horz.intersection(horz) is None assert horz.intersection(Line.ByPoints(P2(-1, 0), P2(1, 0))) is None l45 = Line.ByPoints(P2(0, 0), P2(1, 1)) assert horz.intersection(l45).approx(P2(10, 10)) l135 = Line.ByPoints(P2(0, 0), P2(-1, 1)) assert horz.intersection(l135).approx(P2(-10, 10))
def test_line_intersection(): l1 = Line(1, 1, 0) l2 = Line(1, -1, 0) assert l1.intersection(l2) == P2(0, 0) assert l2.intersection(l1) == P2(0, 0) l3 = Line(1, 1, 1) assert l1.intersection(l3) is None with pytest.raises(TypeError): l1.intersection(None)
def test_does_intersect_line_line(): l45 = Line(1, -1, 0) l0 = Line(0, 1, 0) l90 = Line(1, 0, 0) l45_up = Line(1, -1, 10) assert does_intersect(l45, l0) assert does_intersect(l90, l0) assert not does_intersect(l45, l45_up) with pytest.raises(ValueError): does_intersect(l45, l45)
def test_line_line_segment(): l1 = Line(1, 1, 0) assert {P2(0, 0)} == intersect(l1, LineSegment(P2(-10, -10), P2(10, 10))) assert {P2(0, 0)} == intersect(LineSegment(P2(-10, -10), P2(10, 10)), l1) assert {P2(0, 0)} == intersect(l1, LineSegment(P2(10, 10), P2(-10, -10))) assert {P2(0, 0)} == intersect(LineSegment(P2(10, 10), P2(-10, -10)), l1) assert set() == intersect(l1, LineSegment(P2(1, 1), P2(10, 10)))
def test_parallel_y(): line = Line.ByPoints(P2(0, 0), P2(1, 0)) up_one = line.parallel(P2(1, 1)) assert up_one == line.parallel(P2(0, 1)) assert up_one.y(-1) == 1 assert up_one.y(1) == 1
def test_line_circle(): circle = Circle(100, P2(1, 1)) x = Line(1, 0, 1) points = intersect(x, circle) assert {P2(1.0, 101.0), P2(1.0, -99.0)} == points points = intersect(circle, x) assert {P2(1.0, 101.0), P2(1.0, -99.0)} == points tangent_circle = Circle(1, P2(2, 0)) assert {P2(1.0, 0.0)} == intersect(tangent_circle, x) assert {P2(1.0, 0.0)} == intersect(x, tangent_circle) horz = Line(0, 1, 1) assert {P2(-99.0, 1.0), P2(101, 1.0)} == intersect(horz, circle) assert {P2(-99.0, 1.0), P2(101, 1.0)} == intersect(circle, horz) far_line = Line(1, 0, 200) assert set() == intersect(far_line, circle) assert set() == intersect(circle, far_line)
def test_degenerate_cases(): vert = Line.ByPoints(P2(1, 1), P2(1, -1)) assert vert.y(1) is None assert vert.y(-100) is None assert vert.x(-1) == pytest.approx(1) assert vert.x(1) == pytest.approx(1) assert vert.x(-100) == pytest.approx(1) assert vert.x(100) == pytest.approx(1) horz = Line.ByPoints(P2(-1, 0), P2(1, 0)) assert horz.y(-1) == pytest.approx(0) assert horz.y(1) == pytest.approx(0) assert horz.y(-100) == pytest.approx(0) assert horz.y(100) == pytest.approx(0) assert horz.x(-1) is None assert horz.x(1) is None assert horz.x(-100) is None assert horz.x(100) is None
def test_does_intersect_line_line_segment(): line = Line(1, 0, 0) x_ls = LineSegment(P2(-1, 0), P2(1, 0)) p_ls = LineSegment(P2(-1, -1), P2(-1, 1)) assert does_intersect(line, x_ls) assert does_intersect(x_ls, line) assert not does_intersect(line, p_ls) assert not does_intersect(p_ls, line) with pytest.raises(ValueError): does_intersect(line, LineSegment(P2(0, 0), P2(0, 1)))
def test_create_from_points(): l = Line.ByPoints(P2(-1, -1), P2(1, 1)) assert l.y(1) == pytest.approx(1) assert l.y(-1) == pytest.approx(-1) assert l.x(1) == pytest.approx(1) assert l.x(-1) == pytest.approx(-1) assert l.y(25) == pytest.approx(25) assert l.y(-25) == pytest.approx(-25) assert l.x(25) == pytest.approx(25) assert l.x(-25) == pytest.approx(-25) l = Line.ByPoints(P2(-1, 1), P2(1, -1)) assert l.y(1) == pytest.approx(-1) assert l.y(-1) == pytest.approx(1) assert l.x(1) == pytest.approx(-1) assert l.x(-1) == pytest.approx(1) assert l.y(25) == pytest.approx(-25) assert l.y(-25) == pytest.approx(25) assert l.x(25) == pytest.approx(-25) assert l.x(-25) == pytest.approx(25)
def test_line_contains_point(): line = Line(1, 1, 0) assert line.contains(P2(0, 0)) assert line.contains(P2(-1, 1)) assert line.contains(P2(1, -1)) assert not line.contains(P2(0, 1)) assert not line.contains(V2(0, 0))
def test_does_intersect_polygon_line(cw_polygon): l45 = Line(1, -1, 0) assert does_intersect(cw_polygon, l45) assert does_intersect(l45, cw_polygon) l45_up10 = Line(1, -1, 10) assert not does_intersect(cw_polygon, l45_up10) assert not does_intersect(l45_up10, cw_polygon) assert does_intersect(cw_polygon, Line(1, 0, 0.5)) assert not does_intersect(cw_polygon, Line(1, 0, -0.5)) assert does_intersect(cw_polygon, Line(0, 1, 0.5)) assert not does_intersect(cw_polygon, Line(0, 1, -0.5))
def test_translate(): line = Line(2, 1, 2) assert line.y(0) == pytest.approx(2) assert line.x(0) == pytest.approx(1) translated = line.translate(V2(1, 1)) assert translated.y(0) == pytest.approx(3) assert translated.x(0) == pytest.approx(2) horz = Line.ByPoints(P2(-1, 10), P2(1, 10)) assert horz.y(0) == pytest.approx(10) translated = horz.translate(V2(1, 1)) assert translated.y(0) == pytest.approx(11) vert = Line.ByPoints(P2(10, -1), P2(10, 1)) assert vert.x(0) == pytest.approx(10) translated = vert.translate(V2(1, 1)) assert translated.x(0) == pytest.approx(11)
def test_line_above(): horz = Line(0, 1, 10) assert horz.on_side(P2(0, 20)) == 1 assert horz.on_side(P2(20, 20)) == 1 assert horz.on_side(P2(0, 10)) == 0 assert horz.on_side(P2(0, 0)) == -1 vert = Line(1, 0, 10) assert vert.on_side(P2(0, 0)) == -1 assert vert.on_side(P2(20, 0)) == 1 assert vert.on_side(P2(10, 0)) == 0 sloped = Line(1, 1, 0) assert sloped.on_side(P2(10, 10)) == 1 assert sloped.on_side(P2(-1, -1)) == -1 assert sloped.on_side(P2(-10, 20)) == 1 assert sloped.on_side(P2(-10, 10)) == 0
def test_does_intersect_line_circle(): circle = Circle(99, P2(100, 100)) assert not does_intersect(circle, Line(1, 1, 0)) assert not does_intersect(Line(1, 1, 0), circle) assert does_intersect(circle, Line(1, -1, 0)) assert does_intersect(Line(1, -1, 0), circle) assert does_intersect(circle, Line(1, 0, 100)) assert does_intersect(Line(1, 0, 100), circle) assert does_intersect(circle, Line(0, 1, 100)) assert does_intersect(Line(0, 1, 100), circle) assert not does_intersect(circle, Line(1, 0, 0)) assert not does_intersect(Line(1, 0, 0), circle) assert does_intersect(circle, Line(1, 0, 1)) assert does_intersect(Line(1, 0, 1), circle)
def test_create(): with pytest.raises(ValueError): Line(0, 0, 0)