0
1

More than 1 year has passed since last update.

Python3で花火を実装してみた

Last updated at Posted at 2023-03-04

実行結果(コマンドライン)は以下の通り。

$ python3 ico11.py
x 1.1647083184890923e-16
x 1.7470624777336384e-16
x 2.3294166369781847e-16
x 2.911770796222731e-16
x 3.494124955467277e-16
x 4.076479114711823e-16
x 4.658833273956369e-16
x 5.241187433200916e-16
x 5.823541592445462e-16
x 6.405895751690008e-16
x 6.988249910934554e-16
x 7.5706040701791e-16
x 8.152958229423646e-16
x 8.735312388668193e-16
x 9.317666547912739e-16
x 9.900020707157283e-16
x 1.0482374866401831e-15
x 1.1064729025646376e-15
x 1.1647083184890924e-15

実行結果(ウィンドウ表示)は以下の通り。
Screenshot from 2023-03-04 13-37-43.png

ソースファイル(matplotlibを使った)は以下の通り。中心部から同心円状に点が広がる。再描画。

ico11.py
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
import random

radius=1.0
sectorCount=20
stackCount=20

fig = plt.figure(figsize = (20,20))
ax = fig.add_subplot(111, projection='3d')
fig.set_figheight(6)
fig.set_figwidth(6)
#plt.xlim(0.0, 10.0)
#plt.ylim(0.0, 10.0)
#plt.zlim(0.0, 10.0)
ax.set_xlim(0.0, 10.0)
ax.set_ylim(0.0, 10.0)
ax.set_zlim(0.0, 10.0)
#fig.tight_layout(False)
ax.set_xmargin(10.0)

#xx = []
#yy = []
rc_array = []
gc_array = []
bc_array = []
for i in range(int(stackCount)):
    for j in range(int(sectorCount)):
        rc_array.append( random.random() )
        gc_array.append( random.random() )
        bc_array.append( random.random() )

#plt.show()
plt.ion()
k=1
while k<20:

    plt.cla()
    xx=[]
    yy=[]
    zz=[]

    lengInv=1.0/(radius+float(k))
    radius = radius + 1
    sectorStep = 2* np.pi/ sectorCount;
    stackStep = np.pi/ stackCount;

    for i in range(int(stackCount)):
        stackAngle=(np.pi/2)-i*stackStep
        xy=radius*np.cos(stackAngle)
        z=radius*np.sin(stackAngle)

        for j in range(int(sectorCount)):
            sectorAngle = j * sectorStep
            x = xy * np.cos(sectorAngle)            
            y = xy * np.sin(sectorAngle) 

            xx.append(x)
            yy.append(y)
            zz.append(z)

    print("x",xx[1])

    for i in range(len(xx)):
        ##ax.plot_surface(xx[i], yy[i], zz[i], rstride=4, cstride=4, color='b')
        #ax.plot_wireframe(xx[i], yy[i], zz[i],color="r")
        #ax.scatter(xx[i], yy[i], zz[i], color="r", s=8.0)
        #rc = random.random()
        #gc = random.random()
        #bc = random.random()
        rc = rc_array[i]
        gc = gc_array[i]
        bc = bc_array[i]
        ax.scatter(xx[i], yy[i], zz[i], color=[rc,gc,bc], s=8.0)
        #ax.quiver(0, 0, 0,xx[i], yy[i], zz[i], color='royalblue',length=1.0, normalize=True, arrow_length_ratio=0.2)

    #ax = fig.add_subplot(111, projection='3d')
    #plt.show()
    #plt.plot()

    ax.set_xlim(-20.0, 20.0)
    ax.set_ylim(-20.0, 20.0)
    ax.set_zlim(-20.0, 20.0)
    
    #fig.canvas.draw()
    #fig.canvas.flush_events()
    plt.draw()
    plt.pause(0.01)
    k = k + 1
    #time.sleep(0.2)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1