pythonturtle-graphicspython-turtle

Change my Python Turtle code for N columns and M rows


I have my code ready to show 3x3 equilateral triangles, but I don't know how to change it so N amount of equilateral triangles can be shown in a column (one triangle above another one) and M amount of equilateral triangles can be shown in a row (one triangle next to another one). Any help is much appreciated!

My code is:

import turtle         
ara = turtle.Turtle()    #name of the turtle
ara.speed(0)            
ara.pensize(10)

def pile_left(t, l, n):          #first column
    t = l * 3 ** 0.5 / 2
    for j in range(n):
        if j == 0 : ara.fillcolor('red')
        if j == 1 : ara.fillcolor('orange')
        if j == 2 : ara.fillcolor('green')
        ara.pu()            
        ara.shape('turtle')    
        ara.begin_fill()
        for i in range(3):     
            ara.lt(120)        
            ara.fd(l)          
        ara.end_fill()
        ara.sety(ara.ycor() + t)
pile_left(ara, 60, 3)

def pile_middle(t, l, n):         #second column
    t = l * 3 ** 0.5 / 2
    ara.goto(60,0)
    for j in range(n):
        if j == 0 : ara.fillcolor('purple')
        if j == 1 : ara.fillcolor('yellow')
        if j == 2 : ara.fillcolor('peachpuff')
        ara.pu()            
        ara.shape('turtle')    
        ara.begin_fill()
        for i in range(3):    
            ara.lt(120)        
            ara.fd(l)          
        ara.end_fill()
        ara.sety(ara.ycor() + t)
pile_middle(ara, 60, 3)

def pile_right(t, l, n):          #third column
    t = l * 3 ** 0.5 / 2
    ara.goto(120,0)
    for j in range(n):
        if j == 0 : ara.fillcolor('grey')
        if j == 1 : ara.fillcolor('brown')
        if j == 2 : ara.fillcolor('blue')
        ara.pu()           
        ara.shape('turtle')    
        ara.begin_fill()
        for i in range(3):    
            ara.lt(120)       
            ara.fd(l)          
        ara.end_fill()
        ara.sety(ara.ycor() + t)
pile_right(ara, 60, 3)

turtle.mainloop() 

You can run it on https://trinket.io/python to see who it currently does.


Solution

  • I have my code ready to show 3x3 equilateral triangles

    No, not really. You have your code ready to show 1x3 equilateral triangles, and you simply duplicated your code three times to emulate a second dimension. Your problem is that you can't duplicate your code M times to solve this problem.

    The answer in this situation is less, but smarter, code. We're going to need nested loops for N and M, and the ability to move forward by length l for each M and vertically by height t for each N:

    from turtle import Screen, Turtle
    
    COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
    
    def pile(turtle, length, columns, rows):
        height = length * 3 ** 0.5 / 2
        x_origin = turtle.xcor()
        color = 0
    
        for _ in range(rows):
            for _ in range(columns):
    
                turtle.color(COLORS[color % len(COLORS)])
    
                turtle.begin_fill()
                for _ in range(3):
                    turtle.forward(length)
                    turtle.left(120)
                turtle.end_fill()
    
                turtle.forward(length)
    
                color += 1
    
            turtle.setposition(x_origin, turtle.ycor() + height)
    
    screen = Screen()
    
    yertle = Turtle('turtle')
    yertle.speed('fastest')
    yertle.penup()
    
    pile(yertle, 60, 4, 5)
    
    yertle.hideturtle()
    screen.exitonclick()
    

    However, this is a perfect example where stamping would be simpler than filling. We can make the turtle itself an equilateral triangle, then move it and stamp it:

    from turtle import Screen, Turtle
    
    COLORS = ['red', 'orange', 'green', 'purple', 'yellow', 'pink', 'grey', 'brown', 'blue']
    CURSOR_SIZE = 20
    
    def pile(turtle, length, columns, rows):
        turtle.shapesize(length / CURSOR_SIZE)
    
        height = length * 3 ** 0.5 / 2
        y_origin = turtle.ycor()
        color = 0
    
        for _ in range(columns):
            for _ in range(rows):
    
                turtle.color(COLORS[color % len(COLORS)])
                turtle.stamp()
                turtle.forward(height)
    
                color += 1
    
            turtle.setposition(turtle.xcor() + length, y_origin)
    
    screen = Screen()
    
    yertle = Turtle('triangle')
    yertle.speed('fastest')
    yertle.setheading(90)
    yertle.penup()
    
    pile(yertle, 60, 4, 5)
    
    yertle.hideturtle()
    screen.exitonclick()
    

    Not only simpler, but faster too!