經過測試@ moomoomoo309的橢圓代碼和發現問題(在錯誤的地方打印,寬度和高度不匹配的參數,忽略龜航向所以不能打印傾斜的橢圓,品目不跟蹤圖紙,沒有按不要將筆留在原始狀態等等)我決定嘗試寫我自己的。
我選擇使用turtle.circle()
作爲相對於現有烏龜位置和標題繪製橢圓的位置的模型,允許用戶改變步驟(即製作其他不規則多邊形),離開筆狀態和位置它從哪裏開始,等等這是我想出了(我用self
代替turtle
或pen
我預期它安裝爲一個方法):
import turtle
import math
def ellipse(self, x_radius, y_radius, steps=60):
down = self.isdown() # record pen state for restoration later
if not down:
self.pendown()
heading_radians = math.radians(self.heading())
theta_radians = -math.pi/2
extent_radians = 2 * math.pi
step_radians = extent_radians/steps
extent_radians += theta_radians
x_center, y_start = self.position()
y_center = y_start + y_radius
cos_heading, sin_heading = math.cos(heading_radians), math.sin(heading_radians)
while True:
x, y = x_center + math.cos(theta_radians) * x_radius, y_center + math.sin(theta_radians) * y_radius
# readjust x & y to set the angle of the ellipse based on the original heading of the turtle
x, y = x - x_center, y - y_start
x, y = x * cos_heading - y * sin_heading, x * sin_heading + y * cos_heading
x, y = x + x_center, y + y_start
self.setheading(self.towards(x, y)) # turtle faces direction in which ellipse is drawn
self.goto(x, y)
if theta_radians == extent_radians:
break
theta_radians = min(theta_radians + step_radians, extent_radians) # don't overshoot our starting point
self.setheading(self.towards(x_center, y_start)) # set correct heading for the next thing we draw
if not down: # restore pen state on return
self.penup()
(可選)這個方法每Adding a Method to an Existing Object Instance添加到我們的龜:
from functools import partial
yertle = turtle.Turtle()
yertle.ellipse = partial(ellipse, yertle)
示範代碼,以顯示新的形狀,我們可以得出turtle.ellipse()
:
if __name__ == "__main__":
from functools import partial
yertle = turtle.Turtle()
yertle.ellipse = partial(ellipse, yertle)
import random
yertle.speed("fastest")
yertle.hideturtle()
yertle.penup()
screen = turtle.Screen()
for _ in range(75):
radius = random.randint(10, 50)
yertle.setheading(random.randint(0, 360))
yertle.setx(random.randint(-screen.window_width()/2 + radius * 2, screen.window_width()/2 - radius * 2))
yertle.sety(random.randint(-screen.window_height()/2 + radius + 2, screen.window_height()/2 - radius * 2))
yertle.color((random.random(), random.random(), random.random()), (random.random(), random.random(), random.random()))
flag = random.choice([True, False, False])
if flag:
yertle.begin_fill()
yertle.ellipse(radius, radius/0.5 + random.random() * 3, steps=random.choice([3, 4, 5, 6, 7, 8, 60, 60, 60]))
if flag:
yertle.end_fill()
screen.exitonclick()
示例輸出
我試圖實現extent
一拉turtle.circle()
但無法得到它的正常範圍的任意工作(即通過這種方式,你可以調用turtle.ellipse()
兩次,並且讓它繼續保持原來的曲線),所以我已經離開了那一天。
將我的答案回OP原來的問題,我們現在可以這樣做:
import turtle
import math
def ellipse(self, x_radius, y_radius, steps=60):
# ...
def draw_O():
# Draw an O
turtle.penup()
turtle.forward(letter_height/4)
turtle.pendown()
ellipse(turtle, letter_width, letter_height)
turtle.penup()
turtle.forward(space_width + letter_height/4)
turtle.pendown()
letter_width = 10
letter_height = 170
space_width = 5
turtle.onkey(draw_O, "o")
turtle.listen()
turtle.done()
要生成的OP期望瘦基於橢圓的字母O:
你意味着動態操縱?或只是參數化? –
@ Peter Wood那麼,是一個參數化或動態操作的圓? –
我認爲你必須用很多直線來近似它。 –