def test_rotating_a_point_around_the_x_axis():
    # Given
    p = Point(0, 1, 0)
    half_quarter = rotation_x(pi / 4)
    full_quarter = rotation_x(pi / 2)
    # Then
    assert half_quarter * p == Point(0, sqrt(2) / 2, sqrt(2) / 2)
    assert full_quarter * p == Point(0, 0, 1)
def test_the_inverse_of_an__x_rotation__rotates_in_the_opposite_direction():
    # Given
    p = Point(0, 1, 0)
    half_quarter = rotation_x(pi / 4)
    inv = half_quarter.inverse()
    # Then
    assert inv * p == Point(0, sqrt(2) / 2, -sqrt(2) / 2)
def test_chained_transformations_must_be_applied_in_reverse_order():
    # Given
    p = Point(1, 0, 1)
    A = rotation_x(pi / 2)
    B = scaling(5, 5, 5)
    C = translation(10, 5, 7)
    # When
    T = C * B * A
    # Then
    assert T * p == Point(15, 0, 7)
def test_individual_transformations_are_applied_in_sequence():
    # Given
    p = Point(1, 0, 1)
    A = rotation_x(pi / 2)
    B = scaling(5, 5, 5)
    C = translation(10, 5, 7)

    # apply rotation first
    # When
    p2 = A * p
    # Then
    assert p2 == Point(1, -1, 0)

    # then apply scaling
    # When
    p3 = B * p2
    # Then
    assert p3 == Point(5, -5, 0)

    # then apply translation
    # When
    p4 = C * p3
    # Then
    assert p4 == Point(15, 0, 7)
Пример #5
0
if __name__ == "__main__":
    # pattern = StripePattern(Color(1, 0, 0), Color(1, 1, 1))
    # pattern = CheckersPattern(Color(1, 0, 0), Color(1, 1, 1))
    pattern = GradientPattern(Color(1, 0, 0), Color(1, 1, 1))
    # pattern = RingPattern(Color(1, 0, 0), Color(1, 1, 1))
    pattern.transform = scaling(0.2, 0.2, 0.2)

    floor = Plane()
    floor.material = Material()
    floor.material.color = Color(1, 0.9, 0.9)
    # floor.material.pattern = pattern

    backdrop = Plane()
    backdrop.transform = \
        rotation_y(pi / 3) * translation(0, 0, 5) * rotation_x(pi / 2)

    backdrop2 = Plane()
    backdrop2.transform = \
        rotation_y(-pi / 3) * translation(0, 0, 5) * rotation_x(pi / 2)

    middle = Sphere()
    # middle = glass_sphere()
    middle.transform = translation(-0.5, 1, 0.5)
    # middle.reflective = 1.0
    middle.material = Material()
    middle.material.color = Color(0.1, 1, 0.5)
    # middle.material.pattern = pattern
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3
    # middle.material.reflective = 1.0