Beispiel #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
Beispiel #2
0
    def test_file_headers(self):
        canvas = Canvas(5, 3)
        formatter = PPMFormat(canvas)

        lines = formatter.lines()

        assert lines[0] == "P3"
        assert lines[1] == "5 3"
        assert lines[2] == "255"
Beispiel #3
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
Beispiel #4
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"
Beispiel #5
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()
Beispiel #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])
Beispiel #7
0
def main():
    canvas = Canvas()

    if len(sys.argv) > 1 and sys.argv[1] == "trace":
        clear = False
    else:
        clear = True

    actors = setup.generate_actors()
#    [
#            Actor(
#                position = Point(-8.0, 8.0),
#                velocity = Vector(-2.5, 0.0),
#                target = Point(0.0, 0.0)),
#            Actor(
#                position = Point(8.0, 8.0),
#                velocity = Vector(-2.5, 0),
#                target = Point(0.0, 0.0)),
#            Actor(
#                position = Point(-8.0, -8.0),
#                velocity = Vector(-2.5, 0.0),
#                target = Point(0.0, 0.0)),
#            Actor(
#                position = Point(8.0, -8.0),
#                velocity = Vector(-2.5, 0),
#                target = Point(0.0, 0.0)),
#            ]
    walls = [Wall(*i) for i in pm.walls]

    if pm.use_c_ext:
        optimised.add_actors(actors)

    timestep = pm.timestep
    timer = 0.0
    time_start = time()
    frames = 0
    canvas.clear_screen()

    while canvas.tick():
        
        if clear:
            canvas.clear_screen()

        canvas.draw_text("t = %.2f" % timer)

        canvas.draw_target(pm.actor.target)

#        if actors[0].time > 10:
#            return

        for w in walls:
            canvas.draw_wall(w)

        if pm.use_c_ext:
            optimised.update_actors()
            canvas.draw_actors()
        else:
            for a in actors:
                a.calculate_acceleration(walls, actors)

            for a in actors:
                a.update_position(timestep)
                #if a.has_escaped():
                    #actors.remove(a)
                    #continue

                canvas.draw_actor(a)
#            for w in walls:
#                P = w.projection(a.position)
#                canvas.draw_proj(P)
        
        canvas.update()
        timer += timestep
        frames += 1

    elapsed = time() - time_start
    print "%d frames in %f seconds. Avg %f fps" % (frames, elapsed, frames/elapsed)
Beispiel #8
0
    def test_initialization_of_width_and_height(self):
        canvas = Canvas(10, 20)

        assert canvas.width == 10
        assert canvas.height == 20
Beispiel #9
0
    def test_initialization_all_pixels_set_to_black(self):
        canvas = Canvas(10, 20)

        for pixel in canvas:
            assert pixel == Color(0, 0, 0)
Beispiel #10
0
def main():
    canvas = Canvas()

    if len(sys.argv) > 1 and sys.argv[1] == "trace":
        clear = False
    else:
        clear = True

    actors = setup.generate_actors()
    #    [
    #            Actor(
    #                position = Point(-8.0, 8.0),
    #                velocity = Vector(-2.5, 0.0),
    #                target = Point(0.0, 0.0)),
    #            Actor(
    #                position = Point(8.0, 8.0),
    #                velocity = Vector(-2.5, 0),
    #                target = Point(0.0, 0.0)),
    #            Actor(
    #                position = Point(-8.0, -8.0),
    #                velocity = Vector(-2.5, 0.0),
    #                target = Point(0.0, 0.0)),
    #            Actor(
    #                position = Point(8.0, -8.0),
    #                velocity = Vector(-2.5, 0),
    #                target = Point(0.0, 0.0)),
    #            ]
    walls = [
        Wall(-10, -10, 10, -10),
        Wall(-10, -10, -10, 10),
        Wall(-10, 10, 10, 10),
        Wall(10, -10, 10, 10),
        #            Wall(-20, -20, 20, -20),
        #            Wall(-20, -20, -20, 20),
        #            Wall(-20, 20, 20, 20),
        #            Wall(20, -20, 20, 20),
    ]

    timestep = pm.timestep
    canvas.clear_screen()

    while canvas.tick():

        if clear:
            canvas.clear_screen()

        canvas.draw_text("t = %.2f" % actors[0].time)

        canvas.draw_target(pm.actor.target)

        #        if actors[0].time > 10:
        #            return

        for w in walls:
            canvas.draw_wall(w)

        if pm.use_c_ext:
            optimised.update_actors(actors, walls)
            for a in actors:
                canvas.draw_actor(a)
        else:
            for a in actors:
                a.calculate_acceleration(walls, actors)

            for a in actors:
                a.update_position(timestep)
                # if a.has_escaped():
                # actors.remove(a)
                # continue

                canvas.draw_actor(a)
        #            for w in walls:
        #                P = w.projection(a.position)
        #                canvas.draw_proj(P)

        canvas.update()
Beispiel #11
0
    def test_last_line_in_new_line_character(self):
        canvas = Canvas(5, 3)
        lines = PPMFormat(canvas).lines()

        assert lines[-1] == "\n"