用python绘制叠加等边三角形_如何用pythontu绘制平铺三角形

我试着画一个平铺的等边三角形,看起来像这样

X7ybH.png

用Python的乌龟。我希望能有16,25,36,49或64个三角形。在

我最初的尝试很笨拙,因为我还没有想出如何将海龟从一个三角形整齐地移动到下一个三角形。在

这是我的(部分正确的)代码def draw_triangle(this_turtle, size,flip):

"""Draw a triangle by drawing a line and turning through 120 degrees 3 times"""

this_turtle.pendown()

this_turtle.fill(True)

for _ in range(3):

if flip:

this_turtle.left(120)

this_turtle.forward(size)

if not flip:

this_turtle.right(120)

this_turtle.penup()

myturtle.goto(250,0)

for i in range(4):

for j in range(4):

draw_triangle(myturtle, square_size,(j%2 ==0))

# move to start of next triangle

myturtle.left(120)

#myturtle.forward(square_size)

myturtle.goto(-250,(i+1)*square_size)

一定有一种优雅的方式来做这件事?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值