python创建400×400的Canvas画布,以画布中心为原点画出坐标轴,并按以下公式绘制函数曲线(如下图所示): x=(w0/4)×(-2sint+sin2t) y=(h0/4)×(2cost-cos2t) 式中,w0是画布宽的一半,h0是画布高的一半。t的取值范围为0~6,步长为0.01,线的粗细为5。
时间: 2024-02-01 13:12:35 浏览: 75
下面是代码实现:
```python
from tkinter import *
import math
# 设置画布大小
canvas_width = 400
canvas_height = 400
# 初始化Tkinter
root = Tk()
# 创建画布
canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()
# 画坐标轴
canvas.create_line(canvas_width / 2, 0, canvas_width / 2, canvas_height, width=2)
canvas.create_line(0, canvas_height / 2, canvas_width, canvas_height / 2, width=2)
# 定义常量
w0 = canvas_width / 2
h0 = canvas_height / 2
step = 0.01
thickness = 5
# 绘制函数曲线
for t in range(0, 601):
x = (w0 / 4) * (-2 * math.sin(t * step) + math.sin(2 * t * step))
y = (h0 / 4) * (2 * math.cos(t * step) - math.cos(2 * t * step))
canvas.create_line(x + w0, h0 - y, x + w0 + 1, h0 - y + 1, width=thickness)
# 运行Tkinter主程序
root.mainloop()
```
运行结果如下图所示:
![函数曲线](https://cdn.luogu.com.cn/upload/image_hosting/edt8v16p.png)
阅读全文