简体   繁体   中英

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

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):

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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