drawtree

# drawtree.py
 
from turtle import Turtle, mainloop
 
def tree(plist, l, a, f):
    """ plist is list of pens
    l is length of branch
    a is half of the angle between 2 branches
    f is factor by which branch is shortened
    from level to level."""
    if l > 5: #
        lst = []
        for p in plist:
            p.forward(l)#沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
            q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties.
            p.left(a) #Turn turtle left by angle units
            q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
            lst.append(p)#将元素增加到列表的最后
            lst.append(q)
        tree(lst, l*f, a, f)
   
            
 
def main():
    p = Turtle()
    p.color("green")
    p.pensize(5)
    #p.setundobuffer(None)
    p.hideturtle() #Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
    #because hiding the turtle speeds up the drawing observably.
    #p.speed(10)
   # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
    p.speed(10)
    #TurtleScreen methods can then be called for that object.
    p.left(90)# Turn turtle left by angle units. direction 调整画笔
 
    p.penup() #Pull the pen up – no drawing when moving.
    p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
    p.pendown()# Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
    #否则turtle一移动就会自动的把线画出来
 
    #t = tree([p], 200, 65, 0.6375)
    t = tree([p], 200, 65, 0.6375)
     
main()

帮我看下这段生长粒子树的代码,页面显示不出来,怎么解决? <!DOCTYPE html> <html> <head> <style> #canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 使用 ES6 的类来表示粒子 class Particle { constructor(position, parentPosition) { this.position = position; this.parentPosition = parentPosition; this.children = []; } addChild(child) { this.children.push(child); } } // 创建画布 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 设置画布大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 生成粒子生长树 function generateTree(numParticles, maxDepth, growthStep) { const particles = [new Particle({ x: canvas.width / 2, y: canvas.height }, null)]; for (let i = 0; i < maxDepth; i++) { const newParticles = []; for (const particle of particles) { for (let j = 0; j < numParticles; j++) { const deltaX = Math.random() * growthStep - growthStep / 2; const deltaY = Math.random() * growthStep - growthStep / 2; const newPosition = { x: particle.position.x + deltaX, y: particle.position.y - growthStep + deltaY }; const newParticle = new Particle(newPosition, particle.position); particle.addChild(newParticle); newParticles.push(newParticle); } } particles.push(...newParticles); } return particles; } // 绘制粒子生长树 function drawTree(particles) { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = 'black'; function drawParticle(particle) { ctx.beginPath(); ctx.moveTo(particle.parentPosition.x, particle.parentPosition.y); ctx.lineTo(particle.position.x, particle.position.y); ctx.stroke(); for (const child of particle.children) { drawParticle(child); } } for (const particle of particles) { drawParticle(particle); } } // 示例用法 const numParticles = 3; const maxDepth = 5; const growthStep = 50; const particles = generateTree(numParticles, maxDepth, growthStep); drawTree(particles); </script> </body> </html>
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值