我正在试着画出下面的图片"一个六角星形或六角星形,在六个顶点和六个交叉点的每一个上都放置数字".

enter image description here

以下是我到目前为止的代码.我被困住了,试图找到六边形的顶点来绘制那里的数字.

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()

这是上面的代码生成的图像:

enter image description here

推荐答案

你的三角形不是闭合的,因此你在triangles中有两个以上的三角形(六角星形是).您应该构建完整的三角形,这样您甚至可以将您的代码推广到任何大小的尖星(只要它是3的倍数).

我用shapely来计算交叉点:

import matplotlib.pyplot as plt
import math
from shapely import Polygon, get_coordinates

def plot_hexagram_with_numbers(side_length, nb_points=6):
    if nb_points % 3:
        raise ValueError("nb_points must be a multiple of 3")
    # Calculate the coordinates for the vertices of the hexagram
    vertices = []
    mu_hexagon = 360 / nb_points
    for i in range(nb_points):
        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 and plot triangles
    triangles = []
    nb_triangles = nb_points//3
    for i in range(nb_triangles):
        triangle = [vertices[i], vertices[i + nb_triangles], vertices[i + 2*nb_triangles], vertices[i]]
        plt.plot(*zip(*triangle), 'b-')
        triangles.append(Polygon(triangle))

    # computing intersections
    intersections = [] 
    for i in range(nb_triangles):
        for j in range(i+1, nb_triangles):
            intersections.extend(get_coordinates(triangles[i].intersection(triangles[j]))[:-1])

    # Add numbers at each vertex and intersection of the hexagram
    for i, (x,y) in enumerate(vertices + intersections, start=1):
        plt.annotate(i, (x,y))

    # Set axis equal and show the plot
    plt.axis('equal')
    plt.show()

输出(默认为nb_points=6):

enter image description here

输出(nb_points=9):

enter image description here

Python相关问答推荐

跟踪我已从数组中 Select 的样本的最有效方法

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

Select 用a和i标签包裹的复选框?

大Pandas 胚胎中产生组合

try 与gemini-pro进行多轮聊天时出错

为什么我的Python代码在if-else声明中的行之前执行if-else声明中的行?

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

在Python中动态计算范围

部分视图的DataFrame

未知依赖项pin—1阻止conda安装""

如何防止Pandas将索引标为周期?

以异步方式填充Pandas 数据帧

处理Gekko的非最优解

将CSS链接到HTML文件的问题

freq = inject在pandas中做了什么?''它与freq = D有什么不同?''

极点替换值大于组内另一个极点数据帧的最大值

Pythonquests.get(Url)返回Colab中的空内容

在matplotlib中重叠极 map 以创建径向龙卷风图

时长超过24小时如何从Excel导入时长数据