def intersect_sphere(sphere, ray): # sphere a, ray a -> Maybe int dirn = ry.get_dirn(ray) pos = ry.get_pos(ray) center = get_center(sphere) to_center = vr.sub_vector(pos, center) a = vr.dot_prod(dirn, dirn) b = 2*vr.dot_prod(dirn, to_center) c = vr.dot_prod(to_center, to_center) - get_radius(sphere) ** 2 det = b ** 2 - 4 * a * c if det < 0: return None else: t1 = (-b + math.sqrt(det)) / (2*a) t2 = (-b - math.sqrt(det)) / (2*a) return min([t1, t2])
def main(): # create 3 random vectors v1 = [random.random(), random.random(), random.random()] v2 = [random.random(), random.random(), random.random()] v3 = [random.random(), random.random(), random.random()] # Print out the 3 vectors print("v1 = ", v1) print("v2 = ", v2) print("v3 = ", v3) #Print magnitude of 3 vectors print("v1 magnitude = ", vc.mod(v1)) print("v2 magnitude = ", vc.mod(v2)) print("v3 magnitude = ", vc.mod(v3)) #print addition, dot & cross products of v1 and v2 print("v1 + v2 =", vc.addv(v1, v2)) print("v1 . v2 =", vc.dot_prod(v1, v2)) print("v1 x v2 =", vc.crossprod(v1, v2)) #vector identities #1 identity1part1 = vc.crossprod(v1, v2) identity1part2 = vc.crossprod(vc.scale(v2, -1), v1) if vc.same(identity1part1, identity1part2) == True: print("Identity 1 is correct.") #2 identity2part1 = vc.crossprod(v1, vc.addv(v2, v3)) identity2part2 = vc.addv(vc.crossprod(v1, v2), vc.crossprod(v1, v3)) if vc.same(identity2part1, identity2part2) == True: print("Identity 2 is correct.") #3 identity3part1 = vc.crossprod(v1, vc.crossprod(v2, v3)) identity3part2 = vc.subv((vc.scale(v2, vc.dot_prod(v1, v3))), (vc.scale(v3, vc.dot_prod(v1, v2)))) if vc.same(identity3part1, identity3part2) == True: print("Identity 3 is correct.")
def intersect_lights(scene, lights, pos, normal): # scene, light, vector -> float color = 1 for light in lights: dirn = vr.normalize(vr.sub_vector(lt.get_pos(light), pos)) new_ray = ry.make_ray(pos, dirn) t, n = intersect_scene(scene, new_ray) if not t: dot_or_0 = max([vr.dot_prod(dirn, normal), 0]) incr = dot_or_0 * lt.get_brightness(light) color += incr if color: return min([8, color])
def mat_vec_prod(m, v): """Calculate the matrix-vector product.""" if m.shape[1] != v.shape[0]: raise ValueError l = [vector.dot_prod(m.rows[i], v) for i in range(m.shape[0])] return vector.Vector(l)
v0 = [GF2.zero, GF2.zero, GF2.one, GF2.zero, GF2.zero, GF2.one, GF2.zero] v_0010010 = match_vector_sums_to(p5_vectors, v0) p5_v1 = [GF2.zero, GF2.one, GF2.zero, GF2.zero, GF2.zero, GF2.one, GF2.zero] v_0100010 = match_vector_sums_to(p5_vectors, p5_v1) ## 6: (Problem 6) Solving Linear Equations over GF(2) #You should be able to solve this without using a computer. a = [GF2.one, GF2.one, GF2.zero, GF2.zero] b = [GF2.one, GF2.zero, GF2.one, GF2.zero] c = [GF2.one, GF2.one, GF2.one, GF2.one] x_gf2 = [GF2.one, GF2.zero, GF2.zero, GF2.zero] x_gf2_comp = vector.GF2_solve_dot_prod_sys(one, a, b, c) ## 7: (Problem 7) Formulating Equations using Dot-Product #Please provide each answer as a list of numbers v1 = [2, 3, -4, 1] v2 = [1, -5, 2, 0] v3 = [4, 1, -1, -1] ## 8: (Problem 8) Practice with Dot-Product uv_a = vector.dot_prod([1, 0], [5, 4321]) uv_b = vector.dot_prod([0, 1], [12345, 6]) uv_c = vector.dot_prod([-1, 3], [5, 7]) uv_d = trunc( vector.dot_prod( [-(sqrt(2) / 2.0), sqrt(2) / 2.0], [sqrt(2) / 2.0, -(sqrt(2) / 2.0)] ) )
def test_dot_prod(self): p = vector.dot_prod(self.v, self.v) self.assertAlmostEqual(p, 8.75) with self.assertRaises(ValueError): vector.dot_prod(self.v, vector.Vector([1.0]))