编python代码画跳动的爱心
时间: 2024-06-02 17:06:16 浏览: 132
可以使用Matlab编写代码画跳动的爱心,以下是实现方式:
```Matlab
theta = linspace(0,2*pi);
rho = 1+sin(theta);
[x,y] = pol2cart(theta,rho);
plot(x,y,'r','Linewidth',3)
hold on
rho2 = 1+sin(theta+pi);
[x2,y2] = pol2cart(theta,rho2);
plot(x2,y2,'r','Linewidth',3)
axis equal off
```
这个代码会画出一个跳动的爱心图案。其中,theta是角度值,linspace(0,2*pi)生成了从0到2pi的一组角度值,rho是极径值,1+sin(theta)表示爱心的极径函数,这样就生成了爱心的曲线。接着使用plot函数将曲线画出来,hold on表示保持当前图形窗口不关闭,axis equal off表示关闭坐标轴。
相关问题
python画跳动的爱心代码
以下是一个用Python画跳动的爱心的代码示例:
```python
from tkinter import *
from math import sin, cos
from random import random, log
CANVAS_WIDTH = 800
CANVAS_HEIGHT = 600
CANVAS_CENTER_X = CANVAS_WIDTH // 2
CANVAS_CENTER_Y = CANVAS_HEIGHT // 2
class Heart:
def __init__(self):
self.color = "#ff8181" # 心的颜色,这里使用了粉色
self.shrink_ratio = 1.5 # 放大比例
def render(self, canvas, frame):
t = frame / 100 # 参数t,控制心的形状
x = 16 * (sin(t) ** 3)
y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
x *= self.shrink_ratio
y *= self.shrink_ratio
x, y = self.scatter_inside(x, y) # 随机内部扩散
canvas.create_oval(CANVAS_CENTER_X + x - 2, CANVAS_CENTER_Y + y - 2,
CANVAS_CENTER_X + x + 2, CANVAS_CENTER_Y + y + 2,
fill=self.color) # 在画布上绘制一个小圆点
def scatter_inside(self, x, y, beta=0.15):
ratio_x = - beta * log(random())
ratio_y = - beta * log(random())
dx = ratio_x * (x - CANVAS_CENTER_X)
dy = ratio_y * (y - CANVAS_CENTER_Y)
return x - dx, y - dy
def draw(main, render_canvas, render_heart, render_frame=0):
render_canvas.delete('all')
render_heart.render(render_canvas, render_frame)
main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
if __name__ == '__main__':
root = Tk()
canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
canvas.pack()
heart = Heart()
draw(root, canvas, heart)
root.mainloop()
```
这段代码使用了Tkinter库来创建一个窗口,并在窗口中绘制一个跳动的爱心。通过调整参数t的值,可以控制爱心的形状。同时,还使用了随机内部扩散的方法,使爱心在画布上有轻微的抖动效果。
#### 引用[.reference_title]
- *1* *2* *3* [Python制作爱心跳动代码,这就是程序员的烂漫吗](https://blog.csdn.net/aliYz/article/details/127796534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
使用Python代码画一个跳动的爱心
好的,请看下面的Python代码:
```python
import math
import time
def heart(x, y, size):
"""
画一个爱心,参数x、y是爱心的中心坐标,size是爱心的大小
"""
points = []
for i in range(0, 360):
radians = math.radians(i)
xi = x + (16 * math.sin(radians) ** 3) * size
yi = y - (13 * math.cos(radians) - 5 * math.cos(2 * radians) - 2 * math.cos(3 * radians) - math.cos(4 * radians)) * size
points.append((xi, yi))
return points
if __name__ == '__main__':
size = 10
x0, y0 = 50, 50
while True:
for t in range(0, 360, 5):
# 计算爱心的坐标
x = x0 + int(10 * math.sin(math.radians(t)))
y = y0 + int(10 * math.cos(math.radians(t)))
points = heart(x, y, size)
# 清空屏幕
print("\033[2J")
# 将爱心画出来
for j in range(40):
for i in range(80):
if (i, j) in points:
print("*", end="")
else:
print(" ", end="")
print()
# 延时
time.sleep(0.1)
```
希望这个代码能够满足你的需要,如果有其他问题,欢迎继续询问!
阅读全文