Simple Python Turtle Graphic and Code: Spirograph 7

Veröffentlicht am: 07 August 2024
auf dem Kanal: Basic Python Turtle Art
259
5

A geometric image created "without lifting the pen" and with points on invisible circumference as guide.

Feel free to copy the simple 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.speed(7)
t.pensize(4)
radius = 250 #Changeable to a number close to 250; Size of graphic
number_of_points = 18 #Changeable; Count of ordered pairs on circumference of invisible circle
list_of_points = [] #Initialization of set named list_of_points
def set_of_points(radius): #Procedure for adding orderd pairs to the set named list_of_points
for i in range(number_of_points): #Loop to list ordered pairs number_of_points times
new_point = t.position() #Temporary assignment of current ordered pair to new_point
list_of_points.append(new_point) #Addition of ordered pair in new_point to the set list_of_points
t.circle(radius, 360 / number_of_points) #Search for new point by the circumference-traveling turtle
t.penup() #Python_Graphic start of drawing procedure
t.goto(0, -radius) #Trail-less movement of turtle to the point with given coordinates
set_of_points(radius) #Function call to add orderd pairs to the set named list_of_points
t.pendown()
for i in range(number_of_points): #Loop to draw the basic pattern of spirograph number_of_points times
t.setheading(t.towards(0,0)) #Direction initialization for turtle before drawing a basic pattern
t.color("red") #Changeable dominant color
t.circle(1.3 * radius, 30) #Drawing of first portion of first 60-degree arc
t.color("green") #Changeable to a color not the same as the dominant one above
t.circle(1.3 * radius, 30) #Drawing of second portion of first 60-degree arc
t.right(120) #Turtle rotation before drawing the second 60-degree arc
t.circle(0.75 * radius, 60) #Drawing of second 60-degree arc
if i == number_of_points - 1: #Conditional execution if the third arc to be drawn belongs to last pattern
t.setheading(t.towards(list_of_points[0])) #Turtle rotation to head towards first point in the set list_of_points
t.right(30) #Adjustment in turtle rotation to ensure the 60-degree arc ends on point
t.color("red") #Changeable to the color specified as dominant above
t.circle((t.distance(list_of_points[0])), 60) #Drawing of third 60-degree arc
else: #Conditional execution if the third arc to be drawn is not in last pattern
t.setheading(t.towards(list_of_points[i + 1])) #Turtle rotation to head towards next point in the set list_of_points
t.right(30)
t.color("red")
t.circle(t.distance((list_of_points[i + 1])), 60)
t.hideturtle()
screen.exitonclick()


Auf dieser Seite können Sie das Online-Video Simple Python Turtle Graphic and Code: Spirograph 7 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Basic Python Turtle Art 07 August 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 259 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!