自己做的结果,非官方答案,仅供参考,评论可给出自己见解,一起讨论,共同进步( •̀ ω •́ )y
** 3.13 (Turtle: 显示一个STOP牌)编写一个程序,显示一个STOP牌。六边形是红色的而文字是白色的。**
import turtle as t
t.pu()
t.goto(0, -200)
t.pd()
t.begin_fill()
t.color("red")
t.circle(200, steps=6)
t.end_fill()
t.pu()
t.goto(0, -40)
t.pd()
t.color("white")
t.write("STOP",font = ("华文琥珀",80),align = "center")
t.hideturtle()
t.done()
结果:
3.16 (Turtle: 绘制图形)编写一个程序,绘制一个三角形、一个正方形、一个五边形、一个六边形和一个八边形。注意:图形底边平行x轴。(提示:turtle朝向调整60度就可以使三角形的底边平行x轴
import turtle
# 三角形
turtle.pu()
turtle.goto(-500,0)
turtle.pd()
turtle.begin_fill()
turtle.color("pink")
turtle.right(60)
turtle.circle(100, steps=3)
turtle.left(60)
turtle.end_fill()
# 正方形
turtle.pu()
turtle.goto(-300, 0)
turtle.pd()
turtle.begin_fill()
turtle.color("brown")
turtle.right(45)
turtle.circle(100, steps=4)
turtle.left(45)
turtle.end_fill()
# 五边形
turtle.pu()
turtle.goto(-100, 0)
turtle.pd()
turtle.begin_fill()
turtle.color("gray")
turtle.right(36)
turtle.circle(100, steps=5)
turtle.left(36)
turtle.end_fill()
# 六边形
turtle.pu()
turtle.goto(120, 0)
turtle.pd()
turtle.begin_fill()
turtle.color("purple")
turtle.right(30)
turtle.circle(100, steps=6)
turtle.left(30)
turtle.end_fill()
# 八边形
turtle.pu()
turtle.goto(340, 0)
turtle.pd()
turtle.begin_fill()
turtle.color("orange")
turtle.right(22.5)
turtle.circle(100, steps=8)
turtle.left(22.5)
turtle.end_fill()
turtle.hideturtle()
turtle.done()
结果:
3.17 (Turtle:三角形面积)编写一个程序,提示用户输入一个三角形的三点:p1、p2、p3,然后在三角形的下面显示三角形的面积。
"""
测试数据:-10,50,-50,-50,50,-30
"""
import turtle
x1, y1, x2, y2, x3, y3 = eval(input("输入三角形的三个顶点:"))
turtle.pu()
turtle.goto(x1, y1)
turtle.pd()
turtle.goto(x2, y2)
turtle.goto(x3, y3)
turtle.goto(x1, y1)
side1 = ((x2-x1)**2 + (y2-y1)**2) ** 0.5
side2 = ((x2-x3)**2 + (y2-y3)**2) ** 0.5
side3 = ((x3-x1)**2 + (y3-y1)**2) ** 0.5
s = (side1+side2+side3)/2
area = (s*(s-side1)*(s-side2)*(s-side3)) ** 0.5 # 4600.0
turtle.pu()
turtle.goto(x1, y1)
turtle.pd()
turtle.write("P1(%d"%(x1)+","+"%d"%(y1)+")")
turtle.pu()
turtle.goto(x2, y2)
turtle.pd()
turtle.write("P2(%d"%(x2)+","+"%d"%(y2)+")")
turtle.pu()
turtle.goto(x3, y3)
turtle.pd()
turtle.write("P3(%d"%(x3)+","+"%d"%(y3)+")")
turtle.pu()
turtle.goto(x2, y2-30)
turtle.pd()
turtle.write("这个三角形的面积为:%0.1f" % area)
turtle.hideturtle()
turtle.done()
结果:
3.18 (Turtle:三角形的角)提示用户输入三角形的三点:p1、p2、p3,然后显示它的角度。
"""
测试数据:-10,50,-50,-50,50,-30
"""
import math
import turtle
x1, y1, x2, y2, x3, y3 = eval(input("三点坐标:"))
turtle.pu()
turtle.goto(x1, y1)
turtle.pd()
turtle.goto(x2, y2)
turtle.goto(x3, y3)
turtle.goto(x1, y1)
a = math.sqrt((x2-x3) ** 2 + (y2-y3) ** 2)
b = math.sqrt((x1-x3) ** 2 + (y1-y3) ** 2)
c = math.sqrt((x1-x2) ** 2 + (y1-y2) ** 2)
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 - b*b - a*a) / (-2 * a * b)))
turtle.pu()
turtle.goto(x1, y1)
turtle.pd()
turtle.write("P1(%0.2f"%(round(A*100)/100.0)+")")
turtle.pu()
turtle.goto(x2, y2)
turtle.pd()
turtle.write("P2(%0.2f"%(round(B*100)/100.0)+")")
turtle.pu()
turtle.goto(x3, y3)
turtle.pd()
turtle.write("P3(%0.2f"%(round(C*100)/100.0)+")")
turtle.done()
结果:
3.19 (Turtle:绘制一条线)提示用户输入两点,然后绘制一条连接两点的线并且显示这些点的坐标。
"""
测试数据:-39, 49, 50, -50
"""
import turtle
x1, y1, x2, y2 = eval(input("两点坐标:"))
turtle.pu()
turtle.goto(x1, y1)
turtle.pd()
turtle.write("(%d"%(x1)+","+"%d"%(y1)+")")
turtle.goto(x2, y2)
turtle.write("(%d"%(x2)+","+"%d"%(y2)+")")
turtle.done()