提问人:Ruben Uijtdewilligen 提问时间:6/8/2023 更新时间:6/8/2023 访问量:38
在 VPython 中围绕分形创建金字塔
Creating a pyramid around a fractal in VPython
问:
我正在尝试使用 VPython 创建我自己的分形。我目前有这个六边形:
我想将一层中六边形角的线条添加到下一层中的玉米中。这样我就真的会生成一个金字塔。
目前,这是我的代码:
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)
我已经尝试在六边形的顶点上添加线(圆柱体),但我只能让线是垂直的:
有谁知道如何去做?公式编辑或 VPython 代码对我来说都是不错的解决方案。
答: 暂无答案
评论