Пример #1
0

class Projectile:
    def __init__(self, p, v):
        self.position = p
        self.velocity = v


def tick(w: World, p: Projectile) -> Projectile:
    pos = p.position + p.velocity
    velocity = p.velocity + w.gravity + w.wind
    return Projectile(pos, velocity)


start = base.Point(0, 1, 0)
velocity = base.Vector(1, 1.8, 0).normalize() * 11.25
p = Projectile(start, velocity)
gravity = base.Vector(0, -0.1, 0)
wind = base.Vector(-0.01, 0, 0)
w = World(gravity, wind)
c = base.Canvas(900, 550)
c1 = base.Color(1, 0, 0)
t = 0
while p.position.y > 0:
    c.write_pixel(int(p.position.x), 550 - int(p.position.y), c1)
    p = tick(w, p)
    t += 1

with open("image.ppm", "w") as f:
    f.write(c.to_ppm())
Пример #2
0
from raytracer import base, rays, shapes, materials, lights, patterns

CANVAS_PIXELS = 256
COLOR = base.Color(1, 0, 0)

shape = shapes.Sphere()
shape.material = materials.Material()
shape.material.color = base.Color(0.1, 1, 0)
shape.material.pattern = patterns.StripePattern(base.Color(1, 1, 1),
                                                base.Color(0, 0, 0))

light_position = base.Point(-10, 10, -10)
light_color = base.Color(1, 1, 1)
light = lights.PointLight(light_position, light_color)

ray_origin = base.Point(0, 0, -5)
wall_z = 10
wall_size = 7

pixel_size = wall_size / CANVAS_PIXELS
half = wall_size / 2

c = base.Canvas(CANVAS_PIXELS, CANVAS_PIXELS)
for y in range(CANVAS_PIXELS):
    print(y)
    world_y = half - pixel_size * y
    for x in range(CANVAS_PIXELS):
        world_x = -half + pixel_size * x

        position = base.Point(world_x, world_y, wall_z)
Пример #3
0
import raytracer.base as rt
import raytracer.rays as rays
from raytracer.spheres import Sphere

WALL_SIZE = 7
CANVAS_SIZE = 100
PIXEL_SIZE = WALL_SIZE / CANVAS_SIZE
HALF = WALL_SIZE / 2

canvas = rt.Canvas(CANVAS_SIZE, CANVAS_SIZE)
c = rt.Color(1, 0, 0)
s = Sphere()
ray_origin = rt.Point(0, 0, -5)
wall_z = 10

for y in range(0, CANVAS_SIZE):
    world_y = HALF - PIXEL_SIZE * y
    for x in range(0, CANVAS_SIZE):
        world_x = -HALF + PIXEL_SIZE * x
        position = rt.Point(world_x, world_y, wall_z)
        v = position - ray_origin
        r = rays.Ray(ray_origin, v.normalize())
        xs = r.intersects(s)
        if xs.hit():
            canvas.write_pixel(x, y, c)

with open("sphere.ppm", "w") as ppm_file:
    ppm_file.write(canvas.to_ppm())
Пример #4
0
import raytracer.base as rt
import math

HOUR_ROTATION = math.pi / 6
CANVAS_SIZE = 80
RADIUS = 30

twelve = rt.Point(0, 0, 1)
c = rt.Canvas(80, 80)
color = rt.Color(1, 1, 1)

# start at 12 and compute the other 11 hour points
for hour in range(0, 12):
    p = rt.RotationY(hour * HOUR_ROTATION) * twelve
    x = int(p.x * RADIUS) + 40
    y = int(p.z * RADIUS) + 40
    print(f"{hour}: {x},{y}")
    c.write_pixel(x, y, color)

with open("clock.ppm", "w") as ppm_file:
    ppm_file.write(c.to_ppm())