这周讲勾股定理,班主任突发奇想,让我编一个程序来画勾股海螺图(如图所示)
说起来容易,但对于我这个小白来说,实践起来真的很难!
因为要想知道画笔每次转动角度,还涉及到了三角函数知识(应该是九年级的吧?)
最终,after 4 hours……
I did it!
(有种要崩溃的感觉
)
我肯定绕了弯路,恳请大佬指点!
源码:
from turtle import *
import math
colorName=['blue','yellow']
n=-1
speed(10)
dir=pos()
c=100
begin_fill()
backward(100)
right(90)
while True:
if n==1:
n=0
else:
n=n+1
begin_fill()
a=c
b=100
fillcolor(colorName[n])
forward(100)
dir1=pos()
goto(dir)
c=(c ** 2+10000) ** 0.5
end_fill()
A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c)))
B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c)))
C = math.degrees(math.acos((c * c - a * a - b * b) / (-2 * a * b)))
left(90-A)
goto(dir1)
#测试
print("当前边长:\n")
print("a="+str(a))
print("b="+str(b))
print("c="+str(c))
print("-"*20)
说起来容易,但对于我这个小白来说,实践起来真的很难!
因为要想知道画笔每次转动角度,还涉及到了三角函数知识(应该是九年级的吧?)
最终,after 4 hours……
I did it!
(有种要崩溃的感觉
)我肯定绕了弯路,恳请大佬指点!
源码:
from turtle import *
import math
colorName=['blue','yellow']
n=-1
speed(10)
dir=pos()
c=100
begin_fill()
backward(100)
right(90)
while True:
if n==1:
n=0
else:
n=n+1
begin_fill()
a=c
b=100
fillcolor(colorName[n])
forward(100)
dir1=pos()
goto(dir)
c=(c ** 2+10000) ** 0.5
end_fill()
A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c)))
B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c)))
C = math.degrees(math.acos((c * c - a * a - b * b) / (-2 * a * b)))
left(90-A)
goto(dir1)
#测试
print("当前边长:\n")
print("a="+str(a))
print("b="+str(b))
print("c="+str(c))
print("-"*20)
.jpg)