11from geometry import *
22from PIL import Image
33
4- path = "./rays.png"
5- size = 300
6- tmp = Image .new ('RGB' , (size , size ), "white" )
7- rays = tmp .load ()
8-
9- def coordinates (num , sz = size ):
4+ def coordinates (num , sz ):
105 """
116 Take a number in range(size) and returns the proportional value between 1 and 5
127 """
138 return 4.0 * num / (sz - 1 ) + 1
149
1510def create_world ():
16- S1 = Sphere ((4 , 4 , 2 ), 2 , (100 , 220 , 0 ))
11+ S1 = Sphere ((1 , 4 , 2 ), 2 , (100 , 220 , 0 ))
1712 S2 = Sphere ((2 , 2 , 1 ), 1 , (0 , 100 , 200 ))
1813 S3 = Sphere ((3 , 1 , 1 ), 1 , (100 , 100 , 200 ))
1914 P = HorizontalPlane (0 , (200 , 100 , 200 ), (0 , 20 , 200 ))
@@ -31,26 +26,36 @@ def first_intersection(scene, line):
3126 ans .sort ()
3227 return [ans [0 ]]
3328
34- def first_intersection_color (scene , line ):
29+ def first_intersection_color (scene , line , light ):
3530 ans = first_intersection (scene , line )
3631 if not ans :
3732 return (255 , 255 , 255 )
38- to_light = first_intersection (scene , Line .through_points (ans [0 ][2 ], light ))
33+ l = Line .through_points (ans [0 ][2 ], light )
34+ to_light = first_intersection (scene , l )
3935 if to_light and to_light [0 ][0 ] < 1 :
4036 return (0 , 0 , 0 )
41- return ans [0 ][1 ].color_at_point (ans [0 ][2 ])
37+ a , b , c = ans [0 ][1 ].color_at_point (ans [0 ][2 ])
38+ vector1 = Vector (l .slope ).normalize ()
39+ vector2 = ans [0 ][1 ].normal (ans [0 ][2 ])
40+ cos = vector1 .scalar_product (vector2 )
41+ return (int (a * cos ), int (b * cos ), int (c * cos ))
42+
4243
43- world = create_world ()
44- camera = (3 , - 1 , 3 )
45- light = (3 , - 1 , 5 )
44+ def render (size ):
45+ path = "./rays.png"
46+ tmp = Image .new ('RGBA' , (size , size ), "white" )
47+ rays = tmp .load ()
48+ world = create_world ()
49+ camera = (2 , - 1 , 3 )
50+ light = (3 , - 1 , 10 )
51+ # Colouring each pixel
52+ for i in range (size ):
53+ x0 = coordinates (i , size )
54+ for k in range (size ):
55+ y0 = coordinates (k , size )
56+ j = size - 1 - k
57+ l = Line .through_points (camera , (x0 , 0 , y0 ))
58+ rays [i , j ] = first_intersection_color (world , l , light )
59+ tmp .save (path )
4660
47- # Colouring each pixel
48- for i in range (size ):
49- x0 = coordinates (i )
50- for k in range (size ):
51- y0 = coordinates (k )
52- j = size - 1 - k
53- l = Line .through_points (camera , (x0 , 0 , y0 ))
54- rays [i , j ] = first_intersection_color (world , l )
55-
56- tmp .save (path )
61+ render (300 )
0 commit comments