こんにちは。
六角格子の頂点座標を求めました。
source code
hexagonal_lattice.py
#!/usr/bin/env python3
import math, sys
n = 2 if len(sys.argv) == 1 else int(sys.argv[1])
L = math.sqrt(3)/2
# [(1,0), (1/2, L)] # triangular-latice basis
vertices = []
for i in range(-n, n+1):
for j in range(-n, n+1):
k, l = i%3, j%2
if k == 0 or k + l == 2: # hexagonal vertices
m = i + l/2
vertices.append([m, j * L])
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(9, 9))
ax.scatter(*tuple(zip(*vertices))) # transpose
ax.set_aspect('equal')
plt.show()
また、(i,j)
ループの部分をいろいろ変えても六角格子を得られます:
for i in range(-n, n+1):
for j in range(-n, n+1):
if i%3 + j%3 != 2: # hexagonal vertices
m = i - l/2
vertices.append([m, j * L])
他にも、
for i in range(-n, n+1):
for j in range(-n, n+1):
m = (i * 3 - (i+j)%2) / 2
vertices.append([m, j * L])