这个生物完全由代码生成,它在幽暗的世界里,摇曳着尾鳍、腹鳍,来回游啊游,但永远也逃离不了。
实现这个赛博生物的公式来自于X上的大佬@yuruyurau
用p5.js实现了各式各样的赛博生物,感兴趣的可以去看看。
基于大佬分享的代码公式,笔者也用python复现了几个赛博生物
水母1
python代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 初始化全局时间变量
t = 0.0
def mag(k, e):
"""计算向量模长"""
return np.sqrt(k**2 + e**2)
def calculate_points(t_val):
"""向量化计算所有点的坐标"""
# 生成i的向量化数组(10000到1的倒序)
i = np.arange(10000, 0, -1, dtype=np.float64)
# 计算基础参数
x = i % 200
y = i / 43.0
# 计算k和e分量
k = 5.0 * np.cos(x / 14.0) * np.cos(y / 30.0)
e = y / 8.0 - 13.0
# 计算d
d = (k**2 + e**2) / 59.0 + 4.0 # 等价于mag(k,e)**2/59 +4
# 计算q(核心公式)
atan = np.arctan2(e, k) # atan2(k,e)对应数学上的arctan(e/k)
q = 60.0 - 3.0 * np.sin(atan * e) + k * (3.0 + (4.0 / d) * np.sin(d**2 - 2 * t_val))
# 计算c(中间变量)
c = d / 2.0 + e / 99.0 - t_val / 18.0
# 计算最终坐标
px = q * np.sin(c) + 200.0
py = (q + 9.0 * d) * np.cos(c) + 200.0
return px, py
def update(frame):
global t
# 时间递增(PI/20)
t += np.pi / 20
ax.clear()
# 设置绘图参数(黑色背景,400x400范围)
ax.set_facecolor((0, 0, 0))
ax.set_xlim(0, 400)
ax.set_ylim(0, 400)
ax.set_aspect('equal')
ax.invert_yaxis()
# 计算并绘制点(白色,透明度约0.66)
px, py = calculate_points(t)
ax.scatter(px, py, s=1, c='white', alpha=0.66, edgecolors='none')
# 初始化图形
fig, ax = plt.subplots(figsize=(4, 4))
fig.patch.set_facecolor((0, 0, 0)) # 窗口背景黑色
ax.set_facecolor((0, 0, 0)) # 坐标轴背景黑色
# 创建动画(约60帧/秒)
ani = animation.FuncAnimation(
fig, update,
interval=16, # 16ms间隔≈60fps
blit=False,
frames=300
)
# 保存为GIF
ani.save(
'1.gif',
writer='pillow',
fps=45, # 帧率(决定每秒钟显示多少帧)
dpi=72
)
plt.show()
水母2
python代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 初始化全局时间变量
t = 0.0
def calculate_points(t_val):
"""向量化计算所有点的坐标和颜色"""
# 生成i的向量化数组(40000到1的倒序)
i = np.arange(40000, 0, -1, dtype=np.float64)
# 计算基础参数
x = i % 200
y = i / 200.0
# 计算k和e分量(x/8-12.5 和 y/8-12.5)
k = x / 8.0 - 12.5
e = y / 8.0 - 12.5
# 计算o(mag(k,e)²/169 → (k²+e²)/169)
o = (k**2 + e**2) / 169.0
# 计算d分量(0.5 + 5*cos(o))
d = 0.5 + 5.0 * np.cos(o)
# 计算颜色值
# 假设w=255(白色),颜色分量取计算值,alpha=36/255
color_intensity = (d * np.sin(k) * np.sin(4.0 * t_val + e))**2 * 255
color = np.clip(color_intensity / 255, 0, 1) # 归一化到[0,1]
# 计算最终坐标
px = x + d * k * np.sin(2.0 * d + o + t_val) + e * np.cos(e + t_val) + 100.0
py = o * 135.0 - y / 4.0 - d * 6.0 * np.cos(3.0 * d + 9.0 * o + t_val) + 125.0
return px, py, color
def update(frame):
global t
# 时间递增(t += PI/120)
t += np.pi / 60
# 清空画布并设置参数
ax.clear()
ax.set_facecolor((6/255, 6/255, 6/255)) # RGB(6,6,6)背景
ax.set_xlim(0, 400) # 400x400画布范围
ax.set_ylim(0, 400)
ax.set_aspect('equal')
ax.invert_yaxis()
# 计算并绘制点(颜色由计算值决定,alpha=36/255)
px, py, color = calculate_points(t)
ax.scatter(px, py, s=1, c=color, alpha=36/255, edgecolors='none')
# 初始化图形(400x400像素近似)
fig, ax = plt.subplots(figsize=(4, 4))
fig.patch.set_facecolor((6/255, 6/255, 6/255)) # 窗口背景同步
# 创建动画(约60帧/秒)
ani = animation.FuncAnimation(
fig, update,
interval=16, # 16ms间隔≈60fps
blit=False,
frames=200
)
ani.save(
'2.gif',
writer='pillow',
fps=45, # 帧率(决定每秒钟显示多少帧)
dpi=72
)
plt.show()
是不是有点像《小鲤鱼历险记》里头的小美美
章鱼
python代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 初始化全局时间变量
t = 0.0
def calculate_points(t_val):
"""向量化计算所有点的坐标"""
# 生成i的向量化数组(40000到1的倒序)
i = np.arange(40000, 0, -1, dtype=np.float64)
# 计算基础参数
x = i % 200
y = i / 400.0
# 计算k分量(x/8 - 12.5)
k = x / 8.0 - 12.5
# 计算d分量(cos(k/2) + sin(y/3) - 0.5)
d = np.cos(k / 2.0) + np.sin(y / 3.0) - 0.5
# 计算q分量(核心公式)
q = (x / 4.0) + 60.0 + d * k * (1.0 + np.cos(4.0 * d - 2.0 * t_val + y / 14.0))
# 计算c中间变量(y*d/169 - t/8 + d/9)
c = (y * d) / 169.0 - t_val / 8.0 + d / 9.0
# 计算最终坐标
px = q * 0.7 * np.cos(c) + 200.0 + 60.0 * np.sin( (3.0 * t_val)/32.0 + c/4.0 )
py = (q + 59.0) * 0.7 * np.sin(c) + 200.0
return px, py
def update(frame):
global t
# 时间递增(t += PI/30)
t += np.pi / 20
# 清空画布并设置参数
ax.clear()
ax.set_facecolor((0, 0, 0)) # 黑色背景
ax.set_xlim(0, 400) # 400x400画布
ax.set_ylim(0, 400)
ax.set_aspect('equal')
ax.invert_yaxis()
# 计算并绘制点(白色,透明度≈0.14)
px, py = calculate_points(t)
ax.scatter(px, py, s=1, c='white', alpha=36/255, edgecolors='none')
# 初始化图形
fig, ax = plt.subplots(figsize=(4, 4))
fig.patch.set_facecolor((0, 0, 0)) # 窗口背景黑色
ax.set_facecolor((0, 0, 0)) # 坐标轴背景黑色
# 创建动画(约60帧/秒)
ani = animation.FuncAnimation(
fig, update,
interval=16, # 16ms间隔≈60fps
blit=False,
frames=300
)
ani.save(
'3.gif',
writer='pillow',
fps=45, # 帧率(决定每秒钟显示多少帧)
dpi=72
)
plt.show()
看着这些赛博生物,我陷入了沉思,它们被困在程序员设定的边框里,我们也何尝不是呢!一声叹息,唉
它们的世界是被模拟出来的,我们的世界难道也是吗?
- 为什么宏观上丰富多彩,但是微观的基本粒子却都是一模一样的?这正和图片丰富多彩,但是像素是一模一样的一回事
- 为什么光速有上限?因为机器的运行速度有限
- 为什么会有 普朗克常量?因为机器的数据精度有限
- 为什么有 泡利不相容原理?看来系统采用的数据组织是多维数组
- 为什么会有量子纠缠?这实际上是引用同一个对象的两个指针
- 为什么时间有开端?系统有启动时间嘛
原来如此,程序员才是造物主,哈哈哈!