我正在尝试生成下图“六边形或六角星形多边形,其中数字放置在六个顶点和六个交点的每一个处”。
下面是我到目前为止的代码。我一直在试图找到六边形的顶点来绘制那里的数字。
import matplotlib.pyplot as plt
import math
def plot_hexagram_with_numbers(side_length):
# Calculate the coordinates for the vertices of the hexagram
vertices = []
mu_hexagon = 360 / 6
for i in range(6):
angle_rad = math.radians(i * mu_hexagon)
x = side_length * math.cos(angle_rad)
y = side_length * math.sin(angle_rad)
vertices.append((x, y))
# Create a list of triangles' vertices for the hexagram
triangles = []
for i in range(6):
triangle = [vertices[i], vertices[(i + 2) % 6], vertices[(i + 4) % 6]]
triangles.append(triangle)
# Plot the hexagram's triangles
for triangle in triangles:
x_coords, y_coords = zip(*triangle)
plt.plot(x_coords, y_coords, 'b-')
# Add numbers at each vertex and intersection of the hexagram
label_distance = side_length / 20 # Adjust this value as needed
for i in range(6):
x, y = vertices[i]
# cant figure out how to find the edges/intersections :(
# Set axis equal and show the plot
plt.axis('equal')
plt.show()
这是上面代码生成的图像: