Simple Python Turtle Graphic and Code Octagon Swirl
HTML-код
- Опубликовано: 30 сен 2024
- A spinning wheel generated by alternating-colored and size-decreasing octagons drawn one on top of another.
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 www.amazon.com...
import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
t.pensize(3)
t.speed(0)
number_of_octagons = 24 #Changeable to a number close to 18
radius = 265 #Changeable; Distance from center to vertex of current octagon
vertex = [] #Initialization of list to contain vertices of current octagon
bent = 0.6 #Changeable to a number between 0.1 and 0.9, preferably; Curvature of swirl
anchor_pt = () #Space to contain starting point of next octagon's drawing
color_switch = 0 #Toggle to alter from one (color_switch = 0) to another (color_switch = 1)
def octagon(): #Procedure for drawing the current octagon
for i in range(8):
if i == 7:
t.goto(vertex[0]) #Step to connect vertex[7] to vertex[0] if side of current pentagon is the last one
else:
t.goto(vertex[i + 1])
t.penup() #Python_Graphic start of drawing procedure
t.left(112.5) #Step to point turtle to left vertex of 2 vertices on top of octagon
t.forward(radius) #Trail-less movement of turtle to left vertex of 2 vertices on top of octagon
t.right(90) #Preparatory rotation of turtle before tracing an invisible circle clockwise
for i in range(number_of_octagons): #Loop to draw the number of pentagons indicated in the initialization above
for j in range(8): #Embedded loop to locate and list the 8 vertices of the current octagon
vertex.append(t.pos()) #Step to list the current position of turtle as a vertex of the current octagon
t.circle(-radius, 45) #Circular trace of turtle to locate the next vertex of current octagon
t.right(22.5) #Turtle rotation to start drawing the current octagon
t.pendown()
if color_switch == 0: #Conditional statement asking if color_switch = 0
t.color("purple") #Steps to execute if color_switch = 0; Color changeable
color_switch = 1
else:
t.color("orange") #Steps to execute if color_switch = 1; Color changeable
color_switch = 0
t.begin_fill() #Star of color fill procedure for current octagon
t.forward(bent * t.distance(vertex[1])) #Lined movement of turtle to locate starting point of next octagon's drawing
anchor_pt = t.pos() #Assignment of current turtle position to anchor_pt -- the start of next octagon
octagon() #Function call to draw the current octagon with vertices listed in "vertex"
t.end_fill() #End of color fill procedure for current octagon
t.penup()
t.home() #Movement of turtle to initial position to prepare for next octagon's drawing
radius = t.distance(anchor_pt) #Redefinition of radius as distance between (0, 0) and the current anchor_pt
degrees = t.towards(anchor_pt) #Angle created by invisible line joining (0, 0) and current anchor_pt
t.setheading(degrees) #Turtle rotation to point towards anchor_pt -- the start of next octagon's drawing
t.goto(anchor_pt) #Trail-less movement of turtle to anchor_pt -- the start of next octagon's drawing
t.right(90) #Preparatory rotation of turtle before tracing the next invisible circle clockwise
vertex.clear() #Clearing of "vertex" list to accommodate newly located vertices for the next (the
t.hideturtle() #new "current") octagon
screen.exitonclick()