def get_intersection(agent, ray, segment): r_ptx = ray[0][0] r_pty = ray[0][1] r_dx = ray[1][0] - ray[0][0] r_dy = ray[1][1] - ray[0][1] sg_ptx = segment[0][0] sg_pty = segment[0][1] sg_dx = segment[1][0] - segment[0][0] sg_dy = segment[1][1] - segment[0][1] r_mag = vo.magnitude((r_dx, r_dy)) sg_mag = vo.magnitude((sg_dx, sg_dy)) if (r_dx / r_mag == sg_dx / sg_mag) and (r_dy / r_mag == sg_dy / sg_mag): return None t2 = (r_dx * (sg_pty - r_pty) + r_dy * (r_ptx - sg_ptx)) / (sg_dx * r_dy - sg_dy * r_dx) t1 = (sg_ptx + sg_dx * t2 - r_ptx) / (r_dx) factor = t1 if t1 <= 0: return None if t2 < 0 or t2 > 1: return None #if t1 * t1 * (r_dx * r_dx + r_dy * r_dy) > agent.vision_range * agent.vision_range: #factor = sqrt(agent.vision_range / (r_dx * r_dx + r_dy * r_dy)); x = r_ptx + r_dx * factor y = r_pty + r_dy * factor return ((x, y), t1)
def inside_bounding_circle(agent, wall): v = ((wall[1][0] - wall[0][0]), (wall[1][1] - wall[0][1])) center = agent.position pt_v = ((center[0] - wall[0][0]), (center[1] - wall[0][1])) v_magn = vo.magnitude(v) unit_v = vo.unit_vector(v) scalar_projection = pt_v[0] * unit_v[0] + pt_v[1] * unit_v[1] if scalar_projection < 0: closest = wall[0] elif scalar_projection > v_magn: closest = wall[1] else: proj_v = ((unit_v[0] * scalar_projection), (unit_v[1] * scalar_projection)) closest = ((wall[0][0] + proj_v[0]), (wall[0][1] + proj_v[1])) dist_v = vo.magnitude(((center[0] - closest[0]), (center[1] - closest[1]))) if dist_v < agent.vision_range: return True return False
def find_eigenvector(A, tolerance=0.00001): guess = [random.random() for _ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalar_multiply(1 / length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def find_eigenvector(A, tolerance=0.00001): guess = [random.random() for _ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalar_multiply(1/length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def find_eigenvector(m: Matrix, tolerance: float = 0.00001) -> Tuple[Vector, float]: guess = [random.random() for _ in m] while True: result = matrix_times_vector(m, guess) # transform the guess norm = magnitude(result) # compute the norm next_guess = [x/norm for x in result] # rescale if distance(guess, next_guess) < tolerance: # convergence so return (eigenvector, eigenvalue) return next_guess, norm guess = next_guess return 1 if (i,j) in friend_pairs or (j,i) in friend_pairs else 0
def calculate_visible_vertices(agent, map): elements = get_rays(agent, map) rays = elements[0] walls = elements[1] visible_vertices = [] for ray in rays: intersection = get_closest_intersection(agent, ray, walls) if intersection and intersection == ray[1]: intersection_vector = ((intersection[0] - agent.position[0]), (intersection[1] - agent.position[1])) angle = vo.get_relative_angle(agent.orientation_reference_vector, intersection_vector) distance = vo.magnitude(intersection_vector) visible_vertices.append((angle, distance)) visible_vertices.sort(key=lambda x: x[0]) return visible_vertices
def calculate_visible_vertices(agent, map, hash_table): visible_vertices = [] agent_position = agent.position rays = get_rays(agent_position, map) for ray in rays: intersection = get_closest_intersection(ray, map, hash_table) if intersection and intersection == ray[1]: intersection_vector = ((intersection[0] - agent.position[0]), (intersection[1] - agent.position[1])) angle = vo.get_relative_angle(agent.orientation_reference_vector, intersection_vector) distance = vo.magnitude(intersection_vector) visible_vertices.append((angle, distance)) visible_vertices.sort(key=lambda x: x[0]) return visible_vertices
def direction(w): mag = magnitude(w) return [w_i / mag for w_i in w]