diff --git a/another_main.py b/another_main.py new file mode 100644 index 0000000..2e12663 --- /dev/null +++ b/another_main.py @@ -0,0 +1,82 @@ +from spectro import wavelen +import matplotlib.pyplot as plt +from math import pi, cos, acos, sqrt +from color import wavelength_to_rgb +grating_c = 1600 * 10**-9 + +arrow_light = [] +colorlist = [] + +light_len = 36 + +""" +raw_source_deg = [pi/2, pi*2/3] +raw_target_deg = [pi/2, pi*2/3] +""" +raw_source_deg = [pi/2, pi/3] # beta=90°, gamma=120° +raw_target_deg = [pi/2, pi/6] # beta=90°, gamma=100° +diffraction_order = 1 + + +def calc_degs(vec): + degs = [] + tmp = 0 + for l in vec: + tmp += l**2 + tmp = sqrt(tmp) + for l in vec: + degs.append(acos(l/tmp)) + return degs + + +for i in range(-300, 300, 1): + delta_z = i/100 + + scattered_source = [ + sqrt(1-cos(raw_source_deg[0])**2-cos(raw_source_deg[1])**2)*light_len, + cos(raw_source_deg[0]) * light_len, + cos(raw_source_deg[1]) * light_len + delta_z + ] + scattered_source_deg = calc_degs(scattered_source) + arrow_light.append([ + -scattered_source[0], + -scattered_source[1], + -scattered_source[2] + delta_z, + scattered_source[0], + scattered_source[1], + scattered_source[2], + ]) + scattered_target = [ + -sqrt(1-cos(raw_target_deg[0])**2-cos(raw_target_deg[1])**2)*light_len, + cos(raw_target_deg[0]) * light_len, + cos(raw_target_deg[1]) * light_len - delta_z + ] + scattered_target_deg = calc_degs(scattered_target) + arrow_light.append([ + 0, + 0, + delta_z, + scattered_target[0], + scattered_target[1], + scattered_target[2], + ]) + + color = wavelength_to_rgb( + wavelen(scattered_source_deg[2], scattered_target_deg[2], diffraction_order, grating_c)) + + colorlist.append(color) + colorlist.append(color) + +X, Y, Z, U, V, W = zip(*arrow_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() diff --git a/main.py b/main.py index e4acb58..e1e67c0 100644 --- a/main.py +++ b/main.py @@ -19,14 +19,15 @@ light_len = 36 grating_c = 1600 * 10**-9 -diffraction_orders = [2, 1,] +diffraction_orders = [-5, 5, -4, 4, -3, 3, -2, 2, -1, 1] # Order that rended earlier might be covered by other orders # So lower order should be placed at the tail colorlist = [] -sorrow_light = [] +arrow_light = [] +""" dirs = [] for i in range(0, 3, 1): for j in range(0, 3, 1): @@ -35,7 +36,6 @@ for i in range(0, 3, 1): dirs.append((j, i, k, pi/2, pi/2)) """ dirs = [(0, 0, 0, pi/2, pi/3)] - """ for dir in dirs: beta_i = dir[3] @@ -50,7 +50,7 @@ for dir in dirs: LightDir(alpha_i, beta_i, gamma_i)) colorlist.append((0, 0, 0)) - sorrow_light.append([ + arrow_light.append([ dir[0], dir[1], dir[2], -cos_alpha_i*light_len, -cos(beta_i)*light_len, @@ -68,14 +68,14 @@ for dir in dirs: 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, - ]) + arrow_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, + ]) print(dir_range) -X, Y, Z, U, V, W = zip(*sorrow_light) +X, Y, Z, U, V, W = zip(*arrow_light) fig = plt.figure() ax = fig.add_subplot(111, projection="3d") diff --git a/spectro.py b/spectro.py index bfec21b..b23822d 100644 --- a/spectro.py +++ b/spectro.py @@ -63,19 +63,11 @@ def diffracted_light(incident_light, diffraction_order, grating_constant, is_ref ))) # wavelen = 2pi/wavenum - cos_gamma_1 = math.cos(incident_light.direction.gamma) + \ + cos_gamma = math.cos(incident_light.direction.gamma) + \ diffraction_order \ * (2*math.pi / incident_light.wavenum) \ / grating_constant - try_diffract(cos_gamma_1) - - # when diffraction_order is 0, cos_gamma_2 equals to cos_gamma_1 - if (diffraction_order != 0): - cos_gamma_2 = math.cos(incident_light.direction.gamma) - \ - diffraction_order \ - * (2*math.pi / incident_light.wavenum) \ - / grating_constant - try_diffract(cos_gamma_2) + try_diffract(cos_gamma) return ans