Simple Python Turtle Graphic and Code: Lattice

Опубликовано: 05 Октябрь 2024
на канале: Basic Python Turtle Art
39
1

A pattern created with rows of oppositely-oriented triangles. The triangles are automatically color-filled using this Turtle method: the shapes closed by the line joining the initial and final points of drawing are the only ones colored. In this graphic, each row of triangles has initial and final points.

Feel free to copy the basic Python Turtle code that is given below. Don't hesitate to ask questions about the code if you have any. Enjoy! Please comment, like, or subscribe :)

Incidentally, for manually colorable graphics and variations, please visit my author site at https://www.amazon.com/author/basicpy...

import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
t.pensize(4)
t.color("purple", "purple") #Assigned color for pen and fill, respectively; Both changeable
length_of_graphic = 600 #Changeable
height_of_graphic = (5 / 6) * length_of_graphic
dimension_check = length_of_graphic / 10 #Change in y value of starting point for row or line_of_triangles
def line_of_triangles(): #Procedure for drawing a row of alternately oriented triangles
t.fillcolor() #Start of color-filling procedure
t.begin_fill()
inverter = (-1) ** (1 + j) #Changer of angle or heading's algebraic sign for start of row
t.left(inverter * 45)
t.forward(length_of_graphic / 10 * 2 ** 0.5) #Initial line in the row of alternately oriented triangles
for i in range(4): #Loop to draw the indicated number (changeable) of intermediate lines
t.right(inverter * 90)
t.forward(2 * length_of_graphic / 10 * 2 ** 0.5)
inverter = (-1) ** (j + i) #Changer of angle or heading's algebraic sign for intermediate lines
t.right(inverter * 90)
t.forward(length_of_graphic / 10 * 2 ** 0.5) #Terminal line in the row of alternately oriented triangles
t.end_fill() #End of color-filling procedure
t.penup() #Python_graphic start of drawing procedure
t.goto(-length_of_graphic / 2, height_of_graphic / 2 - dimension_check) #Trail-less movement of turtle to starting point of
t.pendown() #first row
for j in range(4): #Loop to draw 4 rows of alternately oriented triangles
line_of_triangles() #Function call to draw a row of alternately oriented triangles
t.setheading(0)
t.penup()
t.goto(-length_of_graphic / 2, height_of_graphic / 2 - (3 + 2 * j) * dimension_check) #Trail-less movement of turtle
t.pendown() #to starting point of succeeding rows
t.hideturtle()
screen.exitonclick()