def main():
    c = Canvas(500, 500)

    p = point(0, 0, 1)

    translate = translation(250, 0, 250)
    scale = scaling(100, 0, 100)

    for h in range(12):
        r = rotation_y(h * pi / 6)
        transform = multiply_matrix(translate, multiply_matrix(scale, r))
        p2 = multiply_tuple(transform, p)
        print(f"position ({p2[0]}, {p2[1]}, {p2[2]})")
        c.set_pixel(round(p2[0]), c.height - round(p2[2]),
                    color(0.0, 1.0, 0.0))

    with open('clock.ppm', 'w') as out_file:
        out_file.write(c.to_ppm())
Example #2
0
def main():
    print("What happens when you invert the identity matrix?")
    a = identity_matrix()
    print(inverse(a))

    print()
    print("What do you get when you multiply a matrix by its inverse?")
    a = identity_matrix()
    a[1][0] = 2
    a[0][3] = -5
    b = multiply_matrix(a, inverse(a))
    print(b)

    print()
    print(
        "Is there any difference between the inverse of the transpose of a matrix, and the transpose of the inverse?"
    )
    c = inverse(transpose(a))
    d = transpose(inverse(a))
    print(c)
    print(d)

    print()
    print(
        "Remember how multiplying the identity matrix by a tuple gives you the tuple, unchanged?"
    )
    t = tuple_4d(5, 4, 3, 1)
    i = identity_matrix()
    c = multiply_tuple(i, t)
    print(c)

    print()
    print(
        "Now, try changing any single element of the identity matrix to a different number, and then multiplying it by a tuple. What happens to the tuple?"
    )
    i[1][1] = 2
    i[2][2] = 3
    c = multiply_tuple(i, t)
    print(c)
def step_impl(context):
    assert multiply_matrix(context.a, identity_matrix()) == context.a
def step_impl(context):
    expected = matrix(4, 4)
    for r in range(4):
        for c in range(4):
            expected[r][c] = float(context.table[r][c])
    assert multiply_matrix(context.a, context.b) == expected
def step_impl(context):
    context.c = multiply_matrix(context.m, context.b)
def step_impl(context):
    actual = multiply_matrix(context.c, inverse(context.b))
    expected = context.m
    assert_matrix(actual, expected)
def step_impl(context):
    context.transform = multiply_matrix(
        context.C, multiply_matrix(context.B, context.full_quarter))
def step_assert_transformation_matrix_of_object_at_index(
    context, index, tx, ty, tz, sx, sy, sz):
    assert_matrix(
        context.w.objects[index].transform(),
        multiply_matrix(scaling(sx, sy, sz), translation(tx, ty, tz)))
Example #9
0
def step_create_scaling_rotation_z_matrix_m(context):
    context.m = multiply_matrix(scaling(1, 0.5, 1), rotation_z(pi / 5))
Example #10
0
def render_scene_with_plane():
    floor = Plane()
    floor.set_transform(scaling(10, 0.01, 10))
    # floor.set_transform(
    # multiply_matrix(translation(0, 0.33, 0), scaling(10, 0.01, 10)))
    floor.material = Material()
    floor.material.color = color(1, 0.9, 0.9)
    floor.material.specular = 0

    # left_wall = Plane()
    # left_wall.set_transform(
    #     multiply_matrix(
    #         translation(0, 0, 5),
    #         multiply_matrix(rotation_y(-pi / 4),
    #                  multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10)))))
    # left_wall.material = floor.material

    # right_wall = Plane()
    # right_wall.set_transform(
    #     multiply_matrix(
    #         translation(0, 0, 5),
    #         multiply_matrix(rotation_y(pi / 4),
    #                  multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10)))))

    middle = Sphere()
    middle.set_transform(translation(-0.5, 1, 0.5))
    middle.material = Material()
    middle.material.pattern = StripePattern(color(0.6, 1.0, 0.6),
                                            color(0.3, 0.6, 0.3))
    middle.material.color = color(0.1, 1, 0.5)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3

    middle.material.pattern.set_transform(
        multiply_matrix(scaling(0.2, 0.2, 0.2), rotation_y(pi / 4)))

    right = Sphere()
    right.set_transform(
        multiply_matrix(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5)))
    right.material = Material()
    right.material.color = color(0.5, 1, 0.1)
    # right.material.diffuse = 0.7
    right.material.diffuse = 0.2
    right.material.specular = 0.3
    right.material.reflective = 0.7

    left = Sphere()
    left.set_transform(
        multiply_matrix(translation(-1.5, 0.33, -0.75),
                        scaling(0.33, 0.33, 0.33)))
    left.material = Material()
    left.material.color = color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3

    world = World()
    world.add_light(PointLight(point(-10, 10, -10), color(1, 1, 1)))
    world.objects.append(floor)
    # world.objects.append(left_wall)
    # world.objects.append(right_wall)
    world.objects.append(middle)
    world.objects.append(right)
    world.objects.append(left)

    camera = Camera(600, 500, pi / 3)
    camera.set_transform(
        view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0)))
    # camera.set_transform(
    # view_transform(point(0, 4, -1), point(0, 1, 0), vector(0, 1, 0)))

    canvas = RayTracer().render(camera, world)
    ppm = canvas.to_ppm()
    outf = open('render_scene_with_plane.ppm', 'w')
    outf.write(ppm)
Example #11
0
def step_set_transform_of_c_to_transformation_matrix(context):
    context.c.set_transform(
        multiply_matrix(rotation_y(pi / 4), translation(0, -2, 5)))
def render_simple_scene():
    floor = Sphere()
    floor.set_transform(scaling(10, 0.01, 10))
    floor.material = Material()
    floor.material.color = color(1, 0.9, 0.9)
    floor.material.specular = 0

    left_wall = Sphere()
    left_wall.set_transform(
        multiply_matrix(
            translation(0, 0, 5),
            multiply_matrix(
                rotation_y(-pi / 4),
                multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10)))))
    left_wall.material = floor.material

    right_wall = Sphere()
    right_wall.set_transform(
        multiply_matrix(
            translation(0, 0, 5),
            multiply_matrix(
                rotation_y(pi / 4),
                multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10)))))
    right_wall.material = floor.material

    middle = Sphere()
    middle.set_transform(translation(-0.5, 1, 0.5))
    middle.material = Material()
    middle.material.color = color(0.1, 1, 0.5)
    middle.material.diffuse = 0.7
    middle.material.specular = 0.3

    right = Sphere()
    right.set_transform(
        multiply_matrix(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5)))
    right.material = Material()
    right.material.color = color(0.5, 1, 0.1)
    right.material.diffuse = 0.7
    right.material.specular = 0.3

    left = Sphere()
    left.set_transform(
        multiply_matrix(translation(-1.5, 0.33, -0.75),
                        scaling(0.33, 0.33, 0.33)))
    left.material = Material()
    left.material.color = color(1, 0.8, 0.1)
    left.material.diffuse = 0.7
    left.material.specular = 0.3

    world = World()
    world.add_light(PointLight(point(-10, 10, -10), color(1, 1, 1)))
    world.objects.append(floor)
    world.objects.append(left_wall)
    world.objects.append(right_wall)
    world.objects.append(middle)
    world.objects.append(right)
    world.objects.append(left)

    camera = Camera(600, 500, pi / 3)
    camera.set_transform(
        view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0)))

    canvas = RayTracer().render(camera, world)

    with open('render_simple_scene.ppm', 'w') as out_file:
        out_file.write(canvas.to_ppm())