def test_scaling_matrix_applied_to_vector(self): transform = scaling(2, 3, 4) vector = Coordinates.vector(-4, 6, 8) result = transform * vector assert result == Coordinates.vector(-8, 18, 32)
def test_multiply_by_inverse_of_scaling_matrix(self): transform = scaling(2, 3, 4).inverse() vector = Coordinates.vector(-4, 6, 8) result = transform * vector assert result == Coordinates.vector(-2, 2, 2)
def test_subtracting_vector_from_zero(self): v1 = Coordinates.vector(0, 0, 0) v2 = Coordinates.vector(1, -2, 3) result = v1 - v2 assert result == Coordinates.vector(-1, 2, -3)
def test_dot_product(self): v1 = Coordinates.vector(1, 2, 3) v2 = Coordinates.vector(2, 3, 4) result = v1.dot_product(v2) assert result == 20
def test_subtract_vectors(self): v1 = Coordinates.vector(3, 2, 1) v2 = Coordinates.vector(5, 6, 7) result = v1 - v2 assert result == Coordinates.vector(-2, -4, -6) assert result.is_vector()
def test_cross_product(self): v1 = Coordinates.vector(1, 2, 3) v2 = Coordinates.vector(2, 3, 4) result1 = v1.cross_product(v2) result2 = v2.cross_product(v1) assert result1 == Coordinates.vector(-1, 2, -1) assert result2 == Coordinates.vector(1, -2, 1)
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
def test_subtract_vector_from_point(self): point = Coordinates.point(3, 2, 1) vector = Coordinates.vector(5, 6, 7) result = point - vector assert result == Coordinates.point(-2, -4, -6) assert result.is_point()
def test_subtract_points(self): p1 = Coordinates.point(x=3, y=2, z=1) p2 = Coordinates.point(x=5, y=6, z=7) result = p1 - p2 assert result == Coordinates.vector(x=-2, y=-4, z=-6) assert result.is_vector()
def test_normalize_vector_1_2_3(self): v = Coordinates.vector(1, 2, 3) result = v.normalize() assert round(result.x, 5) == 0.26726 assert round(result.y, 5) == 0.53452 assert round(result.z, 5) == 0.80178
from data_structures import Coordinates from data_structures import Color from drawing import Canvas from drawing.formats import PPMFormat # Challenge for end of chapter 1 projectile = { "position": Coordinates.point(0, 1, 0), "velocity": Coordinates.vector(1, 1.8, 0).normalize() * 11.25, } environment = { "gravity": Coordinates.vector(0, -0.1, 0), "wind": Coordinates.vector(-0.01, 0, 0), } def tick(projectile, environment): position = projectile["position"] + projectile["velocity"] velocity = projectile["velocity"] + environment["gravity"] + environment["wind"] return {"position": position, "velocity": velocity} projectiles = [projectile] while projectile["position"].y > 0: projectile = tick(projectile, environment) projectiles.append(projectile) red = Color(1, 0, 0)
def test_vector_conveniece_constructor(self): coordinates = Coordinates.vector(1, 2, 3) assert not coordinates.is_point() assert coordinates.is_vector()
def test_magnitude_of_normalized_vector_is_1(self): v = Coordinates.vector(1, 2, 3) result = v.normalize().magnitude() assert result == 1.0
def test_normalize_vector_4_0_0(self): v = Coordinates.vector(4, 0, 0) result = v.normalize() assert result == Coordinates.vector(1, 0, 0)
def test_magnitude_of_vector_1_2_3(self): v = Coordinates.vector(1, 2, 3) result = v.magnitude() assert result == sqrt(14)
def test_magnitude_of_vector_0_0_1(self): v = Coordinates.vector(1, 0, 0) result = v.magnitude() assert result == 1.0