gpt4 book ai didi

python - 如何以编程方式创建重复图像模式?

转载 作者:行者123 更新时间:2023-11-28 18:44:31 25 4
gpt4 key购买 nike

我想使用 Python 创建以下图像模式。 enter image description here为清楚起见:这是两个独立的图像序列(一个在顶行,一个在底行)。它们相互关联,因为它们是堆叠四面体的投影区域。在 3D 环境中,它看起来如下:

enter image description here请注意,这些 3D 对象未按比例缩放,因此对象的总尺寸保持不变。上面显示的投影区域就是这种情况。
四层结构(未显示)顶部还有 10 个单元格。
n 层的 C 单元格总数为:

C = (n^3 + 3*n^2 + 2*n)/6 

现在我正在手动创建图案(制作 3D 对象、渲染投影区域、重复),但这非常乏味并且对于更多分割来说不可行。

我设法用以下代码创建了一个多边形,但我不知道如何循环它以使总边长保持不变,但多边形以上面可视化的方式被分割。

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

el = 1
dfv = 1/np.sqrt(3)*el
dfe = 1/(2*np.sqrt(3))*el
vertices1 = [(0,0),(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]
vertices2 = [(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.add_patch(Polygon(vertices1, closed=True, fill=True))
ax1.set_xlim([-1, 1])
ax1.set_ylim([-1, 1])
ax1.set_aspect('equal')

ax2 = fig.add_subplot(122)
ax2.add_patch(Polygon(vertices2, closed=True, fill=True))
ax2.set_xlim([-1, 1])
ax2.set_ylim([-1, 1])
ax2.set_aspect('equal')
plt.show()

enter image description here
我使用了 matplotlib 和包含的多边形补丁,但我不确定这是否是最佳方法。多边形的方向或颜色也不重要。

最佳答案

辅助类:

class Custom_Polygon(object):
"""docstring for Polygon"""
def __init__(self, verts):
self.verticies = np.array(verts)
self.dims = self.verticies.shape[1]

def scale(self, scaleFactor):
scaleMatrix = np.zeros((self.dims, self.dims))
np.fill_diagonal(scaleMatrix, scaleFactor)
self.verticies = np.dot(self.verticies, scaleMatrix)

def scale_with_orgin(self, scaleFactor, origin):
origin = origin.copy()
self.translate([-i for i in origin])
self.scale(scaleFactor)
self.translate([i for i in origin])

def translate(self, shiftBy):
self.verticies += shiftBy

def get_width(self):
x_min = self.verticies[:,0].min()
x_max = self.verticies[:,0].max()
return abs(x_max - x_min)


def get_height(self):
y_min = self.verticies[:,1].min()
y_max = self.verticies[:,1].max()
return abs(y_max - y_min)

制作了一个辅助类,用于缩放和平移多边形来制作图案。并编写了绘制第一个图案的算法。应该不难为绘制第二个图案做出类似的算法。

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

def makeFirstPattern(ax, d, verticies, scale=True):
Pn = Custom_Polygon(verticies)
direction = 1
divisions = d
if scale: Pn.scale_with_orgin(2.0/(divisions+1), Pn.verticies[-1])
for i in range(divisions, 0, -2):
for _ in range(i):
ax.add_patch(Polygon(Pn.verticies, closed=True, fill=True, ec='none'))
Pn.translate([direction * Pn.get_width()/2, 0])
direction *= -1
Pn.translate([direction * Pn.get_width(), Pn.get_height()])

el = 1
dfv = 1/np.sqrt(3)*el
dfe = 1/(2*np.sqrt(3))*el
vertices1 = [(0,0),(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]
vertices2 = [(0.5*el,-dfe),(0,dfv),(-0.5*el,-dfe)]


fig = plt.figure()
ax1 = fig.add_subplot(111)

makeFirstPattern(ax1, 7, vertices2)

ax1.set_xlim([-1, 1])
ax1.set_ylim([-1, 1])
ax1.set_aspect('equal')
plt.show()

enter image description here

关于python - 如何以编程方式创建重复图像模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22101013/

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