def find_nearest_to(self, point: Point): set_a = set(self.graph.get_points()) point_current = set_a.pop() num_current_distance = point.distance_to(point_current) list_neighbors = self.graph.get_neighbours_list(point_current) while len(list_neighbors) != 0: # Get shortest distance among neighbors list_distances = list( map(lambda x: x[1].distance_to(point), list_neighbors)) num_new_distance = min(list_distances) if num_current_distance < num_new_distance: return point_current # Update current distance num_current_distance = num_new_distance # Update point_current index_of_new_distance = list_distances.index(num_new_distance) point_current = list_neighbors[index_of_new_distance][1] # Update set_a for neighbor in list_neighbors: set_a.discard(neighbor) # Update list_neighbors filter_only_a_marked_points = filter( lambda x: x[1] in set_a, self.graph.get_neighbours_list(point_current)) list_neighbors = list(filter_only_a_marked_points) return point_current
def find_nearest_to(self, point: Point): current = self.cache temp = None minimal = point.distance_to(current) cluster_set = {x for x in self.graph.get_points()} cluster_set.discard(current) while True: the_smallest = float('inf') neighbours = self.graph.get_neighbours(current) cluster_set.difference_update(neighbours) for neighbour in neighbours: dist = neighbour.distance_to(point) if dist < the_smallest: the_smallest = dist temp = neighbour # noinspection PyChainedComparisons if the_smallest < minimal and len( cluster_set) != 0 and minimal != 0: minimal = the_smallest current = temp else: break self.cache = current return current pass
def find_nearest_to(self, point: Point): clusters = list(self.graph.graph.keys()) dif = float('inf') nearest = None for cluster in clusters: dist = point.distance_to(cluster) if dist < dif: dif = dist nearest = cluster return nearest pass