【问题标题】:How to properly draw hexagons with offset coordinates?如何正确绘制具有偏移坐标的六边形?
【发布时间】:2020-03-21 08:28:37
【问题描述】:

这是我的代码:

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

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

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

【问题讨论】:

  • radius 太大。看起来RegularPolygon 将“半径”定义为中心和每个顶点之间的距离。因此,您需要使用一些几何图形来确定该值应该是多少,以便从中心到边缘的距离为 1。

标签: python matplotlib coordinate-systems hexagonal-tiles


【解决方案1】:

使用余弦定律(对于角为 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)

这是正确的代码:

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

【讨论】:

    【解决方案2】:

    很简单,你的几何图形是错误的。您指定的半径为 2/3。检查您的文档以获取RegularPolygon;我认为您会发现正确的半径是 0.577 (sqrt(3) / 3) 或接近的值。

    【讨论】:

      【解决方案3】:

      正六边形的半径等于它的边。在这种情况下,正确的偏移量应该是: offset = radius*3**0.5。如果半径为 2/3,则偏移量应为 1.1547k,其中 k=-2,-1...

      【讨论】:

        猜你喜欢
        • 2019-10-30
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多