简体   繁体   English

使用 python 乌龟画一个带线条的三角形

[英]drawing a triangle with lines across using python turtle

I am still learning and trying to draw a triangle with lines across from bottom to top, but am having a terrible time figuring it out.我仍在学习并尝试绘制一个从底部到顶部的三角形,但是我很难弄清楚它。 I am able to pick the turtle up and move it drawing slightly bigger or smaller to make several triangles enclosed in one larger one, but I am beating my head off of the table trying to figure this out.我可以把乌龟捡起来并移动它,使其稍微变大或变小,以使几个三角形包围在一个较大的三角形中,但我正在将头从桌子上敲下来,试图弄清楚这一点。 Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks in advance提前致谢在此处输入图像描述

import turtle

t = turtle.Turtle()
def drawTriangle(t, side):
  t.forward(side)
  t.left(120)



for x in range (3):
  drawTriangle(t, 100)


drawTriangle()

Here is a basic triangle, if you have a function called drawTriangle then it kind of makes sense to make it draw a triangle rather than something that you have to call three times to get a triangle这是一个基本的三角形,如果你有一个名为drawTriangle的 function,那么让它绘制一个三角形而不是你必须调用三次才能得到一个三角形的东西是有意义的

import turtle

def drawTriangle(t, side):
  for _ in range(3):
    t.forward(side)
    t.left(120)

t = turtle.Turtle()
drawTriangle(t, 200)

Not sure what you mean by lines across, so if you edit the question to make that clearer then hopefully I will notice in time to edit the answer to add that part - okay now I see the picture, coming up.......不知道你所说的跨行是什么意思,所以如果你编辑问题以使其更清楚,那么希望我会及时注意到编辑答案以添加该部分 - 好吧,现在我看到了图片,即将出现...... .

This will do.......这会做.......

import turtle

def drawTriangle(t, side):
  for _ in range(3):
    t.forward(side)
    t.left(120)

t = turtle.Turtle()
t.penup()
t.setheading(-120)
t.setposition(0, 100)
t.pendown()

for side in range(40, 240, 40):
  drawTriangle(t, side)

Here's a bare bones solution that operates on an isosceles triangle and doesn't necessarily slice the entire triangle evenly (as in the OP's illustration):这是一个在等腰三角形上运行的基本解决方案,并且不一定均匀地切割整个三角形(如 OP 的插图中所示):

from turtle import Screen, Turtle

SHOWN, TOTAL = 5, 7  # five of seven equal slices will be shown
WIDTH, HEIGHT = 540, 270  # dimensions of (isosceles) triangle

screen = Screen()
turtle = Turtle()

for n in range(TOTAL - SHOWN + 1, TOTAL + 1):
    ratio = n / TOTAL
    turtle.goto(WIDTH/2 * ratio, -HEIGHT * ratio)
    turtle.setx(-WIDTH/2 * ratio)
    turtle.home()

turtle.hideturtle()
screen.exitonclick()

在此处输入图像描述

A different approach is to use stamping instead of drawing .另一种方法是使用冲压而不是绘图 Not necessarily simpler in this case but it might be easier for some folks to visualize drawing entire triangles in one stroke:在这种情况下不一定更简单,但对于某些人来说,一笔画出整个三角形可能更容易:

from turtle import Screen, Turtle

SHOWN, TOTAL = 5, 7  # five of seven equal slices will be shown
WIDTH, HEIGHT = 540, 270
DELTA = ((TOTAL - 1) + HEIGHT / TOTAL) / 2  # a bit of fudging here

CURSOR_SIZE = 20

screen = Screen()
screen.mode('logo')

turtle = Turtle()
turtle.hideturtle()
turtle.shape('triangle')
turtle.fillcolor('white')

for n in range(TOTAL, TOTAL - SHOWN, -1):
    ratio = n / TOTAL
    turtle.shapesize(ratio * WIDTH / CURSOR_SIZE, ratio * HEIGHT / CURSOR_SIZE)
    turtle.stamp()
    turtle.forward(DELTA)

screen.exitonclick()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM