Task 3 – CyramiD Code

# Lesson I've learned from this task: 
# I have to redo my pyramid as I didn't use the letter given (C and D). 
# I use 2 simple for loop functions and the plotting in graph paper is attached in PDF format for more details.
# I'm struggling to make the curve version for the letter D into the pyramid position due to the angle rotation for letter D. 
# It works fine for letter C but unfortunately not for letter D at first. But then Mina advised, maybe try to bring back the code to turtle.home position at the end and it works perfectly now. 
# One version is curved line and the second version is straight line. 
# So for my submission, I use 5 levels (the total letter from my first name: Myrna) for letter C both straight and curve version.
# I use 7 levels (the total letter from my last name: Kennedy) for letter D both version: straight and curve.  


import axi
A4_PORT = (0, 0, 8.25, 11.75) # A4 Portrait bounds
A4_LAND = (0, 0, 11.75, 8.25) # A4 Landscape bounds
BOUNDS = A4_LAND

def straight_C(turtle, x, y): #straight curve C instruction
    turtle.pu() # pen up
    turtle.goto(x-4, y-3) # go to point (x-4, y-3)
    turtle.pd() # pen down
    turtle.goto(x-5, y-2) # go to point (x-5, y-2)
    turtle.goto(x-5, y-1) # go to point (x-5, y-1)
    turtle.goto(x-4, y) # go to point (x-4, y)
    turtle.goto(x-1, y) # go to point (x-1, y)
    turtle.goto(x, y-1) # go to point (x, y-1)
    turtle.goto(x, y-2) # go to point (x, y-2)
    turtle.goto(x-1, y-3) # go to point (x-1, y-3)
    turtle.pu() # pen up
    turtle.home() # bring back the turtle to home position

def straight_D(turtle, x, y): #straight curve D instruction
    turtle.pu() # pen up 
    turtle.goto(x, y) # go to point (x, y)
    turtle.pd() # pen down
    turtle.goto(x-5, y) # go to point (x-5, y)
    turtle.goto(x-5, y-2) # go to point (x-5, y-2)
    turtle.goto(x-4, y-3) # go to point (x-4, y-3)
    turtle.goto(x-1, y-3) # go to point (x-1, y-3)
    turtle.goto(x, y-2) # go to point (x, y-2)
    turtle.goto(x, y) # go to point (x, y)
    turtle.pu() # pen up   
    turtle.home() # bring back the turtle to home position

def curve_C(turtle, x, y): #round curve C instruction
    turtle.pu() # pen up
    turtle.goto(x-4, y-3) # go to point (x-4, y-3)
    turtle.pd() # pen down
    r = 1 # radius for the curve quarter circle of C
    turtle.circle(r, -90, 10) # draw a circular shape with radius r and covering -90° (quarter circle) constituted of 10 lines
    turtle.goto(x-5, y-1) # go to point (x-5, y-1)
    r = 1 # radius for the curve quarter circle of C
    turtle.circle(r, -90, 10) # draw a circular shape with radius r and covering -90° (quarter circle) constituted of 10 lines
    turtle.goto(x-1, y) # go to point (x-1, y)
    r = 1 # radius for the curve quarter circle of C
    turtle.circle(r, -90, 10) # draw a circular shape with radius r and covering -90° (quarter circle) constituted of 10 lines
    turtle.goto(x, y-2)  # go to point (x, y-2)
    r = 1 # radius for the curve quarter circle of C
    turtle.circle(r, -90, 10) # draw a circular shape with radius r and covering -90° (quarter circle) constituted of 10 lines
    turtle.pu()  # pen up
    turtle.home() # bring back the turtle to home position

def curve_D(turtle, x, y): #round curve D instruction
    turtle.pu() # pen up
    turtle.goto(x, y) # go to point (x, y)
    turtle.pd() # pen down
    turtle.goto(x-5, y) # go to point (x-5, y)
    turtle.goto(x-5, y-2) # go to point (x-5, y-2)
    turtle.left(90) # rotate 90° to the left 
    r = 1 # radius for the curve quarter circle of D
    turtle.circle(r, 90, 10) # draw a circular shape with radius r and covering 90° (quarter circle) constituted of 10 lines
    turtle.goto(x-1, y-3) # go to point (x-1, y-3)
    r = 1 # radius for the curve quarter circle of D
    turtle.circle(r, 90, 10) # draw a circular shape with radius r and covering 90° (quarter circle) constituted of 10 lines
    turtle.goto(x, y) # go to point (x, y)
    turtle.pu()  # pen up
    turtle.home() # bring back the turtle to home position
    
def pyramid(turtle, width, height, levels): 
    # I originally copy the function from Google Colab for pyramid but then I modify it according to my letter configuration and plot it on the graph paper. 
    # For the step by step how I figured out this pyramid function, please see my paper submission.

    for a in range(levels):
        for b in range(a+1):
            #curve_C(turtle, width*(b-a)*2, height*(a+b)) 
            #straight_C(turtle, width*(b-a)*2, height*(a+b))
            #straight_D(turtle, width*(b-a)*2, height*(a+b))
            curve_D(turtle, width*(b-a)*2, height*(a+b))  
          
def save_img(turtle, name = 'out'):
    drawing = turtle.drawing.rotate_and_scale_to_fit(BOUNDS[2], BOUNDS[3], step=90, padding=0.5) # scale letter to fit A4-sized paper
    im = drawing.render(bounds=BOUNDS) # render drawing
    im.write_to_png(name + '.png') # save a image of your drawing in a file called name.png
    
def draw_img(turtle):
    drawing = turtle.drawing.rotate_and_scale_to_fit(BOUNDS[2], BOUNDS[3], step=90, padding=0.5) # scale letter to fit A4-sized paper
    axi.draw(drawing)
 
def main():
    turtle = axi.Turtle()
    width = 3
    height = 3
    levels = int(input ("Please enter levels: ")) # Matt helped me with this input which I really like to customise the levels as required
    pyramid(turtle, width, height, levels)
       
    output_img = 'Task3_Dyramid_c'
    save_img(turtle, output_img)
    # draw_img(turtle) # Uncomment to draw your image

if __name__ == '__main__':
    main()