效果展示:
代码实现:
from math import cos, pi
import numpy as np
import cv2
class HeartSignal:
def __init__(self, frame_num=20, seed_points_num=2000, seed_num=None, frame_width=1080, frame_height=960, scale=10.1):
super().__init__()
self.frame_width = frame_width
self.frame_height = frame_height
self.center_x = self.frame_width / 2
self.center_y = self.frame_height / 2
self._points = set() # 主图坐标点
self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
self._center_diffusion_points = set() # 中心扩散效果点坐标集合
self._heart_halo_point = set() # 光晕效果坐标集合
self.frame_points = [] # 每帧动态点坐标
self.frame_num = frame_num
self.seed_num = seed_num
self.seed_points_num = seed_points_num
self.scale = scale
def heart_function(self, t, frame_idx=0, scale=5.20):
"""
图形方程
:param frame_idx: 帧的索引,根据帧数变换心形
:param scale: 放大比例
:param t: 参数
:return: 坐标
"""
trans = 3
trans = 3 - (1 + self.curve(frame_idx, self.frame_num)) * 0.5 # 改变心形饱满度度的参数
x = 15 * (np.sin(t) ** 3)
t = np.where((pi < t) & (t < 2 * pi), 2 * pi - t, t) # 翻转x > 0部分的图形到3、4象限
y = -(14 * np.cos(t) - 4 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(trans * t))
ign_area = 0.15
center_ids = np.where((x > -ign_area) & (x < ign_area))
if np.random.random() > 0.32:
x, y = np.delete(x, center_ids), np.delete(y, center_ids) # 删除稠密部分的扩散,为了美观
# 放大
x *= scale
y *= scale
# 移到画布中央
x += self.center_x
y += self.center_y
# 原心形方程
# x = 15 * (sin(t) ** 3)
# y = -(14 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))
return x.astype(int), y.astype(int)
def butterfly_function(self, t, frame_idx=0, scale=64):
"""
图形函数
:param frame_idx:
:param scale: 放大比例
:param t: 参数
:return: 坐标
"""
# 基础函数
# x = 15 * (sin(t) ** 3)
# y = -(14 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))
t = t * pi
p = np.exp(np.sin(t)) - 2.5 * np.cos(</
文章出处登录后可见!
已经登录?立即刷新