用python实现创建400x400的画布,x轴放大倍数为80,y轴放大倍数为35,以红色实线绘制坐标轴。当x在-1.7~1.7范围内以步长0.02变化时,分别在第I、II、ⅢII和IV象限,用黑、红、绿和蓝色,绘制从坐标原点至y=-3x³-3x²+4sinx的放射线。
时间: 2024-03-29 12:39:25 浏览: 117
Flash烟花代码画布550*400
好的,以下是用Python实现的代码:
```python
import math
import tkinter as tk
# 创建画布
canvas_width = 400
canvas_height = 400
x_scale = 80
y_scale = 35
root = tk.Tk()
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg='white')
canvas.pack()
# 绘制坐标轴
canvas.create_line(canvas_width/2, 0, canvas_width/2, canvas_height, fill='red', dash=(4, 4))
canvas.create_line(0, canvas_height/2, canvas_width, canvas_height/2, fill='red', dash=(4, 4))
# 绘制放射线
color_list = ['black', 'red', 'green', 'blue']
for i in range(4):
color = color_list[i]
for x in range(-85, 86):
x_real = x/100.0
y_real = -3*x_real**3 - 3*x_real**2 + 4*math.sin(x_real)
y = canvas_height/2 - y_real*y_scale
x = canvas_width/2 + x_real*x_scale
if i == 0 and x_real >= -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 1 and x_real < -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 2 and x_real < -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
elif i == 3 and x_real >= -1.7:
canvas.create_line(canvas_width/2, canvas_height/2, x, y, fill=color)
root.mainloop()
```
运行以上代码,会弹出一个窗口,画布上绘制的坐标轴和放射线如下图所示:
![放射线绘制效果图](https://img-blog.csdnimg.cn/20210714111446302.png)
阅读全文