圖像處理和可視化是Python最有趣的領域之一。在這方面,煙花是一個很有趣的主題,因為它不僅涉及到圖像處理,還涉及到顏色控制、隨機數據生成、數學函數和物理學等多個領域。在本文中,我們將使用Python Matplotlib庫和NumPy庫來繪製簡單的煙花畫面。此外,我們還將使用隨機函數和三角函數來模擬煙花的不同部分的運動。
一、繪製一個簡單的煙花
要繪製一個簡單的煙花,我們需要分解它的部分,例如爆炸、火花和光芒。我們將使用matplotlib庫來繪製所有的部分。下面是一個繪製簡單煙花的代碼:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor='black')
ax = fig.add_subplot(111, frameon=False)
ax.set_xticks([])
ax.set_yticks([])
# 爆炸部分
x, y = np.random.normal(0, 0.5, (2, 1000))
ax.plot(x, y, 'o', markersize=15, color='white', alpha=0.5)
# 火花部分
theta = np.linspace(0, np.pi*2, 200)
radius = 0.25 + np.random.rand(200)*0.15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
ax.plot(offset_x, offset_y, 'o', markersize=3, color='white', alpha=0.5)
# 光芒部分
for i in range(10):
alpha = 0.1 + i/10 * 0.2
radius = 0.5 + i*0.1
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
ax.plot(offset_x, offset_y, '-', linewidth=2, color='white', alpha=alpha)
plt.show()
在上面的代碼中,我們首先創建了一個黑色背景的Matplotlib圖形,並添加了一個子圖來繪製整個煙花。然後,我們使用NumPy庫生成不同的隨機數據來模擬爆炸、火花和光芒等不同部分的效果,最後將這些部分添加到子圖中。
二、添加顏色
為了使煙花更有趣,我們可以為每個部分添加顏色。下面是一個帶有顏色的簡單煙花的代碼:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor='black')
ax = fig.add_subplot(111, frameon=False)
ax.set_xticks([])
ax.set_yticks([])
# 爆炸部分
x, y = np.random.normal(0, 0.5, (2, 1000))
color = np.random.rand(3)
ax.plot(x, y, 'o', markersize=15, color=color, alpha=0.5)
# 火花部分
theta = np.linspace(0, np.pi*2, 200)
radius = 0.25 + np.random.rand(200)*0.15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, 'o', markersize=3, color=color, alpha=0.5)
# 光芒部分
for i in range(10):
alpha = 0.1 + i/10 * 0.2
radius = 0.5 + i*0.1
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, '-', linewidth=2, color=color, alpha=alpha)
plt.show()
在上面的代碼中,我們為每個部分創建了一個隨機的RGB顏色,並將其應用到相應的部分。這使得煙花更加多彩和有趣。
三、添加運動
為了模擬真實的煙花效果,我們還可以添加運動效果。為了實現這一點,我們需要使用三角函數來模擬煙花爆炸部分的運動和火花部分的旋轉。下面是一個添加運動的簡單煙花的代碼:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor='black')
ax = fig.add_subplot(111, frameon=False)
ax.set_xticks([])
ax.set_yticks([])
# 爆炸部分
x, y = np.random.normal(0, 0.5, (2, 1000))
color = np.random.rand(3)
ax.plot(x, y, 'o', markersize=15, color=color, alpha=0.5)
# 火花部分
theta = np.linspace(0, np.pi*2, 200)
radius = 0.25 + np.random.rand(200)*0.15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, 'o', markersize=3, color=color, alpha=0.5)
# 光芒部分
for i in range(10):
alpha = 0.1 + i/10 * 0.2
radius = 0.5 + i*0.1
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, '-', linewidth=2, color=color, alpha=alpha)
# 煙花爆炸部分的運動
for i in range(100):
x, y = np.random.normal(0, 0.5, (2,500))
x *= i/100
y *= i/100
color = np.random.rand(3)
ax.plot(x, y, 'o', markersize=15, color=color, alpha=0.5)
# 火花部分的旋轉
for i in range(30):
theta += np.pi/15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, 'o', markersize=3, color=color, alpha=0.5)
plt.show()
在上面的代碼中,我們添加了兩個循環來模擬爆炸部分的運動和火花部分的旋轉。我們使用了三角函數來計算偏移量和旋轉角度。這使得煙花更加逼真和動態。
四、添加用戶交互
最後,我們還可以添加一些用戶交互功能,使得用戶可以與我們的煙花進行交互。下面是一個添加用戶交互的簡單煙花的代碼:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor='black')
ax = fig.add_subplot(111, frameon=False)
ax.set_xticks([])
ax.set_yticks([])
# 爆炸部分
x, y = np.random.normal(0, 0.5, (2, 1000))
color = np.random.rand(3)
ax.plot(x, y, 'o', markersize=15, color=color, alpha=0.5)
# 火花部分
theta = np.linspace(0, np.pi*2, 200)
radius = 0.25 + np.random.rand(200)*0.15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
sparkles, = ax.plot(offset_x, offset_y, 'o', markersize=3, color=color, alpha=0.5)
# 光芒部分
for i in range(10):
alpha = 0.1 + i/10 * 0.2
radius = 0.5 + i*0.1
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
ax.plot(offset_x, offset_y, '-', linewidth=2, color=color, alpha=alpha)
# 煙花爆炸部分的運動
for i in range(100):
x, y = np.random.normal(0, 0.5, (2,500))
x *= i/100
y *= i/100
color = np.random.rand(3)
ax.plot(x, y, 'o', markersize=15, color=color, alpha=0.5)
# 火花部分的旋轉
for i in range(30):
theta += np.pi/15
offset_x = np.cos(theta) * radius
offset_y = np.sin(theta) * radius + 0.5
color = np.random.rand(3)
sparkles.set_xdata(offset_x)
sparkles.set_ydata(offset_y)
sparkles.set_color(color)
plt.draw()
plt.pause(0.05)
plt.show()
在上面的代碼中,我們將火花部分的繪圖對象存儲在變數sparkles中,然後在循環中更新其位置和顏色。這允許用戶在煙花爆炸後通過交互來控制火花的運動。我們還使用了plt.draw()和plt.pause()函數來動態更新圖形,以使交互更加流暢。
總結
在本文中,我們通過使用Python Matplotlib庫和NumPy庫,演示了如何在煙花領域中繪製簡單的煙花效果。我們從多個方面探討了如何添加顏色、運動和用戶交互等效果,使得煙花更加真實和動態。Python的易用性和強大的功能使得在圖像處理和可視化領域中探索更多更有趣的主題成為可能。
原創文章,作者:VQMS,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/132154.html