def scale_matrix(*scale): scale_vec = matrix.row_vector(list(scale)) scale_mat = matrix.eye(len(scale)) mat = matrix.Matrix([[(scale[col] if col == row else 0) for col in range(len(scale))] for row in range(len(scale))]) return mat
def scale_matrix(*scale): scale_vec = matrix.row_vector(list(scale)) scale_mat = matrix.eye(len(scale)) mat = matrix.Matrix( [ [ (scale[col] if col == row else 0) for col in range(len(scale)) ] for row in range(len(scale)) ] ) return mat
def rotation_matrix(size, d1, d2, angle): if d1 == d2: raise ValueError("d1 and d2 are equal!") if d1 >= size or d2 >= size: raise ValueError("Rotation dimensions are larger than matrix size!") cos = math.cos(angle) sin = math.sin(angle) rotation_mat = matrix.eye(size) rotation_mat[(d1, d1)] = cos rotation_mat[(d1, d2)] = -sin rotation_mat[(d2, d2)] = cos rotation_mat[(d2, d1)] = sin return rotation_mat
def translation_matrix(*translation): mat = eye(len(translation) + 1) for i, t in enumerate(translation): mat[(i, len(translation))] = t return mat
FOV = 70 ASPECT_RATIO = WIDTH / HEIGHT PIXEL_SCALE = 5 MAX_STEPS = 15 MAX_DISTANCE = 15.0 TOLERANCE = 0.1 pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Software Raytracer') surface = pygame.Surface(screen.get_size()) scene = { 'camera': matrix.eye(), 'spheres': [ # position, radius, colour, specular [[0.0, 0.0, -5.0], 0.5, [255, 0, 0], 10.0], [[1.0, 1.0, -3.0], 0.5, [0, 255, 0], 0.5], [[-3.0, -1.0, -10.0], 1.5, [0, 100, 255], 2.0], ], } def min_distance(point): # this should also return the sphere itself distances = [sphere.distance_to(s, point) for s in scene['spheres']] closest = min(distances) index = distances.index(closest) return closest, scene['spheres'][index]