def main(): A = Vector(1, 1, -1) float = 0.9999999 B = multiply_by_float(float, A) print("B=" + str(B.get_r())) B.print_properties_space()
def multiply_vectors_by_constant(constant, vector_1): vector_2_e0 = constant * vector_1.e[0] vector_2_e1 = constant * vector_1.e[1] vector_2_e2 = constant * vector_1.e[2] vector_2 = Vector(vector_2_e0, vector_2_e1, vector_2_e2) return vector_2
def divide_vectors(vector_1, vector_2): vector_3_e0 = vector_1.e[0] / vector_2.e[0] vector_3_e1 = vector_1.e[1] / vector_2.e[1] vector_3_e2 = vector_1.e[2] / vector_2.e[2] vector_3 = Vector(vector_3_e0, vector_3_e1, vector_3_e2) return vector_3
def multiply_vectors(vector_1, vector_2): vector_3_e0 = vector_1.e[0] * vector_2.e[0] vector_3_e1 = vector_1.e[1] * vector_2.e[1] vector_3_e2 = vector_1.e[2] * vector_2.e[2] vector_3 = Vector(vector_3_e0, vector_3_e1, vector_3_e2) return vector_3
def add_two_vectors(vector_1, vector_2): #make the e vector using the sum of each vector component from vector_1 and vector_2 vector_3_e0 = vector_1.e[0] + vector_2.e[0] vector_3_e1 = vector_1.e[1] + vector_2.e[1] vector_3_e2 = vector_1.e[2] + vector_2.e[2] vector_3 = Vector(vector_3_e0, vector_3_e1, vector_3_e2) return vector_3
def multiply_by_float(n, vector_1): vector_2_e0 = n * float(vector_1.e[0]) vector_2_e1 = n * float(vector_1.e[1]) vector_2_e2 = n * float(vector_1.e[2]) vector_2 = Vector(vector_2_e0, vector_2_e1, vector_2_e2) return vector_2
def subtract_vectors(vector_1, vector_2): #make the e vector using the difference of each vector component from vector_1 and vector_2 vector_3_e0 = vector_1.e[0] - vector_2.e[0] vector_3_e1 = vector_1.e[1] - vector_2.e[1] vector_3_e2 = vector_1.e[2] - vector_2.e[2] vector_3 = Vector(vector_3_e0, vector_3_e1, vector_3_e2) return vector_3
def color(ray): ray_direction_unit_vector = ray.direction().make_unit_vector() x = ray_direction_unit_vector.x y = ray_direction_unit_vector.y z = ray_direction_unit_vector.z unit_direction = Vector(x, y, z) # unit_direction.print_properties_space() y = unit_direction.get_y() t = 0.5 * (y + 1.0) vector_A = Vector(1.0, 1.0, 1.0) vector_B = Vector(0.5, 0.7, 1.0) return add_two_vectors(vector_A.multiply_by_float(1.0 - t), vector_B.multiply_by_float(t))
def main(): #open a file for writing and create if if it does not exist f = open("second_program_vectors.ppm", "w+") #write some lines of pixel data to the file nx = 200; ny = 100; f.write("P3\n%d %d \n255\n" %(nx, ny)) #to reverse count, go from high range to low range and add -1 for j in range(ny-1, 0, -1): for i in range(nx): #calculates the pixels color = Vector(i/nx, j/ny, 0.2) ir = int(255.99*color.e[0]) ig = int(255.99*color.e[1]) ib = int(255.99*color.e[2]) #writes in the file the rgb values of each pixel f.write("%d %d %d\n" %(ir, ig, ib)) #close the file when done f.close()
def main(): f = open("color_bg.ppm", "w+") nx = 200 ny = 100 f.write("P3\n%d %d \n255\n" % (nx, ny)) lower_left_corner = Vector(-2.0, -1.0, -1.0) horizontal = Vector(4.0, 0.0, 0.0) vertical = Vector(0.0, 2.0, 0.0) origin = Vector(0.0, 0.0, 0.0) # for j in range(nx): # for i in range(ny-1, 0, -1): for j in range(ny - 1, 0, -1): for i in range(nx): u = i / nx print("u: " + str(u)) v = j / ny print("v: " + str(v)) #calculates the direction of the ray horizontal = multiply_by_float(u, horizontal) print("horizontal r" + str(horizontal.get_r())) print("horizontal b" + str(horizontal.get_b())) print("horizontal g" + str(horizontal.get_g())) print("horizontal") horizontal.print_properties_space() vertical = multiply_by_float(v, vertical) print("vertical") vertical.print_properties_space() sum_of_horizontal_and_vertical = add_two_vectors( horizontal, vertical) direction = add_two_vectors(lower_left_corner, sum_of_horizontal_and_vertical) r = Ray(origin, direction) col = color(r) ir = int(255.99 * col.r) ig = int(255.99 * col.g) ib = int(255.99 * col.b) # ir = 255.99*col.r # ig = 255.99*col.g # ib = 255.99*col.b f.write("%d %d %d\n" % (ir, ig, ib)) f.close()