@@ -14,31 +14,43 @@ def coordinates(num, sz = size):
1414
1515def create_world ():
1616 S1 = Sphere ((4 , 4 , 2 ), 2 , (100 , 220 , 0 ))
17- S2 = Sphere ((7 , 7 , 1 ), 1 , (200 , 10 , 200 ))
17+ S2 = Sphere ((2 , 2 , 1 ), 1 , (0 , 100 , 200 ))
1818 S3 = Sphere ((3 , 1 , 1 ), 1 , (100 , 100 , 200 ))
19- P = HorizontalPlane (0 , (200 , 100 , 200 ), (10 , 200 , 10 ))
19+ P = HorizontalPlane (0 , (200 , 100 , 200 ), (0 , 20 , 200 ))
2020 return [S1 , S2 , S3 , P ]
2121
22+ def first_intersection (scene , line ):
23+ ans = []
24+ for S in scene :
25+ ts = S .intersection_value (line )
26+ if ts :
27+ p = line .point_after_t (ts [0 ])
28+ ans .append ((ts [0 ], S , p .coords ))
29+ if not ans :
30+ return []
31+ ans .sort ()
32+ return [ans [0 ]]
33+
34+ def first_intersection_color (scene , line ):
35+ ans = first_intersection (scene , line )
36+ if not ans :
37+ return (255 , 255 , 255 )
38+ to_light = first_intersection (scene , Line .through_points (ans [0 ][2 ], light ))
39+ if to_light and to_light [0 ][0 ] < 1 :
40+ return (0 , 0 , 0 )
41+ return ans [0 ][1 ].color_at_point (ans [0 ][2 ])
42+
2243world = create_world ()
2344camera = (3 , - 1 , 3 )
24- light = (0 , 10 , 0 )
45+ light = (3 , - 1 , 5 )
2546
2647# Colouring each pixel
2748for i in range (size ):
2849 x0 = coordinates (i )
29- for j in range (size ):
30- y0 = coordinates (j )
50+ for k in range (size ):
51+ y0 = coordinates (k )
52+ j = size - 1 - k
3153 l = Line .through_points (camera , (x0 , 0 , y0 ))
32- ans = []
33- for S in world :
34- ts = S .intersection_value (l )
35- if ts :
36- p = l .point_after_t (ts [0 ])
37- ans .append ((ts [0 ], S , p .coords ))
38- if not ans :
39- rays [i , size - 1 - j ] = (255 , 255 , 255 )
40- else :
41- ans .sort ()
42- rays [i , size - 1 - j ] = ans [0 ][1 ].color_at_point (ans [0 ][2 ])
54+ rays [i , j ] = first_intersection_color (world , l )
4355
4456tmp .save (path )
0 commit comments