def generate_rects(self, length_divisions, num_samples, plt = None): self.sub_fields = [] horiz1 = Vector.sub(self.v2, self.v1) horiz2 = Vector.sub(self.v3, self.v4) step1 = horiz1.get_length()/length_divisions step2 = horiz2.get_length()/length_divisions points = [[0 for x in range(length_divisions + 1)] for x in range(length_divisions + 1)] for i in range(length_divisions + 1): current1 = Vector.sum(self.v1, Vector.scale(horiz1, i * step1)) current2 = Vector.sum(self.v4, Vector.scale(horiz2, i * step2)) current_vert = Vector.sub(current2, current1) current_step = current_vert.get_length()/length_divisions for j in range(length_divisions + 1): point_pos = Vector.sum(current1, Vector.scale(current_vert, j * current_step)) points[i][j] = point_pos for x in range(length_divisions): for y in range(length_divisions): self.sub_fields.append(Quad(points[x][y], points[x + 1][y], points[x + 1][y + 1], points[x][y + 1], num_samples, plt = plt))
def nearest_neighbour(start_point, vectors): min_dist = None nearest = None for potential in vectors: dist_to = Vector.distance(start_point, potential) if min_dist is None or dist_to < min_dist: min_dist = dist_to nearest = potential return nearest
def greedy_ts_path(vec_list): graph = dict() used = [] current = vec_list[0] # print vec_list while len(used) + 1 < len(vec_list): used.append(current) min_dist = None nearest_neighbour = None for potential in vec_list: if potential not in used: dist_to = Vector.distance(current, potential) if min_dist is None or dist_to < min_dist: min_dist = dist_to nearest_neighbour = potential graph[current] = nearest_neighbour current = nearest_neighbour return graph
def __inner(from_node, to_node): return Vector.distance(vec_list[from_node], vec_list[to_node])
def length(tour): length = 0 for i in range(len(tour) - 1): length += Vector.distance(tour[i], tour[i + 1]) return length