在 VPython 中围绕分形创建金字塔

Creating a pyramid around a fractal in VPython

提问人:Ruben Uijtdewilligen 提问时间:6/8/2023 更新时间:6/8/2023 访问量:38

问:

我正在尝试使用 VPython 创建我自己的分形。我目前有这个六边形:Fractal top view

这个分形的公式是Formula

我增加了一些高度,使它成为一种没有边的金字塔:Fractal side view

我想将一层中六边形角的线条添加到下一层中的玉米中。这样我就真的会生成一个金字塔。

目前,这是我的代码:

from vpython import *
import math

def draw_hexagonal_snowflake(position, size, depth, height):
    if depth == 0 or size < 1e-6:
        return
    
    # Calculate the vertices of the hexagon
    vertices = []
    for i in range(6):
        theta = math.radians(60 * i)
        px = position.x + size * math.cos(theta)
        py = position.y + size * math.sin(theta)
        vertices.append(vector(px, py, height))
    
    # Draw the hexagon
    for i in range(6):
        if size > 1e-6:
            cylinder(pos=vertices[i], axis=vertices[(i+1)%6]-vertices[i], radius=size/20, color=color.white)
    
    # Recursive function for all sides of the hexagon
    for i in range(6):
        new_position = (vertices[i] + vertices[(i+1)%6]) / 2
        new_height = height - size * math.sin(math.radians(60))
        draw_hexagonal_snowflake(new_position, size / 2, depth - 1, new_height)

scene = canvas(width=800, height=800)

position = vector(0, 0, 0)  
size = 200  
depth = 5 # Amount of iterations
height = 0

draw_hexagonal_snowflake(position, size, depth, height)

我已经尝试在六边形的顶点上添加线(圆柱体),但我只能让线是垂直的:

Vertical lines 'pyramid'

有谁知道如何去做?公式编辑或 VPython 代码对我来说都是不错的解决方案。

python 分形 vpython

评论

0赞 user1114907 6/10/2023
我没有时间弄清楚你的谜题,也不清楚 iist 连接相邻层是什么意思,因为相邻层的顶点数量不同。但是,我会评论说 vpython 模块默认导入 math,因此您不需要导入它,也不需要使用“math”。

答: 暂无答案