使用Python编写一个代码给我,并完成:用tuxrle库直角三角形,底边长80,斜边长160,底角60度,线条粗6像素,线条颜色为蓝色,填充颜色为红色 用turtle库绘制平行四边形,相邻两边长度分别为150和80,对角120度和60度,线条粗6像素,颜色为绿色
时间: 2024-12-26 07:31:09 浏览: 2
首先,我们来创建一个直角三角形的示例。为了实现这个需求,我们需要导入`turtle`库并设置相应的参数。
```python
import turtle
# 设置画布和线条属性
window = turtle.Screen()
window.bgcolor("white")
triangle_turtle = turtle.Turtle()
triangle_turtle.speed(0)
triangle_turtle.pensize(6)
triangle_turtle.color("blue")
# 定义角度和边长
base_length = 80
hypotenuse_length = 160
angle = 60
# 使用海伦公式计算高(直角边)
a = base_length / 2
b = hypotenuse_length / 2
c = a ** 2 + b ** 2 - (a * b) * (math.cos(math.radians(angle)))
height = math.sqrt(c)
# 绘制直角三角形
triangle_turtle.goto(-base_length/2, height)
triangle_turtle.begin_fill()
triangle_turtle.left(angle)
triangle_turtle.forward(base_length)
triangle_turtle.right(90 - angle)
triangle_turtle.forward(height)
triangle_turtle.right(angle)
triangle_turtle.forward(base_length)
triangle_turtle.end_fill()
# 关闭窗口
window.exitonclick()
```
接下来,我们将绘制一个平行四边形的示例:
```python
import math
import turtle
# 设置画布和线条属性
window = turtle.Screen()
window.bgcolor("white")
parallelogram_turtle = turtle.Turtle()
parallelogram_turtle.speed(0)
parallelogram_turtle.pensize(6)
parallelogram_turtle.color("green")
# 边长和角度
side_a = 150
side_b = 80
diagonal_angle_1 = 120
diagonal_angle_2 = 60
# 计算对角线长度
def calculate_diagonal(side, angle):
return side * math.tan(math.radians(angle))
d1 = calculate_diagonal(side_a, diagonal_angle_1)
d2 = calculate_diagonal(side_b, diagonal_angle_2)
# 绘制平行四边形
parallelogram_turtle.penup()
parallelogram_turtle.goto(-side_b/2, d2/2)
parallelogram_turtle.pendown()
parallelogram_turtle.begin_fill()
parallelogram_turtle.forward(side_a)
parallelogram_turtle.left(diagonal_angle_1)
parallelogram_turtle.forward(d1)
parallelogram_turtle.left(diagonal_angle_2)
parallelogram_turtle.forward(side_b)
parallelogram_turtle.left(180-diagonal_angle_1)
parallelogram_turtle.forward(d1)
parallelogram_turtle.end_fill()
# 关闭窗口
window.exitonclick()
```
以上两个例子都展示了如何使用`turtle`库进行图形绘制。现在你应该能够在Python环境中运行这些代码得到所需的图形了。
阅读全文