标签:
效果是不是很漂亮呢?
代码如下:
#----------------------------------------- # Python + Matplotlib 绘制 Penrose 铺砌 # by Zhao Liang zhao_liang@pku.edu.cn #----------------------------------------- import matplotlib.pyplot as plt import numpy as np from matplotlib.path import Path from matplotlib.patches import PathPatch plt.figure(figsize=(8,6),dpi=100) plt.subplot(aspect=1) plt.axis([-0.6,0.6,-0.6,0.6]) plt.xticks([]) plt.yticks([]) plt.axis(‘off‘) a = 0.5*(np.sqrt(5)-1) ‘‘‘ 所有要绘制的三角形都放在一个列表中,列表的每个元素形如 [color, A, B ,C],其中 color = 0, 1 表示两种颜色之一, A, B, C 是 np.array 数组表示三角形三个顶点的坐标. 每次把切割后的小三角形放在一个新的列表 result 里面然后 返回 result. ‘‘‘ def subdivide(triangles): result = [] for color,A,B,C in triangles: if color == 0: P = A + (B-A)*a result += [(0,C,P,B),(1,P,C,A)] else: Q = B+(A-B)*a R = B+(C-B)*a result +=[(1,R,C,A),(1,Q,R,B),(0,R,Q,A)] return result ‘‘‘ 画图没有什么好说的,注意三角形 ABC 的底边 BC 是永远不画的,它们是 合并为菱形时的边界. ‘‘‘ def DrawFigure(triangles): for color,A,B,C in triangles: vertices = [C,A,B] codes = [Path.MOVETO]+[Path.LINETO]*2 tri = Path(vertices,codes) if color == 0: tri_patch=PathPatch(tri,facecolor=‘#FF0099‘,edgecolor=‘#666666‘,linewidth=0.8) else: tri_patch=PathPatch(tri,facecolor=‘#66CCFF‘,edgecolor=‘#666666‘,linewidth=0.8) plt.gca().add_patch(tri_patch) plt.show() triangles = [] A=np.array([0,0]) for i in range(10): B = np.array([np.cos(0.2*np.pi*i),np.sin(0.2*np.pi*i)]) C = np.array([np.cos(0.2*np.pi*(i+1)),np.sin(0.2*np.pi*(i+1))]) if i%2 == 0: B , C = C, B triangles.append([0,A,B,C]) m = input(‘Enter number of divisions: ‘) for j in xrange(m): triangles=subdivide(triangles) DrawFigure(triangles)
Python + Matplotlib 绘制 Penrose 铺砌
标签:
原文地址:http://www.cnblogs.com/xida/p/4572341.html