|
2 | 2 | from PIL import Image |
3 | 3 |
|
4 | 4 | path = "./rays.png" |
5 | | - |
6 | | -tmp = Image.new('RGB', (200, 200), "white") |
| 5 | +size = 300 |
| 6 | +tmp = Image.new('RGB', (size, size), "white") |
7 | 7 | rays = tmp.load() |
8 | 8 |
|
9 | | -def coordinates(num): |
| 9 | +def coordinates(num, sz = size): |
10 | 10 | """ |
11 | | - Take a number between 0 and 199 and returns the proportional value between -2 and 2 |
| 11 | + Take a number in range(size) and returns the proportional value between -2 and 2 |
12 | 12 | """ |
13 | | - return 8.0*num/199.0 - 2.0 |
14 | | - |
15 | | - |
16 | | -S = Sphere((0, 0, 0), 0.5, (0, 0, 0)) |
17 | | -origin = (-10, 0, 0) |
| 13 | + return 4.0*num/(sz - 1) - 2.0 |
18 | 14 |
|
| 15 | +# Creating the world |
| 16 | +S1 = Sphere((1, 0, 0), 0.5, (10, 220, 0)) |
| 17 | +S2 = Sphere((0.6, 0, 0), 0.5, (200, 10, 200)) |
| 18 | +camera = (0, 0, -8) |
| 19 | +world = [S1, S2] |
19 | 20 |
|
20 | | -for i in range(200): |
| 21 | +# Colouring each pixel |
| 22 | +for i in range(size): |
21 | 23 | x0 = coordinates(i) |
22 | | - for j in range(200): |
| 24 | + for j in range(size): |
23 | 25 | y0 = coordinates(j) |
24 | | - l = Line.throughPoints(origin, (x0, y0, -5)) |
25 | | - rays[i, j] = S.intersect(l) |
| 26 | + l = Line.throughPoints(camera, (x0, y0, -5)) |
| 27 | + ans = [] |
| 28 | + for S in world: |
| 29 | + if S.intersect(l): |
| 30 | + ans.append((S.intersection_value(l), S)) |
| 31 | + if not ans: |
| 32 | + rays[i, j] = (255, 255, 255) |
| 33 | + else: |
| 34 | + ans.sort() |
| 35 | + rays[i, j] = ans[0][1].color |
| 36 | + |
26 | 37 |
|
27 | 38 | tmp.save(path) |
0 commit comments