gpt4 book ai didi

python - 分钟。迭代绘制规则形状 ("turtle")

转载 作者:行者123 更新时间:2023-12-03 22:46:28 25 4
gpt4 key购买 nike

我试图找到形成正多边形所需的最少迭代次数,而我的“turtle ”(形状)不重复其运动......并注意到我无法确定的奇怪(?)关系。

如果您运行下面的代码并尝试使用不同的值(注意:确保将参数“x”和“n”替换为您选择的实际数字):

import turtle

def draw_square():
wn = turtle.Screen()
wn.bgcolor("black")
mike = turtle.Turtle()
mike.shape("turtle")
mike.color("yellow")
mike.speed(100)

count = 0
while count < n: # replace n with number!
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(x) # replace x with number!

if __name__ == "__main__":
draw_square()

你会发现 turtle 在做圆周运动(-ish)。

例如,您会注意到当 x = 100 时,min。形成规则形状所需的 n 值为 36(因为 100°- 90°=10°;360°/10°=36)。
when x = 10 e.g
.

进一步的测试表明:
x = 1, (min.) n = 360                   # 360°/1° = 360

x = 5, (min.) n = 72 # 360°/5° = 72

x = 9, (min.) n = 10* # 360°/9° = 10*

x = 10, (min.) n = 9* # 360°/10° = 9*

x = 45, (min.) n = 8 # 360°/45° = 8

x = 90, (min.) n = 1* # 360°/90° = 4*

## NOTE: no obvs. solution for n, if x isn't factor of 360....

*:奇怪的是,您必须将结果除以 4 才能得到最小值。一些数字的 n 值。
我最初认为这与 9 的倍数有关,或者正方形的四次旋转,但 [以上] 导致我拒绝了我的假设。

所以有人对通用规则有更好的想法吗?干杯。

最佳答案

So anyone have any better ideas as to a generic rule?



我相信我已经缩小了范围。您的表中有一些错误。并且有四种不同类型的异常,而不仅仅是“将结果除以 4”一种。事实上,在 360 的因素中,异常比简单的 360 / x 更频繁地发生。规则。四个异常(exception)是:

之后, n = 360 / x如果 x是一个:
A) multiple of 8 then n *= 4
B) multiple of 4 then n *= 2
C) multiple of 6 and not a multiple of 9 then n /= 2
D) multiple of 2 then n /= 4

规则必须按上述顺序应用,并且只能触发一个规则。如果没有适用的规则,请离开 n照原样。 360所有因子的修改表:
x =   1, n = 360      , 360° /   1° = 360 
x = 2, n = 45 (/ 4), 360° / 2° = 180 (D)
x = 3, n = 120 , 360° / 3° = 120
x = 4, n = 180 (* 2), 360° / 4° = 90 (B)
x = 5, n = 72 , 360° / 5° = 72
x = 6, n = 30 (/ 2), 360° / 6° = 60 (C)
x = 8, n = 180 (* 4), 360° / 8° = 45 (A)
x = 9, n = 40 , 360° / 9° = 40
x = 10, n = 9 (/ 4), 360° / 10° = 36 (D)
x = 12, n = 60 (* 2), 360° / 12° = 30 (B)
x = 15, n = 24 , 360° / 15° = 24
x = 18, n = 5 (/ 4), 360° / 18° = 20 (D)
x = 20, n = 36 (* 2), 360° / 20° = 18 (B)
x = 24, n = 60 (* 4), 360° / 24° = 15 (A)
x = 30, n = 6 (/ 2), 360° / 30° = 12 (C)
x = 36, n = 20 (* 2), 360° / 36° = 10 (B)
x = 40, n = 36 (* 4), 360° / 40° = 9 (A)
x = 45, n = 8 , 360° / 45° = 8
x = 60, n = 12 (* 2), 360° / 60° = 6 (B)
x = 72, n = 20 (* 4), 360° / 72° = 5 (A)
x = 90, n = 1 (/ 4), 360° / 90° = 4 (D)
x = 120, n = 12 (* 4), 360° / 120° = 3 (A)
x = 180, n = 4 (* 2), 360° / 180° = 2 (B)
x = 360, n = 4 (* 4), 360° / 360° = 1 (A)

生成上表的代码:
EXCEPTIONS = [
('A', lambda x: x % 8 == 0, lambda n: n * 4, "(* 4)"),
('B', lambda x: x % 4 == 0, lambda n: n * 2, "(* 2)"),
('C', lambda x: x % 6 == 0 and x % 9 != 0, lambda n: n // 2, "(/ 2)"),
('D', lambda x: x % 2 == 0, lambda n: n // 4, "(/ 4)"),
]

for x in range(1, 360 + 1):
if 360 % x != 0:
continue

n = 360 // x

for exception, test, outcome, explain in EXCEPTIONS:
if test(x):
n = outcome(n)
exception = f"({exception})"
break
else: # no break
exception = explain = '' # no rule applies

angle = 360 // x

print(f"x = {x:3}, n = {n:3} {explain:5}, 360° / {x:3}° = {angle:3} {exception}")

我用来测试单个表条目的代码的返工:
from turtle import Screen, Turtle

def draw_square(angle, repetitions):
mike = Turtle("turtle")
mike.speed('fastest')
mike.color("yellow")

count = 0

while count < repetitions:
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(90)
mike.forward(100)
mike.right(angle)

count += 1

if __name__ == "__main__":
wn = Screen()
wn.bgcolor("black")

draw_square(9, 40)

wn.exitonclick()

enter image description here

关于python - 分钟。迭代绘制规则形状 ("turtle"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54499535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com