Пример #1
0
def test_v2_approx():
    v1 = V2(5, 10)

    assert v1.approx(v1)

    assert v1.approx(V2(5.0000000001, 10.0000000001))

    assert not v1.approx(V2(5.0000001, 10.0000001))

    assert v1.approx(V2(5.01, 10.01), atol=0.1)
Пример #2
0
def test_v2_dot():
    v1 = V2(2, 3)
    v2 = V2(4, 5)

    assert v1.dot(v2) == 2 * 4 + 3 * 5

    with pytest.raises(TypeError):
        v1.dot(1)

    with pytest.raises(TypeError):
        v1.dot(None)
Пример #3
0
def test_v2_cross():
    v1 = V2(2, 3)
    v2 = V2(4, 5)

    assert v1.cross(v2) == 2 * 5 - 3 * 4
    assert v2.cross(v1) == 4 * 3 - 2 * 5

    with pytest.raises(TypeError):
        v1.cross(1)

    with pytest.raises(TypeError):
        v1.cross(None)
Пример #4
0
def test_v2_rotated():
    v1 = V2(1, 0)

    assert v1.rotated(math.pi / 2).approx(V2(0, 1))
    assert v1.rotated(math.pi).approx(V2(-1, 0))
    assert v1.rotated(math.pi * 3 / 2).approx(V2(0, -1))
    assert v1.rotated(2 * math.pi).approx(v1)

    with pytest.raises(TypeError):
        v1.rotated(None)

    with pytest.raises(TypeError):
        v1.rotated(v1)
Пример #5
0
def test_v2_as_point():
    vector = V2(100, 200)

    point = vector.point()
    assert isinstance(point, P2)
    assert point.x == 100
    assert point.y == 200
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
def test_v2_sub():
    v1 = V2(100, 200)
    v2 = V2(1, 2)

    v3 = v1 - v2
    assert v3.x == 99
    assert v3.y == 198

    v3 = v2 - v1
    assert v3.x == -99
    assert v3.y == -198

    with pytest.raises(TypeError):
        v1 - 1

    with pytest.raises(TypeError):
        1 - v1

    with pytest.raises(TypeError):
        v1 - None

    with pytest.raises(TypeError):
        None - v1
Пример #9
0
def test_v2_add():
    v1 = V2(100, 200)
    v2 = V2(1, 2)

    v3 = v1 + v2
    assert v3.x == 101
    assert v3.y == 202

    v3 = v2 + v1
    assert v3.x == 101
    assert v3.y == 202

    with pytest.raises(TypeError):
        v1 + 1

    with pytest.raises(TypeError):
        1 + v1

    with pytest.raises(TypeError):
        v1 + None

    with pytest.raises(TypeError):
        None + v1
Пример #10
0
def test_v2_scaled():
    v1 = V2(10, 20)

    vtest = v1.scaled(10)
    assert vtest.x == 100
    assert vtest.y == 200

    vtest = v1.scaled(1 / 10)
    assert vtest.x == 1
    assert vtest.y == 2

    with pytest.raises(TypeError):
        v1.scaled(v1)

    with pytest.raises(TypeError):
        v1.scaled(None)
Пример #11
0
def test_circle_contains():
    circle = Circle(100)

    assert circle.contains(P2(-100, 0))
    assert circle.contains(P2(100, 0))
    assert circle.contains(P2(0, 100))
    assert circle.contains(P2(0, -100))

    assert not circle.contains(P2(150, 0))
    assert not circle.contains(P2(0, 150))

    with pytest.raises(TypeError):
        circle.contains(None)

    with pytest.raises(TypeError):
        circle.contains(V2(0, 0))
Пример #12
0
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)
Пример #13
0
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)
Пример #14
0
def test_v2_mul():
    v1 = V2(5, 10)

    v2 = v1 * 10
    assert v2.x == 50
    assert v2.y == 100

    v2 = 10 * v1
    assert v2.x == 50
    assert v2.y == 100

    with pytest.raises(TypeError):
        v1 * v1

    with pytest.raises(TypeError):
        v1 * None

    with pytest.raises(TypeError):
        None * v1
Пример #15
0
def test_line_segment_contains():
    ls = LineSegment(P2(-10, -10), P2(10, 10))
    assert ls.contains(P2(0, 0))

    assert ls.contains(P2(5, 5))
    assert ls.contains(P2(-10, -10))
    assert ls.contains(P2(10, 10))

    assert not ls.contains(P2(-11, -11))
    assert not ls.contains(P2(11, 11))
    assert not ls.contains(P2(1, 0))

    with pytest.raises(TypeError):
        ls.contains(V2(10, 10))

    with pytest.raises(TypeError):
        ls.contains(None)

    ls = LineSegment(P2(0, 0), P2(9999999999.9, 9999999999.9))
    assert ls.contains(P2(0, 0))
    assert ls.contains(P2(9999999999.9, 9999999999.9))
Пример #16
0
def test_v2_div():
    v1 = V2(5, 10)

    v2 = v1 / 5
    assert v2.x == 1
    assert v2.y == 2

    v2 = 5 / v1
    assert v2.x == 1
    assert v2.y == 0.5

    with pytest.raises(TypeError):
        v1 / v1

    with pytest.raises(TypeError):
        v1 / None

    with pytest.raises(TypeError):
        None / v1

    with pytest.raises(ZeroDivisionError):
        v1 / 0
Пример #17
0
def test_polygon_translate(cw_polygon, ccw_polygon):
    assert P2(0.5, 0.5) == cw_polygon.centroid()
    assert P2(1.5, 1.5) == cw_polygon.translate(V2(1, 1)).centroid()
    assert P2(0.5, 0.5) == ccw_polygon.centroid()
    assert P2(1.5, 1.5) == ccw_polygon.translate(V2(1, 1)).centroid()
Пример #18
0
def test_line_segment_translate():
    ls = LineSegment(P2(-10, -10), P2(10, 10))
    assert LineSegment(P2(-9, -9), P2(11, 11)) == ls.translate(V2(1, 1))
    ls = LineSegment(P2(10, 10), P2(-10, -10))
    assert LineSegment(P2(11, 11), P2(-9, -9)) == ls.translate(V2(1, 1))
Пример #19
0
def test_line_segment_vector():
    ls = LineSegment(P2(0, 0), P2(10, 10))
    assert V2(10, 10) == ls.vector()

    ls = LineSegment(P2(10, 10), P2(0, 0))
    assert V2(-10, -10) == ls.vector()
Пример #20
0
def test_v2_create():
    vector = V2(100, 200)

    assert vector.x == 100
    assert vector.y == 200