A Python program written by a junior high school student. It was well done for the first time. At first, you can use any writing style. I think.
However, in order to be able to create difficult programs, it is important to organize your mind and make the program easy to understand. I rewrote this program as an example.
I was able to draw beautiful figures. It is wonderful to devise a way to put out colorful shades. I made it by carefully considering the conditional division of the if statement and the calculation formula.
number = int(input("How many circles do you draw?"))
R = 1.0
G = 0.0
B = 0.0
color = 0
from turtle import *
delay(5)
shape("turtle")
for i in range(number):
color = int(360/number*i)
if color <= 60:
R = 1.0
G = color/60
B = 0.0
elif color <= 120:
R = 2.0-color/60
G = 1.0
B = 0.0
elif color <= 180:
R = 0.0
G = 1.0
B = color/60-2.0
elif color <= 240:
R = 0.0
G = 4.0-color/60
B = 1.0
elif color <= 300:
R = color/60-4.0
G = 0.0
B = 1.0
elif color <= 360:
R = 1.0
G = 0.0
B = 6.0-color/60
pencolor(R,G,B)
circle(100)
left(360/number)
done()
Execution result
import moves first The part for finding the RGB value is functionalized I can't see the regularity of the formula in the if statement. I think it was made by trial and error. Changed to a regular expression with the same result. = Is included in the conditional expression of the if statement, but the result does not change even if = is excluded. color Considering that the range is 0 to 359, it seems more appropriate to not add =.
import turtle
def color2rgb(color):
if color < 60:
R = 1.0
G = color/60
B = 0.0
elif color < 120:
R = 1.0-(color-60)/60
G = 1.0
B = 0.0
elif color < 180:
R = 0.0
G = 1.0
B = (color-120)/60
elif color < 240:
R = 0.0
G = 1.0-(color-180)/60
B = 1.0
elif color < 300:
R = (color-240)/60
G = 0.0
B = 1.0
elif color < 360:
R = 1.0
G = 0.0
B = 1.0-(color-300)/60
return(R, G, B)
def draw(number):
turtle.delay(5)
turtle.shape("turtle")
for i in range(number):
color = int(360 / number * i)
turtle.pencolor(color2rgb(color))
turtle.circle(100)
turtle.left(360 / number)
turtle.done()
number = int(input("How many circles do you draw?"))
draw(number)
If you look closely at color2rgb (), the values of R, G, and B have the same shape with a phase shift of 120. You can also stop the if statement and create a table. However, if you make it into a table, the readability will decrease a little. Let's do this.
import turtle
def color2r(color):
color = color % 360
if color < 60:
R = 1.0
elif color < 120:
R = 1.0-(color-60)/60
elif color < 180:
R = 0.0
elif color < 240:
R = 0.0
elif color < 300:
R = (color-240)/60
elif color < 360:
R = 1.0
return R
def color2rgb(color):
return color2r(color), color2r(color-120), color2r(color-240)
def draw(number):
turtle.delay(5)
turtle.shape("turtle")
for i in range(number):
color = int(360 / number * i)
turtle.pencolor(color2rgb(color))
turtle.circle(100)
turtle.left(360 / number)
turtle.done()
number = int(input("How many circles do you draw?"))
draw(number)
Looking at the comments I received, the image swelled, so I made a function table version. I don't know if it's easy to read, but I like it.
import turtle
convert_func_table = [lambda x: 1.0, lambda x:1.0-x/60, lambda x:0, lambda x:0, lambda x:x/60, lambda x:1.0]
def convert(i,fine):
return convert_func_table[i%6](fine)
def color2rgb(color):
i, fine = divmod(color, 60)
return (
convert(i, fine),
convert(i+4, fine),
convert(i+2, fine),
)
def draw(number):
turtle.delay(5)
turtle.shape("turtle")
for i in range(number):
color = int(360/number*i)
turtle.pencolor(color2rgb(color))
turtle.circle(100)
turtle.left(360/number)
turtle.done()
number = int(input("How many circles do you draw?"))
draw(number)
Thank you, Mr. K, for providing the program.
Recommended Posts