def test_adding_colors(self): c1 = Color(0.9, 0.6, 0.75) c2 = Color(0.7, 0.1, 0.25) result = c1 + c2 assert result == Color(1.6, 0.7, 1.0)
def test_mult_by_other_color(self): c1 = Color(1, 0.2, 0.4) c2 = Color(0.9, 1, 0.1) result = c1 * c2 assert round(result.red, 2) == 0.9 assert round(result.green, 2) == 0.2 assert round(result.blue, 2) == 0.04
def test_subtracting_colors(self): c1 = Color(0.9, 0.6, 0.75) c2 = Color(0.7, 0.1, 0.25) result = c1 - c2 assert round(result.red, 2) == 0.2 assert round(result.green, 2) == 0.5 assert round(result.blue, 2) == 0.5
def test_all_lines_are_less_than_seventy_characters(self): canvas = Canvas(5, 3) c1 = Color(1.5, 0, 0) c2 = Color(0, 0.5, 0) c3 = Color(-0.5, 0, 1) canvas.write_pixel(0, 0, c1) canvas.write_pixel(2, 1, c2) canvas.write_pixel(4, 2, c3) lines = PPMFormat(canvas).lines() for line in lines: assert len(line) < 70
def test_constructing_pixel_data(self): canvas = Canvas(5, 3) c1 = Color(1.5, 0, 0) c2 = Color(0, 0.5, 0) c3 = Color(-0.5, 0, 1) canvas.write_pixel(0, 0, c1) canvas.write_pixel(2, 1, c2) canvas.write_pixel(4, 2, c3) lines = PPMFormat(canvas).lines() assert lines[3] == "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0" assert lines[4] == "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0" assert lines[5] == "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255"
def test_writing_pixel_to_canvas(self): canvas = Canvas(10, 20) red = Color(1, 0, 0) canvas.write_pixel(2, 3, red) assert canvas.pixel_at(2, 3) == red
} 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) canvas = Canvas(900, 550) for projectile in projectiles: position = projectile["position"] if ( # position inbounds of canvas position.x > 0 and position.x <= canvas.width and position.y > 0 and position.y <= canvas.height ): canvas.write_pixel(int(position.x), int(canvas.height - position.y), red) PPMFormat(canvas).write()
def test_initialization(self): color = Color(-0.5, 0.4, 1.7) assert color.red == -0.5 assert color.green == 0.4 assert color.blue == 1.7
def test_multiplying_by_scalar(self): color = Color(0.2, 0.3, 0.4) result = color * 2 assert result == Color(0.4, 0.6, 0.8)
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]) PPMFormat(canvas).write()
def _initialize_pixels(self): self.pixels = [] for i in range(self.width * self.height): self.pixels.append(Color(0, 0, 0))
def test_initialization_all_pixels_set_to_black(self): canvas = Canvas(10, 20) for pixel in canvas: assert pixel == Color(0, 0, 0)