Files
spectro/main.py
T

74 lines
2.0 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from spectro import LightWaveVec, LightDir, diffracted_light
from math import pi, acos, cos, sqrt
from color import wavelength_to_rgb
wavelen_min_nm = 380
wavelen_max_nm = 750
wavelen_step_nm = 10
"""
wavelens = [404.66e-9, 435.83e-9, 546.07e-9, 576.96e-9, 579.00e-9]
"""
wavelens = []
for i in range(wavelen_min_nm, wavelen_max_nm, wavelen_step_nm):
wavelens.append(i * 10**-9)
light_len = 36
grating_c = 1600 * 10**-9
diffraction_orders = range(0, 5)
colorlist = []
sorrow_light = []
dirs = []
for i in range(0, 10, 1):
for j in range(0, 10, 1):
dirs.append((j/10, i/10, 0, pi/2, pi/2))
for dir in dirs:
beta_i = dir[3]
gamma_i = dir[4]
cos_alpha_i = sqrt(1 - cos(beta_i)**2 - cos(gamma_i)**2)
alpha_i = acos(cos_alpha_i)
for wavelen in wavelens:
light_i = LightWaveVec(pi * 2 / wavelen,
LightDir(alpha_i, beta_i, gamma_i))
colorlist.append((0, 0, 0))
sorrow_light.append([
dir[0], dir[1], dir[2],
cos_alpha_i*light_len,
cos(beta_i)*light_len,
cos(gamma_i)*light_len,
])
for m in diffraction_orders:
light_r_list = diffracted_light(light_i, m, grating_c)
for light_r in light_r_list:
colorlist.append(wavelength_to_rgb(wavelen))
sorrow_light .append([dir[0], dir[1], dir[2],
-cos(light_r.direction.alpha)*light_len,
-cos(light_r.direction.beta)*light_len,
-cos(light_r.direction.gamma)*light_len,
])
X, Y, Z, U, V, W = zip(*sorrow_light)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0)
ax.set_xlim([-light_len, light_len])
ax.set_ylim([-light_len, light_len])
ax.set_zlim([-light_len, light_len])
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()