Simple Python Turtle Graphic and Code: Stylized Yin Yang

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

A stylized version of the Yin and Yang symbol, featuring two sets of concentric circles, each one displaying progressive transition of shade from gray. The transition in color from lighter to darker shade or from darker to lighter is facilitated by the use of sign_changer -- a variable that alters the algebraic sign of other key variables in the code.

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 url at https://www.amazon.com/author/basicpy...

import turtle
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
screen.colormode(1.0) #Requirement for use of 0 to 1.0 for r, g, and b color values
t.pensize(4)
number_of_color_shades = 6 #Changeable; Count of B and W shades in concentric circles
radius = 240 #Changeable; Distance from center of graphic to outer edge
r = g = b = 1 #Initial value of r, g, and b; (1, 1, 1) for "white"
rad = radius / (2 * number_of_color_shades) #Intermediate variable rad; Related to quantity difference_in_radius
difference_in_radius = rad #which takes in rad's initial value only
difference_in_shade = 1 / (number_of_color_shades - 1) #Difference in B and W shade between two adjacent concentric circles
def basic_yinyang(): #Procedure for drawing the classic Yin Yang symbol
t.fillcolor(0, 0, 0) #Drawing start for black portion of symbol
t.begin_fill()
t.circle(radius, 180)
t.left(180)
t.circle(-radius / 2, 180)
t.circle(radius / 2, 180)
t.end_fill()
t.left(180)
t.fillcolor(1, 1, 1) #Drawing start for white portion of symbol
t.begin_fill()
t.circle(-radius, 180)
t.circle(-radius / 2, 180)
t.circle(radius / 2, 180)
t.end_fill()
t.penup() #Python_Graphic start of drawing procedure
t.goto(0, - radius) #Trail-less movement of turtle to start of drawing Yin Yang
t.pendown()
basic_yinyang() #Function call to draw the classic Yin Yang symbol
t.penup()
t.home() #Trail-less movement of turtle to center of graphic
for sign_changer in [1, -1]: #Loop to draw two groups of concentric circles
for i in range(number_of_color_shades -1): #Loop to draw a group of concentric circles
t.goto(0, sign_changer * (1 + i) * difference_in_radius) #Trail-less movement of turtle to start of concentric circles
b = b - sign_changer * difference_in_shade #Slight change in b value (initially 1) to a lower one to reflect
r = g = b #incremental darkening of B and W shade from one concentric
t.pendown() #circle to the next
t.fillcolor(r, g, b)
t.begin_fill()
t.circle(radius / 2 - (1 + i) * difference_in_radius)
t.end_fill()
t.penup()
t.home() #Preparation to draw the next group of concentric circles whose
t.left(180) #sign_changer value is now -1
t.hideturtle()
screen.exitonclick()