我试图找到需要形成没有我的“龟”正多边形迭代的次数最少(形状)重复其运动....和注意到一个怪(?)的关系,我不能精确定位。>
如果您运行下面的代码并尝试不同的值(请注意:请确保将参数'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()
您会发现海龟以圆周运动移动。
例如,您会注意到x = 100时,最小值为。的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的倍数,或四圈方形的事,但[上述]使我拒绝我的假设。
那么有人对通用规则有更好的主意吗?干杯。
答案 0 :(得分:0)
那么有人对通用规则有更好的主意吗?
我相信我已经缩小了范围。您的表格中有一些错误。并且有四种不同类型的异常,而不仅仅是“将结果除以4”之一。实际上,在360个因素中,异常的发生率比简单的360 / x
规则要高。四个例外是:
之后,如果n = 360 / x
是{1>,则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()
答案 1 :(得分:0)
根据@cdlane标识的规则集,我找到了一种快速查找最小值的方法。任何输入x的迭代次数(无论是否为360因子)都需要完成常规形状! (当然,我也意识到某些情况下根本没有最小值,例如x为20.75时)
。
下面的代码显示了我对已确定的故障的更正以及添加heading()的功能,以检查麦克在循环后是否已返回其原始位置:
import turtle
def draw_square(angle, repetitions):
mike = turtle.Turtle()
mike.shape("turtle")
mike.color("red")
mike.speed("fastest")
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
print("Turn ", count, "; ", mike.heading())
if mike.heading() == 0:
break
print("Min. iterations needed to complete cycle: ", count)
if __name__ == "__main__":
wn = turtle.Screen()
wn.bgcolor("black")
x = int(input("Enter angle: "))
n = int(input("Enter boundary: ")) # For n, advisably to a v large number; while loop will probably break before reaching its limit anyways
draw_square(x, n)
wn.exitonclick()