Ejemplo n.º 1
0
    def test_translation_does_not_affect_vectors(self):
        transform = translation(5, -3, 2)
        vector = Coordinates.vector(-3, 4, 5)

        result = transform * vector

        assert result == vector
Ejemplo n.º 2
0
    def test_multiplying_translation_matrix(self):
        transform = translation(5, -3, 2)
        point = Coordinates.point(-3, 4, 5)

        result = transform * point

        assert result == Coordinates.point(2, 1, 7)
Ejemplo n.º 3
0
    def test_multiplying_by_inverse_of_translation_matrix(self):
        transform = translation(5, -3, 2)
        inverse = transform.inverse()
        point = Coordinates.point(-3, 4, 5)

        result = inverse * point

        assert result == Coordinates.point(-8, 7, 3)
Ejemplo n.º 4
0
    def test_chaining_transformations_must_be_applied_in_reverse_order(self):
        point = Coordinates.point(1, 0, 1)
        rotate = rotation_x(radians(90))
        scale = scaling(5, 5, 5)
        translate = translation(10, 5, 7)

        transforms = translate * scale * rotate
        result = transforms * point

        assert result == Coordinates.point(15, 0, 7)
Ejemplo n.º 5
0
    def test_individual_transformations_are_applied_in_sequence(self):
        point = Coordinates.point(1, 0, 1)
        rotate = rotation_x(radians(90))
        scale = scaling(5, 5, 5)
        translate = translation(10, 5, 7)

        rotated_point = rotate * point
        scaled_point = scale * rotated_point
        translated_point = translate * scaled_point

        assert rounded(rotated_point) == rounded(Coordinates.point(1, -1, 0))
        assert rounded(scaled_point) == rounded(Coordinates.point(5, -5, 0))
        assert translated_point == Coordinates.point(15, 0, 7)
Ejemplo n.º 6
0
from math import radians

from drawing import Canvas
from drawing.formats import PPMFormat
from drawing.transformations import translation, rotation_z, rotation_y, rotation_x
from data_structures import Coordinates, Color

width = 900
height = 900

canvas = Canvas(width, height)
degrees_of_rotations = 360 / 12
radius = (height / 2) * 0.8

point = Coordinates.point(0, 0, 0)
translate = translation(width / 2, height / 2, 0)

for i in range(12):
    colors = [Color(1, 0, 0), Color(0, 1, 0), Color(0, 0, 1)]
    degrees = i * degrees_of_rotations
    print(f"degrees: {degrees}")
    rotate = rotation_z(radians(degrees))
    rotated_point = rotate * (translate * point)

    print(
        f"rotated_point: {rotated_point.x} {rotated_point.y} {rotated_point.z}"
    )

    canvas.write_pixel(int(rotated_point.x), int(rotated_point.y),
                       colors[i % 3])