def test_p2_ccw(): p1 = P2(-1, 1) p2 = P2(0, 0) p3 = P2(1, 1) assert P2.CCW(p1, p2, p3) > 0 assert P2.CCW(p3, p2, p1) < 0
def test_polygon_rotate(cw_polygon): rotated = cw_polygon.rotate(math.pi / 2, P2(0, 0)) points = tuple(rotated.points()) assert points[0] == P2(-1, 0) assert points[1] == P2(0, 0) assert points[2] == P2(0, 1) assert points[3] == P2(-1, 1)
def test_offset_circle_contains(): circle = Circle(100, P2(2, 2)) assert circle.contains(P2(0, 0)) assert not circle.contains(P2(-99, 0)) assert P2(0, 0) in circle assert P2(-99, 0) not in circle
def test_circle_circle(): # functionality tested in test_r2_circle circle1 = Circle(100, P2(0, 0)) circle2 = Circle(100, P2(100, 100)) assert {P2(0.0, 100.0), P2(100.0, 0.0)} == intersect(circle1, circle2)
def test_create_from_points(): l = Line.ByPoints(P2(-1, -1), P2(1, 1)) assert l.a == -1 assert l.b == 1 assert l.c == 0 l = Line.ByPoints(P2(-1, 1), P2(1, -1)) assert l.a == 1 assert l.b == 1 assert l.c == 0 l = Line.ByPoints(P2(-1, 1), P2(1, 1)) assert l.a == 0 assert l.b == 1 assert l.c == 1 l = Line.ByPoints(P2(1, -1), P2(1, 1)) assert l.a == 1 assert l.b == 0 assert l.c == 1 with pytest.raises(ValueError): Line.ByPoints(P2(1, 1), P2(1, 1))
def test_polygon_centered_at(cw_polygon, ccw_polygon): assert P2(10, 10) == cw_polygon.centered(P2(10, 10)).centroid() assert P2(0, 0) == cw_polygon.centered(P2(0, 0)).centroid() assert P2(1, 1) == cw_polygon.centered(P2(1, 1)).centroid() assert P2(0, 0) == ccw_polygon.centered(P2(0, 0)).centroid() assert P2(1, 1) == ccw_polygon.centered(P2(1, 1)).centroid()
def test_parallel_x(): line = Line.ByPoints(P2(0, 0), P2(0, 1)) over_one = line.parallel(P2(1, 0)) assert over_one.a == 1 assert over_one.b == 0 assert over_one.c == 1
def test_p2_eq(): assert P2(0, 0) == P2(0, 0) assert P2(0, 0) != V2(0, 0) assert P2(0, 0) != None assert None != P2(0, 0)
def test_does_intersect_circle_circle(): c1 = Circle(100) c2 = Circle(100, P2(500, 500)) assert not does_intersect(c1, c2) assert not does_intersect(c1, Circle(50)) assert does_intersect(c1, Circle(100, P2(10, 10)))
def test_p2_angle(): p1 = P2(-1, 1) p2 = P2(0, 0) p3 = P2(1, 1) angle = P2.Angle(p1, p2, p3) assert math.isclose(angle, math.pi / 2)
def test_eq(): ls1 = LineSegment(P2(0, 0), P2(10, 10)) ls2 = LineSegment(P2(10, 10), P2(0, 0)) assert ls1 == ls1 assert ls1 == ls2 assert ls2 == ls1 assert ls1 != None assert None != ls1
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.a == 0 assert up_one.b == 1 assert up_one.c == 1
def test_line_segment_does_intersect(): ls = LineSegment(P2(0, 0), P2(10, 10)) assert ls.does_intersect(LineSegment(P2(0, 10), P2(10, 0))) assert not ls.does_intersect(LineSegment(P2(4, 5), P2(0, 10))) assert ls.does_intersect(LineSegment(P2(10, 10), P2(20, 20))) assert ls.does_intersect(LineSegment(P2(0, 0), P2(10, 10)))
def test_line_segment_rotate(): ls = LineSegment(P2(-10, -10), P2(10, 10)) assert ls.rotate(math.pi / 2) == LineSegment(P2(-10, 10), P2(10, -10)) ls = LineSegment(P2(0, 0), P2(10, 10)) assert ls.rotate(math.pi / 2, P2(5, 5)) == LineSegment(P2(0, 10), P2(10, 0))
def test_p2_rotate(): point = P2(2, 2) assert point.rotate(math.pi).approx(P2(-2, -2)) assert point.rotate(math.pi, P2(1, 1)).approx(P2(0, 0)) with pytest.raises(TypeError): point.rotate(point) with pytest.raises(TypeError): point.rotate(None)
def test_line_segment_center(): ls = LineSegment(P2(-10, 10), P2(10, 10)) assert P2(0, 10) == ls.center() ls = LineSegment(P2(10, -10), P2(10, 10)) assert P2(10, 0) == ls.center() ls = LineSegment(P2(-10, -10), P2(10, 10)) assert P2(0, 0) == ls.center()
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)) l3 = Line(1, 1, 20) assert set() == intersect(l1, l3) with pytest.raises(ValueError): intersect(l1, l1)
def test_intersection2(): l1 = Line(1, 1, 0) l2 = Line(1, -1, 0) assert l1.intersect(l2).approx(P2(0, 0)) assert l2.intersect(l1).approx(P2(0, 0)) l3 = Line(1, 1, 1) assert l1.intersect(l3) is None with pytest.raises(TypeError): l1.intersect(None)
def test_p2_as_vector(): p = P2(100, 200) vector = p.vector() assert isinstance(vector, V2) assert vector.x == 100 assert vector.y == 200
def test_parallel_xy(): line = Line(1, 1, 0) up_and_over = line.parallel(P2(1, 1)) assert up_and_over.a == 0.5 assert up_and_over.b == 0.5 assert up_and_over.c == 1
def test_circle_create(): circle = Circle(100) assert circle.radius == 100 assert circle.center == P2(0, 0) circle = Circle(100, P2(100, 100)) assert circle.radius == 100 assert circle.center == P2(100, 100) with pytest.raises(TypeError): Circle(100, V2(100, 100)) with pytest.raises(TypeError): Circle(None)
def test_contains(): line = Line(1, 1, 0) assert line.contains(P2(0, 0)) assert line.contains(P2(-1, 1)) assert line.contains(P2(1, -1)) assert P2(0, 0) in line assert not line.contains(P2(0, 1)) with pytest.raises(TypeError): line.contains(V2(0, 0)) with pytest.raises(TypeError): line.contains(None)
def test_does_intersect_line_segment_line_segment(): ls45 = LineSegment(P2(-1, -1), P2(1, 1)) ls135 = LineSegment(P2(-1, 1), P2(1, -1)) assert does_intersect(ls45, ls135) ls135_up2 = LineSegment(P2(-1, 3), P2(1, 1)) assert does_intersect(ls45, ls135_up2) ls135_up3 = LineSegment(P2(-1, 4), P2(1, 2)) assert not does_intersect(ls45, ls135_up3)
def test_on_perimeter(): circle = Circle(100) assert not circle.on_circumference(P2(0, 0)) assert circle.on_circumference(P2(100, 0)) assert circle.on_circumference(P2(0, 100)) circle = Circle(100, P2(1, 1)) assert not circle.on_circumference(P2(0, 0)) assert circle.on_circumference(P2(1, -99)) assert circle.on_circumference(P2(-99, 1)) assert circle.on_circumference(P2(101, 1)) assert circle.on_circumference(P2(1, 101))
def test_centroid_odd(): expected = P2(10, 10) polygon = Polygon( [P2(7, 9), P2(9, 9), P2(9, 7), P2(13, 11), P2(11, 11), P2(11, 13)] ) assert expected.approx(polygon.centroid())
def test_line_segment_line_segment(): ls1 = LineSegment(P2(0, 0), P2(10, 10)) ls2 = LineSegment(P2(0, 10), P2(10, 0)) assert {P2(5, 5)} == intersect(ls1, ls2) assert {P2(5, 5)} == intersect(ls2, ls1) with pytest.raises(NotImplementedError): intersect(ls1, None) assert set() == intersect(ls1, LineSegment(P2(0, 5), P2(5, 10)))
def test_line_segment_line(): ls = LineSegment(P2(0, 0), P2(10, 10)) line = ls.line() assert line.a == -1 assert line.b == 1 assert line.c == 0 assert line == LineSegment(P2(-10, -10), P2(10, 10)).line() assert line == LineSegment(P2(100, 100), P2(200, 200)).line() assert line != LineSegment(P2(-10, -10), P2(1, 0)).line()
def test_translate(): line = Line(2, 1, 2) assert line.f_of_x(0) == pytest.approx(2) assert line.f_of_y(0) == pytest.approx(1) translated = line.translate(V2(1, 1)) assert translated.f_of_x(0) == pytest.approx(3) assert translated.f_of_y(0) == pytest.approx(2) horz = Line.ByPoints(P2(-1, 10), P2(1, 10)) assert horz.f_of_x(0) == pytest.approx(10) translated = horz.translate(V2(1, 1)) assert translated.f_of_x(0) == pytest.approx(11) vert = Line.ByPoints(P2(10, -1), P2(10, 1)) assert vert.f_of_y(0) == pytest.approx(10) translated = vert.translate(V2(1, 1)) assert translated.f_of_y(0) == pytest.approx(11)
def test_degenerate_xy_of_cases(): vert = Line.ByPoints(P2(1, 1), P2(1, -1)) assert vert.f_of_x(1) is None assert vert.f_of_x(-100) is None assert vert.f_of_y(-1) == pytest.approx(1) assert vert.f_of_y(1) == pytest.approx(1) assert vert.f_of_y(-100) == pytest.approx(1) assert vert.f_of_y(100) == pytest.approx(1) horz = Line.ByPoints(P2(-1, 0), P2(1, 0)) assert horz.f_of_x(-1) == pytest.approx(0) assert horz.f_of_x(1) == pytest.approx(0) assert horz.f_of_x(-100) == pytest.approx(0) assert horz.f_of_x(100) == pytest.approx(0) assert horz.f_of_y(-1) is None assert horz.f_of_y(1) is None assert horz.f_of_y(-100) is None assert horz.f_of_y(100) is None
def test_create(): ls1 = LineSegment(P2(10, 10), P2(0, 0)) assert ls1.p1.approx(P2(10, 10)) assert ls1.p2.approx(P2(0, 0)) with pytest.raises(ValueError): LineSegment(P2(10, 10), P2(10, 10))