Joseph Rainsbury
Joseph Rainsbury

Reputation: 1

Python Turtle tr

I am a noob but I am trying to get triangles to stack on top of each other whilst slowly getting consistently bigger, each triangle has to be centered in one loop. I have this piece of code and it works perfectly for the first triangle, but it starts to move to the left. Any help is appreciated :) What I have https://i.sstatic.net/WXiMD.jpg

This is the goal https://i.sstatic.net/1oIhO.jpg

from turtle import *
number_of_shapes = 4
for shape in range(1, number_of_shapes + 1):
    for sides in range(1, 4):
        forward(shape * 20)
        left(120)

    penup()
    left(120)
    forward(shape * 20)
    right(120)
    pendown()

Upvotes: 0

Views: 803

Answers (3)

cdlane
cdlane

Reputation: 41872

This is a nice problem for a recursive solution, which leaves our turtle where it started once it's finished:

from turtle import *

NUMBER_OF_SHAPES = 4

def triangles(n=NUMBER_OF_SHAPES):
    if n < 1:
        return

    distance = (NUMBER_OF_SHAPES - (n - 1)) * 20

    forward(distance/2)
    left(120)
    forward(distance)

    right(120)
    triangles(n - 1)  # recurse
    right(120)

    forward(distance)
    left(120)
    forward(distance/2)

triangles()

done()

enter image description here

Upvotes: 0

Alain T.
Alain T.

Reputation: 42143

You have to end up at the top of the triangle after drawing it. You can do that by drawing 5 sides (that last two being overdrawn) then reorient your turtle to point to the right again and go back half of a triangle's size to start the new triangle from its bottom left corner.

from turtle import *

size = 80               # starting size
for _ in range(4):      # will draw 4 triangles
    forward(-size//2)   # move to bottom left
    for _ in range(5):  # draw triangle, end up on top
        forward(size)
        left(120)
    left(120)           # complete rotation
    size += 20          # increase size

Upvotes: 0

Anis R.
Anis R.

Reputation: 6912

Your logic would work, if the side (length) of each triangle is double that of the previous one:

from turtle import *
number_of_shapes = 4

# side length of smallest triangle
side_length = 20

for shape in range(1, number_of_shapes + 1):
    for sides in range(1, 4):
        forward(side_length)
        left(120)

    penup()
    left(120)
    forward(side_length)
    right(120)
    pendown()

    #for next triangle, double the length
    side_length *= 2

However, if you need the side of the triangles to always increase by 20 (like in your original example), then one way is doing the following:

  • After drawing one triangle, go back to the triangle's top vertex.

  • From there, set the pen angle to 180 degrees (so the turtle points left)

  • Move forward by half of the new triangle's length.

      from turtle import *
      number_of_shapes = 4
    
      # side length of smallest triangle
      side_length = 20
    
      for shape in range(1, number_of_shapes + 1):
          # draw a triangle
          for sides in range(1, 4):
              forward(side_length)
              left(120)
    
          # go back to the triangle's top vertex
          penup()
          left(60)
          forward(side_length)
    
          # move into position for the next triangle
          side_length += 20
          setheading(180)
          forward(side_length/2)
          left(180)
          pendown()
    

Upvotes: 1

Related Questions