gpt4 book ai didi

python - 如何正确绘制具有偏移坐标的六边形?

转载 作者:行者123 更新时间:2023-12-04 01:37:22 24 4
gpt4 key购买 nike

这是我的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np


offCoord = [[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2]]

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

for c in offCoord:
hex = RegularPolygon((c[0], c[1]), numVertices=6, radius=2./3., alpha=0.2, edgecolor='k')
ax.add_patch(hex)
plt.autoscale(enable = True)
plt.show()

附图中的预期结果与实际结果

Expected result vs actual result

请告诉我为什么我的六边形不是边排边而是互相重叠?我做错了什么?

最佳答案

使用余弦定律(对于角为 120 度且边为 r、r 和 1 的等腰三角形):

1 = r*r + r*r - 2*r*r*cos(2pi/3) = r*r + r*r + r*r = 3*r*r

r = sqrt(1/3)

triangle

这是正确的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
import numpy as np


offCoord = [[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2]]

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

for c in offCoord:
# fix radius here
hexagon = RegularPolygon((c[0], c[1]), numVertices=6, radius=np.sqrt(1/3), alpha=0.2, edgecolor='k')
ax.add_patch(hexagon)
plt.autoscale(enable = True)
plt.show()

关于python - 如何正确绘制具有偏移坐标的六边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59042139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com