# 定义立方体的棱 edges = [[vertices[i] for i in [0, 1, 2, 3, 0]], [vertices[i] for i in [4, 5, 6, 7, 4]], [vertices[i] for i in [0, 4]], [vertices[i] for i in [1, 5]], [vertices[i] for i in [2, 6]], [vertices[i] for i in [3, 7]]]
# 绘制立方体 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for edge in edges: ax.plot(*zip(*edge), color='b')
defpolygon_area(vertices): n = len(vertices) area = 0.0 for i inrange(n): j = (i + 1) % n area += vertices[i][0] * vertices[j][1] area -= vertices[i][1] * vertices[j][0] returnabs(area) / 2.0
defclosest_pair(points): defdistance(p1, p2): return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5 deffind_closest_util(points_sorted_by_x, points_sorted_by_y): iflen(points_sorted_by_x) <= 3: return naive_closest_pair(points_sorted_by_x) mid = len(points_sorted_by_x) // 2 midpoint = points_sorted_by_x[mid] left_y = [p for p in points_sorted_by_y if p[0] <= midpoint[0]] right_y = [p for p in points_sorted_by_y if p[0] > midpoint[0]] (p1, p2, left_dist) = find_closest_util(points_sorted_by_x[:mid], left_y) (p3, p4, right_dist) = find_closest_util(points_sorted_by_x[mid:], right_y) min_dist = min(left_dist, right_dist) closest_pair = (p1, p2) if left_dist < right_dist else (p3, p4)
in_strip = [p for p in points_sorted_by_y ifabs(p[0] - midpoint[0]) < min_dist]
for i inrange(len(in_strip)): for j inrange(i + 1, len(in_strip)): if (in_strip[j][1] - in_strip[i][1]) < min_dist: d = distance(in_strip[i], in_strip[j]) if d < min_dist: min_dist = d closest_pair = (in_strip[i], in_strip[j]) else: break
# 计算边中点和面中心 for face in faces: face_point = [0, 0, 0] for v in face: face_point = [face_point[i] + vertices[v][i] for i inrange(3)] face_point = [p / len(face) for p in face_point] face_points.append(face_point)
# 处理边 for face in faces: for i inrange(len(face)): v1 = face[i] v2 = face[(i + 1) % len(face)] key = tuple(sorted([v1, v2])) if key notin edge_midpoints: mid_point = [(vertices[v1][j] + vertices[v2][j]) / 2for j inrange(3)] edge_midpoints[key] = mid_point