-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphere.py
More file actions
29 lines (24 loc) · 763 Bytes
/
sphere.py
File metadata and controls
29 lines (24 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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())