예제 #1
0
    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
예제 #2
0
    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
예제 #3
0
    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"
예제 #4
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)

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()
예제 #5
0
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()