带 Python 乌龟的圆角矩形
Rounded edge rectangles with Python turtle
除了画一个圆并在其上画一个正方形之外,还有其他方法可以为 square/rectangle 形状添加圆边吗?我正在尝试仅使用 Python 乌龟来制作 YouTube 徽标。我没有使用像 home() 这样的绝对位置函数,因为稍后我需要在不同的位置复制它。
from turtle import *
def placement():
penup()
setheading(0)
forward(5)
setheading(90)
forward(20)
setheading(0)
pendown()
def youtube():
placement() #Custom Starting poisition
hideturtle()
pencolor("#d43b33") # Pen Color
fillcolor("#d43b33") #Youtube Red Colour RGB Code
begin_fill()
forward(90)
setheading(90) #Face Up
forward(30)
setheading(180) #Face Left
forward(90)
setheading(270) #Face Down
forward(30)
end_fill()
setheading(0) #Second Half Youtube Logo
forward(90)
setheading(90)
forward(30)
pencolor("#fa453b") # Pen Color
fillcolor("#fa453b") #Youtube Ligther Red Colour RGB Code
begin_fill()
forward(30)
setheading(180) # Face Left
forward(90)
setheading(270) #Face Down
forward(30)
setheading(0) #turn right
forward(90)
end_fill()
penup()
fillcolor("#ffffff") #Youtube White Play button RGB Code
setheading(180)
forward(55)
setheading(90)
begin_fill()
forward(20)
setheading(315)
forward(30)
setheading(225)
forward(30)
setheading(90)
forward(20)
end_fill()
Draw
首先,我不认为 YouTube 徽标是 rounded rectangle, but rather a hyperellipse. But let's assume a rounded rectangle to keep things simple. Second, I don't believe your colors, nor your arrow, conform to the YouTube Brand 所以我会稍微调整一下:
from turtle import Turtle, Screen
def rounded_rectangle(turtle, short, long, radius):
diameter = radius * 2
heading = turtle.heading()
turtle.setheading(270)
isdown = turtle.isdown()
if isdown:
turtle.penup()
turtle.goto(turtle.xcor() - long/2, turtle.ycor() - short/2 + radius)
turtle.pendown()
for _ in range(2):
turtle.circle(radius, 90)
turtle.forward(long - diameter)
turtle.circle(radius, 90)
turtle.forward(short - diameter)
turtle.penup() # restore turtle state, position and heading
turtle.goto(turtle.xcor() + long/2, turtle.ycor() + short/2 - radius)
if isdown:
turtle.pendown()
turtle.setheading(heading)
def youtube(turtle):
turtle.color("#ff0000") # YouTube red pen color RGB code
turtle.begin_fill()
rounded_rectangle(turtle, 60, 90, 10)
turtle.end_fill()
turtle.penup()
turtle.color("#ffffff") # YouTube white play button RGB code
turtle.setheading(0)
turtle.backward(8)
turtle.setheading(90)
turtle.pendown()
turtle.begin_fill()
turtle.forward(12)
for _ in range(2):
turtle.right(120)
turtle.forward(24)
turtle.right(120)
turtle.forward(12)
turtle.end_fill()
def placement(turtle):
turtle.penup()
turtle.forward(5)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.pendown()
screen = Screen()
yertle = Turtle(visible=False)
placement(yertle) # Custom starting position
youtube(yertle)
screen.mainloop()
除了画一个圆并在其上画一个正方形之外,还有其他方法可以为 square/rectangle 形状添加圆边吗?我正在尝试仅使用 Python 乌龟来制作 YouTube 徽标。我没有使用像 home() 这样的绝对位置函数,因为稍后我需要在不同的位置复制它。
from turtle import *
def placement():
penup()
setheading(0)
forward(5)
setheading(90)
forward(20)
setheading(0)
pendown()
def youtube():
placement() #Custom Starting poisition
hideturtle()
pencolor("#d43b33") # Pen Color
fillcolor("#d43b33") #Youtube Red Colour RGB Code
begin_fill()
forward(90)
setheading(90) #Face Up
forward(30)
setheading(180) #Face Left
forward(90)
setheading(270) #Face Down
forward(30)
end_fill()
setheading(0) #Second Half Youtube Logo
forward(90)
setheading(90)
forward(30)
pencolor("#fa453b") # Pen Color
fillcolor("#fa453b") #Youtube Ligther Red Colour RGB Code
begin_fill()
forward(30)
setheading(180) # Face Left
forward(90)
setheading(270) #Face Down
forward(30)
setheading(0) #turn right
forward(90)
end_fill()
penup()
fillcolor("#ffffff") #Youtube White Play button RGB Code
setheading(180)
forward(55)
setheading(90)
begin_fill()
forward(20)
setheading(315)
forward(30)
setheading(225)
forward(30)
setheading(90)
forward(20)
end_fill()
Draw
首先,我不认为 YouTube 徽标是 rounded rectangle, but rather a hyperellipse. But let's assume a rounded rectangle to keep things simple. Second, I don't believe your colors, nor your arrow, conform to the YouTube Brand 所以我会稍微调整一下:
from turtle import Turtle, Screen
def rounded_rectangle(turtle, short, long, radius):
diameter = radius * 2
heading = turtle.heading()
turtle.setheading(270)
isdown = turtle.isdown()
if isdown:
turtle.penup()
turtle.goto(turtle.xcor() - long/2, turtle.ycor() - short/2 + radius)
turtle.pendown()
for _ in range(2):
turtle.circle(radius, 90)
turtle.forward(long - diameter)
turtle.circle(radius, 90)
turtle.forward(short - diameter)
turtle.penup() # restore turtle state, position and heading
turtle.goto(turtle.xcor() + long/2, turtle.ycor() + short/2 - radius)
if isdown:
turtle.pendown()
turtle.setheading(heading)
def youtube(turtle):
turtle.color("#ff0000") # YouTube red pen color RGB code
turtle.begin_fill()
rounded_rectangle(turtle, 60, 90, 10)
turtle.end_fill()
turtle.penup()
turtle.color("#ffffff") # YouTube white play button RGB code
turtle.setheading(0)
turtle.backward(8)
turtle.setheading(90)
turtle.pendown()
turtle.begin_fill()
turtle.forward(12)
for _ in range(2):
turtle.right(120)
turtle.forward(24)
turtle.right(120)
turtle.forward(12)
turtle.end_fill()
def placement(turtle):
turtle.penup()
turtle.forward(5)
turtle.left(90)
turtle.forward(20)
turtle.right(90)
turtle.pendown()
screen = Screen()
yertle = Turtle(visible=False)
placement(yertle) # Custom starting position
youtube(yertle)
screen.mainloop()