def angle(a, b, c): """Returns the angle (in radians) created between 3 points. The angle at B going from point A to point C. Result is always between 0 and pi. """ ab = distance(a, b) bc = distance(b, c) ac = distance(a, c) d = (ab**2 + bc**2 - ac**2) / (2 * ab * bc) d = min(1, max(d, -1)) return math.acos(d)
def apply_same_length_constraint(graph): same_length_dict = {} for edge in graph.edges(): edge_attr = graph.get_edge_data(*edge) if "same_length_strokes" in edge_attr: stroke_through_count = edge_attr["same_length_strokes"] if stroke_through_count > 0: if stroke_through_count in same_length_dict: same_length_dict[stroke_through_count].append(edge) else: same_length_dict[stroke_through_count] = [edge] print same_length_dict relabel = {} for edges_of_same_length in same_length_dict.values(): average_length = 0 for (n1, n2) in edges_of_same_length: average_length += topology.distance(n1, n2) average_length /= len(edges_of_same_length) print average_length # edges_of_same_length = "" # for ((n1x, n1y), (n2x, n2y)) in edges_of_same_length: # relabel[(n1x, n1y)] = (averageX, y) # relabel[(n1x, n1y)] = (averageX, y) # nx.relabel_nodes(graph, relabel)
def apply_same_length_constraint(graph): same_length_dict = {} for edge in graph.edges(): edge_attr = graph.get_edge_data(*edge) if "same_length_strokes" in edge_attr: stroke_through_count = edge_attr["same_length_strokes"] if stroke_through_count > 0: if stroke_through_count in same_length_dict: same_length_dict[stroke_through_count].append(edge) else: same_length_dict[stroke_through_count] = [edge] print same_length_dict relabel = {} for edges_of_same_length in same_length_dict.values(): average_length = 0 for (n1, n2) in edges_of_same_length: average_length += topology.distance(n1, n2) average_length /= len(edges_of_same_length) print average_length
def get_neighborhood(path, index, direction, neighborhood): """Finds the neighborhood of points around path[index]. That is, all the points that are within neighborhood distance (measured as path length) from the point in question (path[index]). This includes the point itself. We look at the points on the path before the point (if direction is -1) or after the point (if direction is 1), and we return the points that are within neighborhood distance of the point. """ path_length = 0 result = [path[index]] index += direction while 0 <= index < len(path): point = path[index] prev_point = path[index - direction] step_distance = distance(prev_point, point) path_length += step_distance if path_length > neighborhood: break result.append(point) index += direction return result