def build_graph(self):
# Currently O(n * m) which is sad. Spatial partitioning tree (kdtree or quadtree) on node
# locations would make O(m * log n). M and N are small enough in most cases that this
# is fast enough for now.
for line in self.contour_lines:
for index, endpoint in enumerate(line.endpoints):
# Find node with centroid closest to this endpoint.
closest_node = None
closest_sq = sys.float_info.max
for node in self.contour_nodes:
dx = endpoint[0] - node.centroid[0]
dy = endpoint[1] - node.centroid[1]
dist_sq = dx * dx + dy * dy
if dist_sq < closest_sq:
closest_node = node
closest_sq = dist_sq
# Check for root node (closer to top edge of image than to any labeled node)
edge_dist_sq = endpoint[1] * endpoint[1]
if edge_dist_sq < closest_sq:
closest_node = EDGE_NODE
line.nodes[index] = closest_node
评论列表
文章目录