import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,2*np.pi,1000)
x =16*np.sin(t)**3
y =13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)- np.cos(4*t)
plt.plot(x, y, color='red')
plt.axis('equal')
plt.show()
动态爱心效果
结合pygame实现跳动爱心动画
颜色渐变和大小变化的实现方法
代码示例:
import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
running =Truewhile running:for event in pygame.event.get():if event.type== pygame.QUIT:running =Falsescreen.fill((0,0,0))time = pygame.time.get_ticks()/1000size =10+5* math.sin(time *3)points =[]for i inrange(360):angle = math.radians(i)r = size *(1- math.sin(angle))x =400+ r * math.cos(angle)y =300+ r * math.sin(angle)points.append((x, y))pygame.draw.polygon(screen,(255,0,0), points)pygame.display.flip()clock.tick(60)
pygame.quit()
3D爱心渲染
使用matplotlib或PyOpenGL创建3D爱心模型
参数方程在3D空间的应用
代码示例:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0,2*np.pi,100)
v = np.linspace(0, np.pi,100)
x = np.outer(16*np.sin(u)**3, np.ones(np.size(v)))
y = np.outer(13*np.cos(u)-5*np.cos(2*u)-2*np.cos(3*u)-np.cos(4*u), np.sin(v))
z = np.outer(13*np.cos(u)-5*np.cos(2*u)-2*np.cos(3*u)-np.cos(4*u), np.cos(v))
ax.plot_surface(x, y, z, color='red')
plt.show()